Implement JDK8 zero-width match semantics in String.split - #299
Open
rootkiller6788 wants to merge 1 commit into
Open
Implement JDK8 zero-width match semantics in String.split#299rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
String.split re-ran the regex on progressively trimmed suffixes of the
input, which made zero-width matches (the empty regex, \b, lookaheads)
produce an extra leading empty string: on a JDK8+ runtime "abc".split("")
yielded {"", "a", "b", "c"} while the JRE yields {"a", "b", "c"}.
Mirror java.util.regex.Pattern.split instead: find matches on the whole
input, keep the global regex lastIndex in sync, skip a zero-width match
at the very beginning, and emit the trailing piece after the last match.
This aligns split with the JRE for empty and zero-width regexes and for
the limit variants.
Fixes google#143.
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.
Description
Fixes #143: j2cl's
String.splitdid not implement JDK8 zero-width match semantics."abc".split("")returned{"", "a", "b", "c"}on JDK8+ runtimes where the JRE returns{"a", "b", "c"}. The root cause was thatsplitre-ran the regex against progressively trimmed suffixes of the input, so any zero-width-capable regex (empty regex,\b, lookaheads,a*/b*/\s*,^) re-matched at the start of each trimmed suffix and produced an extra empty leading piece.Change
Rewrote
String.split(String, int)to mirrorjava.util.regex.Pattern.split:index;lastIndexis kept in sync so zero-width matches still make progress (mimickingMatcher);Verification
String.splitoracle over 4200 (input, regex, limit) combinations: 4194/4200 match. The 6 remaining differences are all the Java$regex matching before a final line terminator (Java regex engine semantic) which is outside the split algorithm and is already documented in thesplitjavadoc as a JS-vs-Java regex difference.StringTestsplit assertions pass, including theissue2742"/".split("/", 0)edge case.testSplit_emptyExprnow asserts JDK8 semantics and covers the limit variants.Type
Bug fix.