Skip to content

Match the JDK on regionMatches and drop leftover io copies - #192

Merged
dlunch merged 1 commit into
agent/string-buffer-sharingfrom
agent/string-followups
Jul 25, 2026
Merged

Match the JDK on regionMatches and drop leftover io copies#192
dlunch merged 1 commit into
agent/string-buffer-sharingfrom
agent/string-followups

Conversation

@dlunch

@dlunch dlunch commented Jul 25, 2026

Copy link
Copy Markdown
Owner

Follow-up items 1 and 2 from #191. Stacked on #191 — GitHub will retarget this to main once that merges.

regionMatches with a non-positive len

regionMatches returned false whenever len was negative. The JDK doesn't check len at all: its bounds test widens to long (toffset > (long)length() - len), which a negative len passes, and then while (len-- > 0) never runs, so the call reports a match. "Hello".regionMatches(1, "World", 2, -1) is true on a real JVM but was false here.

The bounds test now widens the same way, which also keeps a len near i32::MAX from overflowing the offset arithmetic — the previous toffset as usize + len as usize would have panicked on a debug build for a 32-bit target.

One existing assertion in test_str_06_region_matches_without_ignore_case encoded the old behaviour and was flipped.

Leftover double copies in io

BufferedReader.readLine() (two sites) and DataInputStream.readUTF() allocated an exactly sized char array, filled it, and then passed it to a copying String constructor. Routing them through JavaLangString::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 --workspace passes 453 tests with 0 failures, cargo clippy --workspace --all-targets is clean. Added test_region_matches_non_positive_len covering len of 0, -1 and i32::MIN on both overloads, offsets at and past the end, i32::MAX, and the same cases on a substring. readLine/readUTF are already covered by existing io tests.

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.
Copilot AI review requested due to automatic review settings July 25, 2026 09:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::regionMatches bounds logic to use widened arithmetic and to treat len <= 0 as a trivial match when in-range (JDK behavior).
  • Add/adjust string tests to cover len of 0, negative values (including i32::MIN), and large-length overflow scenarios.
  • Replace copying String constructors in BufferedReader.readLine() and DataInputStream.readUTF() with JavaLangString::from_utf16 to 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.

@dlunch
dlunch merged commit f8745d5 into agent/string-buffer-sharing Jul 25, 2026
9 checks passed
@dlunch
dlunch deleted the agent/string-followups branch July 25, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants