Repository navigation
fix(java): don't split surrogate pairs - #1411
Merged
Merged
Conversation
Parser.parse walks the sentence by UTF-16 code units, so it can put a boundary between the two halves of a surrogate pair and cut a character outside the BMP (emoji, or CJK Extension B characters like 𠮷) in two. This skips those positions, like the JavaScript parser does since google#1403.
tushuhei
approved these changes
Oct 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Parser.parsewalks the sentence by UTF-16 code units, so it can put a boundary between the two halves of a surrogate pair, which cuts a character outside the BMP (emoji, or CJK Extension B characters like𠮷) in two. With the default models,Parser.loadDefaultSimplifiedChineseParser().parse("我爱\uD83C\uDF63")returns["我", "爱\ud83c", "\udf63"], andParser.loadDefaultTraditionalChineseParser().translateHTMLString("吉野家の𠮷")puts a ZWSP between\ud842and\udfb7. This is the Java side of #1403, which fixed the same problem in the JavaScript parser (see also #504).This skips the positions that fall inside a surrogate pair, checked with
Character.isSurrogatePair(sentence.charAt(i - 1), sentence.charAt(i)). Text without surrogate pairs gets exactly the same phrases as before, andtranslateHTMLStringis fixed too since it usesparse.Added
testParseSurrogatePairtoParserTestwith the same model as the JavaScript test, which separates everywhere:parse("𠮷野家")returns["\ud842", "\udfb7", "野", "家"]before the change and["𠮷", "野", "家"]after.Tested with
mvn package(JDK 17) and google-java-format.