From 45b30cd5af5d0c069c6ad7d323d35bfa4fcd907b Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Sun, 10 May 2026 14:21:17 +0800 Subject: [PATCH] fix(strftime): use add() once for padding, minimize churn - pad(): replace per-char loop with a single add(str, ch.repeat(n)) call. The earlier `probe[0] === ch` heuristic was wrong when ch happened to equal a leading char of 'probe' (e.g. ch === 'p'). - strftime.ts: revert unrelated typing/structural refactors so the diff contains only the memoryLimit threading and the %N memory charge. - docs: rewire the deleted dos.html sidebar entry to security-model.html (with localized labels) so the deleted page does not 404 from the sidebar. Co-authored-by: Cursor --- docs/source/_data/sidebar.yml | 2 +- docs/themes/navy/languages/en.yml | 2 +- docs/themes/navy/languages/zh-cn.yml | 2 +- src/util/strftime.ts | 18 ++++++++---------- src/util/underscore.ts | 4 +--- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/docs/source/_data/sidebar.yml b/docs/source/_data/sidebar.yml index 7f54e8baa..6edcc6b29 100644 --- a/docs/source/_data/sidebar.yml +++ b/docs/source/_data/sidebar.yml @@ -19,7 +19,7 @@ tutorials: plugins: plugins.html operators: operators.html truth: truthy-and-falsy.html - dos: dos.html + security_model: security-model.html static_analysis: static-analysis.html miscellaneous: migration9: migrate-to-9.html diff --git a/docs/themes/navy/languages/en.yml b/docs/themes/navy/languages/en.yml index 51cb9052e..e75e09d00 100644 --- a/docs/themes/navy/languages/en.yml +++ b/docs/themes/navy/languages/en.yml @@ -51,7 +51,7 @@ sidebar: plugins: Plugins operators: Operators truth: Truthy and Falsy - dos: DoS + security_model: Security Model static_analysis: Static Analysis miscellaneous: Miscellaneous diff --git a/docs/themes/navy/languages/zh-cn.yml b/docs/themes/navy/languages/zh-cn.yml index cd2889a61..e5ea5d3b2 100644 --- a/docs/themes/navy/languages/zh-cn.yml +++ b/docs/themes/navy/languages/zh-cn.yml @@ -51,7 +51,7 @@ sidebar: plugins: 插件 operators: 运算符 truth: 真和假 - dos: DoS + security_model: 安全模型 static_analysis: 静态分析 miscellaneous: 其他 diff --git a/src/util/strftime.ts b/src/util/strftime.ts index 971de58b8..e6ac04ebf 100644 --- a/src/util/strftime.ts +++ b/src/util/strftime.ts @@ -4,7 +4,7 @@ import type { Limiter } from './limiter' const rFormat = /%([-_0^#:]+)?(\d+)?([EO])?(.)/ interface FormatOptions { - flags: Record; + flags: object; width?: string; modifier?: string; memoryLimit?: Pick; @@ -77,7 +77,7 @@ function getTimezoneOffset (d: LiquidDate, opts: FormatOptions) { (opts.flags[':'] ? ':' : '') + padStart(m, 2, '0') } -const formatCodes: Record unknown> = { +const formatCodes = { a: (d: LiquidDate) => d.getShortWeekdayName(), A: (d: LiquidDate) => d.getLongWeekdayName(), b: (d: LiquidDate) => d.getShortMonthName(), @@ -118,13 +118,13 @@ const formatCodes: Record unknow 't': () => '\t', 'n': () => '\n', '%': () => '%' -} -formatCodes.h = formatCodes.b +}; +(formatCodes as any).h = formatCodes.b export function strftime (d: LiquidDate, formatStr: string, memoryLimit?: Pick) { let output = '' let remaining = formatStr - let match: RegExpExecArray | null + let match while ((match = rFormat.exec(remaining))) { output += remaining.slice(0, match.index) remaining = remaining.slice(match.index + match[0].length) @@ -137,18 +137,16 @@ function format (d: LiquidDate, match: RegExpExecArray, memoryLimit?: Pick = {} + const flags = {} for (const flag of flagStr) flags[flag] = true let ret = String(convert(d, { flags, width, modifier, memoryLimit })) let padChar = padSpaceChars.has(conversion) ? ' ' : '0' - let padWidth = width ? Number(width) : (padWidths[conversion as keyof typeof padWidths] || 0) - if (!Number.isFinite(padWidth) || padWidth < 0) padWidth = 0 + let padWidth = width || padWidths[conversion] || 0 if (flags['^']) ret = ret.toUpperCase() else if (flags['#']) ret = changeCase(ret) if (flags['_']) padChar = ' ' else if (flags['0']) padChar = '0' if (flags['-']) padWidth = 0 - - memoryLimit?.use(Math.max(0, padWidth - ret.length)) + memoryLimit?.use(Number(padWidth) - ret.length) return padStart(ret, padWidth, padChar) } diff --git a/src/util/underscore.ts b/src/util/underscore.ts index 3f4eba180..665fdef6c 100644 --- a/src/util/underscore.ts +++ b/src/util/underscore.ts @@ -154,9 +154,7 @@ export function pad (str: any, length: number, ch: string, add: (str: string, ch str = String(str) const n = length - str.length if (n <= 0) return str - const padChunk = ch.repeat(n) - const probe = add('probe', ch) - return probe[0] === ch ? padChunk + str : str + padChunk + return add(str, ch.repeat(n)) } export function identify (val: T): T {