From 9cf7aeb770dee7b04d76ffd346ab4af1d003aac2 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 10:39:56 +0800 Subject: [PATCH] refactor(evidence): share UTF-16 page boundary checks (#16539) --- src/domain/analysisView/analysisViewText.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/domain/analysisView/analysisViewText.ts b/src/domain/analysisView/analysisViewText.ts index e9e494ca1ef..940c835e800 100644 --- a/src/domain/analysisView/analysisViewText.ts +++ b/src/domain/analysisView/analysisViewText.ts @@ -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.`,