From 266282e4c412cc1a57f9775014b8b43470d6bb96 Mon Sep 17 00:00:00 2001 From: maximilliangrand <214999687+maximilliangrand@users.noreply.github.com> Date: Sun, 16 Aug 2026 11:59:52 +0200 Subject: [PATCH] fix(regex): keep U+2028/U+2029 in Byte segments when Kanji mode is on The Byte-mode regex matches characters with (?:.|[\r\n]). In JS a dot does not match any of the four line terminators (\n, \r, U+2028, U+2029), and only \n and \r were added back. With Kanji mode enabled this regex is used for Byte segments, so a U+2028 or U+2029 in the input matched no segment regex at all and was silently dropped, corrupting the encoded data. Add the two missing separators to the class. Co-Authored-By: Claude Opus 4.8 --- lib/core/regex.js | 2 +- test/unit/core/segments.test.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/core/regex.js b/lib/core/regex.js index 9dd13a42..8180727d 100644 --- a/lib/core/regex.js +++ b/lib/core/regex.js @@ -6,7 +6,7 @@ let kanji = '(?:[u3000-u303F]|[u3040-u309F]|[u30A0-u30FF]|' + '[u0391-u0451]|[u00A7u00A8u00B1u00B4u00D7u00F7])+' kanji = kanji.replace(/u/g, '\\u') -const byte = '(?:(?![A-Z0-9 $%*+\\-./:]|' + kanji + ')(?:.|[\r\n]))+' +const byte = '(?:(?![A-Z0-9 $%*+\\-./:]|' + kanji + ')(?:.|[\r\n\\u2028\\u2029]))+' exports.KANJI = new RegExp(kanji, 'g') exports.BYTE_KANJI = new RegExp('[^A-Z0-9 $%*+\\-./:]+', 'g') diff --git a/test/unit/core/segments.test.js b/test/unit/core/segments.test.js index ecbbb5a3..df968c1f 100644 --- a/test/unit/core/segments.test.js +++ b/test/unit/core/segments.test.js @@ -157,6 +157,20 @@ const kanjiTestData = [ { data: '皿a\n', mode: Mode.BYTE }, { data: '晒三', mode: Mode.KANJI } ] + }, + { + input: '皿a\u2028晒三', + result: [ + { data: '皿a\u2028', mode: Mode.BYTE }, + { data: '晒三', mode: Mode.KANJI } + ] + }, + { + input: '皿a\u2029晒三', + result: [ + { data: '皿a\u2029', mode: Mode.BYTE }, + { data: '晒三', mode: Mode.KANJI } + ] } ]