fix: trim custom characters by Unicode code point (#955)

This commit is contained in:
Jake Wang
2026-09-20 20:21:09 +08:00
committed by GitHub
parent 0340ed5838
commit 3fe244f061
2 changed files with 40 additions and 13 deletions
+23 -13
View File
@@ -34,16 +34,32 @@ export function prepend (this: FilterImpl, v: string, arg: string) {
return rhs + lhs
}
function getTrimStart (str: string, chars: Set<string>): number {
let start = 0
for (const char of str) {
if (!chars.has(char)) break
start += char.length
}
return start
}
function getTrimEnd (str: string, chars: Set<string>, start = 0): number {
let end = str.length
while (end > start) {
const size = end - start >= 2 && str.codePointAt(end - 2)! > 0xFFFF ? 2 : 1
if (!chars.has(str.slice(end - size, end))) break
end -= size
}
return end
}
export function lstrip (this: FilterImpl, v: string, chars?: string) {
const str = stringify(v)
this.context.memoryLimit.use(str.length)
if (chars) {
chars = stringify(chars)
this.context.memoryLimit.use(chars.length)
for (let i = 0, set = new Set(chars); i < str.length; i++) {
if (!set.has(str[i])) return str.slice(i)
}
return ''
return str.slice(getTrimStart(str, new Set(chars)))
}
return str.trimStart()
}
@@ -89,10 +105,7 @@ export function rstrip (this: FilterImpl, str: string, chars?: string) {
if (chars) {
chars = stringify(chars)
this.context.memoryLimit.use(chars.length)
for (let i = str.length - 1, set = new Set(chars); i >= 0; i--) {
if (!set.has(str[i])) return str.slice(0, i + 1)
}
return ''
return str.slice(0, getTrimEnd(str, new Set(chars)))
}
return str.trimEnd()
}
@@ -113,11 +126,8 @@ export function strip (this: FilterImpl, v: string, chars?: string) {
if (chars) {
const set = new Set(stringify(chars))
this.context.memoryLimit.use(set.size)
let i = 0
let j = str.length - 1
while (set.has(str[i])) i++
while (j >= i && set.has(str[j])) j--
return str.slice(i, j + 1)
const start = getTrimStart(str, set)
return str.slice(start, getTrimEnd(str, set, start))
}
return str.trim()
}