Match the JDK on regionMatches and drop leftover io copies - #192
Merged
Conversation
regionMatches returned false for a negative len, but the JDK's bounds test widens to long and its comparison loop simply never runs, so an otherwise in-range region reports a match. Widening the check here too keeps a len near i32::MAX from overflowing the offset arithmetic. BufferedReader.readLine() and DataInputStream.readUTF() built an exactly sized char array and then handed it to a copying constructor. They now go through JavaLangString::from_utf16, which takes the sharing constructor, so each line and each UTF entry costs one array copy instead of two.
There was a problem hiding this comment.
Pull request overview
Aligns java.lang.String#regionMatches behavior with the JDK for non-positive len and removes a redundant char-array copy in a few java.io string-construction paths by routing through the new sharing constructor helper.
Changes:
- Update
String::regionMatchesbounds logic to use widened arithmetic and to treatlen <= 0as a trivial match when in-range (JDK behavior). - Add/adjust string tests to cover
lenof0, negative values (includingi32::MIN), and large-length overflow scenarios. - Replace copying
Stringconstructors inBufferedReader.readLine()andDataInputStream.readUTF()withJavaLangString::from_utf16to avoid an extra copy.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| java_runtime/src/classes/java/lang/string.rs | Adjusts regionMatches bounds checks and len <= 0 behavior to match the JDK and avoid overflow. |
| java_runtime/tests/classes/java/lang/test_string.rs | Updates an existing assertion and adds a focused test for non-positive len cases across overloads/substrings. |
| java_runtime/src/classes/java/io/data_input_stream.rs | Routes readUTF() result construction through JavaLangString::from_utf16 to remove a redundant copy. |
| java_runtime/src/classes/java/io/buffered_reader.rs | Routes readLine() result construction through JavaLangString::from_utf16 to remove a redundant copy. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Follow-up items 1 and 2 from #191. Stacked on #191 — GitHub will retarget this to
mainonce that merges.regionMatcheswith a non-positive lenregionMatchesreturnedfalsewheneverlenwas negative. The JDK doesn't checklenat all: its bounds test widens tolong(toffset > (long)length() - len), which a negativelenpasses, and thenwhile (len-- > 0)never runs, so the call reports a match."Hello".regionMatches(1, "World", 2, -1)istrueon a real JVM but wasfalsehere.The bounds test now widens the same way, which also keeps a
lenneari32::MAXfrom overflowing the offset arithmetic — the previoustoffset as usize + len as usizewould have panicked on a debug build for a 32-bit target.One existing assertion in
test_str_06_region_matches_without_ignore_caseencoded the old behaviour and was flipped.Leftover double copies in io
BufferedReader.readLine()(two sites) andDataInputStream.readUTF()allocated an exactly sized char array, filled it, and then passed it to a copyingStringconstructor. Routing them throughJavaLangString::from_utf16— which uses the package-private sharing constructor added in #191 — drops one array copy per line and per UTF entry, and removes 10 lines.CharArrayWriter.toString()keeps copying: its buffer is mutable and outlives the call.Testing
cargo test --workspacepasses 453 tests with 0 failures,cargo clippy --workspace --all-targetsis clean. Addedtest_region_matches_non_positive_lencoveringlenof 0, -1 andi32::MINon both overloads, offsets at and past the end,i32::MAX, and the same cases on a substring.readLine/readUTFare already covered by existing io tests.