Add window-bounded String and char overloads to SubSequence#11796
Open
dougqh wants to merge 6 commits into
Open
Add window-bounded String and char overloads to SubSequence#11796dougqh wants to merge 6 commits into
dougqh wants to merge 6 commits into
Conversation
Contributor
🟢 Java Benchmark SLOs — All performance SLOs passed
PR vs. master results
Commit: Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion. |
equals/equalsIgnoreCase/startsWith/endsWith/indexOf take a String and delegate to String's region/offset methods (regionMatches, startsWith, indexOf) instead of a per-char CharSequence loop. Each guards against this view's [beginIndex, endIndex) window first so the delegated read stays in range, then reuses the JDK's backing-array compare (Latin1 fast path / intrinsics). equals(Object) now routes Strings through the fast path, keeping the charAt loop only for non-String CharSequences. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Single-character leading/trailing/search checks (e.g. a leading '{' or a
trailing ';') read charAt(beginIndex)/charAt(endIndex-1) or delegate to
String.indexOf(int, from), each bounded to the [beginIndex, endIndex)
window. indexOf(char) returns a window-relative offset or -1.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…uals - lastIndexOf(String) and lastIndexOf(char), window-bounded like indexOf, returning a window-relative offset. - Restructure equality to mirror String's API: equals(String) is the region-compare fast path, contentEquals(CharSequence) is the general char-by-char comparison, and equals(Object) dispatches String -> the fast path, any other CharSequence -> contentEquals. This keeps two equal-content views equal() while giving String args the fast path. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Replace toString().hashCode() with the String hash polynomial evaluated directly over [beginIndex, endIndex). Same value (so equals/hashCode stay consistent), but hashing a view no longer materializes a substring -- preserving the zero-copy property the class exists for. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
e3acc99 to
5ec05e7
Compare
This comment has been minimized.
This comment has been minimized.
The #11736 CharSequence (charAt-loop) versions are superseded by the String-delegating overloads here; String-literal callers (SQLCommenter) bind to the String overloads. Removes the redundant pair + their tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The CharSequence contract treats start/end as offsets in this view's coordinates, so absolute end is beginIndex+end, not beginIndex+start+end (which overshoots by start; only correct when start==0). Latent since #10640 -- no production caller invoked it with start>0. Adds a regression test including the nested case the bug broke worst. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What Does This Do
Rounds out
SubSequenceto support the other string comparison methods.Motivation
SubSequenceis intended as a drop-in replacement for aString.substringcall followed up a comparison operation. To fulfill that purpose, this PR adds methods that were missing in prior PRs.Additional Notes
Since normally the
Stringequivalents don't typically acceptCharSequence, most comparison methods have been modified to just takeString.This simplifies the implementation of the methods and should also make them a little faster, since
Stringmethods are often intrinsified.String'sstartsWith(prefix, off)/regionMatches/indexOf(…, from)bound-check against the backing string's full length, not the view'sendIndex. So each method first guards against this view's window, then delegates:equals(String)regionMatches(beginIndex, o, 0, o.length())o.length() == length()equalsIgnoreCase(String)regionMatches(true, …)o.length() == length()startsWith(String)startsWith(prefix, beginIndex)prefix.length() <= length()endsWith(String)startsWith(suffix, endIndex - len)len <= length()indexOf(String)indexOf(needle, beginIndex)idx + needle.length() <= endIndexlastIndexOf(String)lastIndexOf(needle, endIndex - len)idx >= beginIndexstartsWith(char)charAt(beginIndex)beginIndex < endIndexendsWith(char)charAt(endIndex - 1)beginIndex < endIndexindexOf(char)indexOf(c, beginIndex)idx < endIndexlastIndexOf(char)lastIndexOf(c, endIndex - 1)idx >= beginIndexindexOf/lastIndexOfreturn a window-relative offset (or-1). A needle/char present in the backing string but outside the view is correctly not found.Equality
Mirrors
String's split betweenequalsandcontentEquals:equals(String)— region-compare fast path.contentEquals(CharSequence)— general char-by-char comparison (null → false); two views with equal content are content-equal.equals(Object)—String→ the fast path; any otherCharSequence(incl. anotherSubSequence) →contentEquals.equalsIgnoreCase(null)returnsfalse, matchingString.equalsIgnoreCase.hashCode()is theStringhash polynomial computed directly over the window (same value astoString().hashCode(), but without materializing the substring), so it stays consistent withequalswhile preserving the zero-copy property even when a view is hashed.Tests
SubSequenceTestgains coverage for case-sensitivity, over/undershoot of both window ends (String and char), theequals(Object)dispatch andcontentEquals, the empty window, and window-relativeindexOf/lastIndexOf.🤖 Generated with Claude Code
Also fixes
A latent bug in
SubSequence.subSequence(int start, int end)(from #10640): the absolute end index was computed asbeginIndex + start + end, overshooting bystart(only correct whenstart == 0). TheCharSequencecontract treatsstart/endas offsets in this view's coordinates, so it is nowbeginIndex + end. Latent — no production caller passedstart > 0— with a regression test added (incl. the nested-subSequence case).