mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
refactor(evidence): share UTF-16 page boundary checks (#16539)
This commit is contained in:
1 parent
103c3bceeb
commit
9cf7aeb770
1 file changed
+7
-13
@@ -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.`,
|
||||
|
||||
Reference in new issue
Block a user