refactor(evidence): share UTF-16 page boundary checks (#16539)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-04 10:39:56 +08:00
1 parent 103c3bceeb
commit 9cf7aeb770
1 file changed
+7 -13
+7 -13
View File
@@ -29,23 +29,17 @@ export const analysisTextPage = (
const start = Math.min(offset, body.length);
const high = (n: number) => n >= 0xd800 && n <= 0xdbff;
const low = (n: number) => n >= 0xdc00 && n <= 0xdfff;
if (
start > 0 &&
start < body.length &&
high(body.charCodeAt(start - 1)) &&
low(body.charCodeAt(start))
)
const splitsPair = (index: number) =>
index > 0 &&
index < body.length &&
high(body.charCodeAt(index - 1)) &&
low(body.charCodeAt(index));
if (splitsPair(start))
return invalidAnalysisView(
`${label} offset splits a UTF-16 surrogate pair.`,
);
let end = Math.min(start + limit, body.length);
if (
end > start &&
end < body.length &&
high(body.charCodeAt(end - 1)) &&
low(body.charCodeAt(end))
)
end--;
if (end > start && splitsPair(end)) end--;
if (end === start && start < body.length)
return invalidAnalysisView(
`Increase ${label.toLowerCase()} limit to include the next complete Unicode character.`,