Repository navigation
Make findlast, findprev, and inline-needle searches match String - #98
Merged
Merged
Conversation
findlast/findprev on an inline string haystack, and any search with an inline string needle in a String haystack, fall back to Base's generic character-level search, which differs from String on malformed UTF-8 and empty needles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
findlast/findprev on an inline string, and forward or reverse searches with an inline string needle in a String haystack, fell back to Base's generic AbstractString search. That path compares characters rather than bytes, rejects an empty needle at index 0, and rejects a findprev start index inside a character, so results disagreed with String (and with findfirst on the same inline string) for malformed UTF-8 and empty needles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #98 +/- ##
==========================================
+ Coverage 91.46% 91.50% +0.04%
==========================================
Files 5 5
Lines 726 730 +4
==========================================
+ Hits 664 668 +4
Misses 62 62 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Why
Inline strings are meant to behave like
String. In 2.0,findfirst,occursin, and friends on an inline string haystack useString's byte-level search. Three neighboring cases still fell back to Base's genericAbstractStringsearch:findlastandfindprevon an inline string haystack.StringorSubString{String}haystack. This includesreplace(s::InlineString, t::InlineString => r), becausereplaceconverts the haystack toString.findprev(==(c), s::InlineString, i)with a character.The generic path compares characters instead of bytes. So these calls disagreed with
String, and sometimes withfindfirston the same inline string:Malformed UTF-8 is common in CSV data that was saved as Latin-1, and inline strings usually hold CSV columns. A differential harness that compares every inline width against
Stringfound these mismatches. The harness ran on Julia 1.10, 1.12, and 1.13.What changed
Four single-expression methods route these searches through
String's own methods. Results now matchStringon every supported Julia version by construction:_searchindex(::Union{String, SubString{String}}, ::InlineString, i)_rsearchindex(::InlineString, ::Union{String, SubString{String}, InlineString}, i)_rsearchindex(::Union{String, SubString{String}}, ::InlineString, i)findprev(::Fix2{isequal/==, AbstractChar}, ::InlineString, i)SubString{<:InlineString}haystacks still use Base's generic methods. That is the same behavior as any otherAbstractStringtype, and this PR leaves it alone.Verification
searchtestset checksfindfirst,findlast,occursin,replace, andfindprevat every start index againstString. It covers malformed and empty needles, with the inline string as haystack, needle, or both. Before the fix (9b9b60f), that testset had 54 failures and 26 errors on 1.12. After the fix (c12a9af), the full suite passes on 1.10.11, 1.12.6, and 1.13.4:search1491/1491 and Aqua 11/11, including the ambiguity check.Stringbug, wherefindfirst("\xbc", "tü")throws.Performance
Each new method costs one small allocation, for
String(x)of at most 255 bytes. Measured on Julia 1.12.6 (Apple M3 Max) with Chairmarks, before and after interleaved in one process. "Before" replays Base's generic method withinvoke.findlast(",", ::String15)findlast("ab", ::String255)findprev(==('/'), ::String255, lastindex)findprev(==(','), ::String15, lastindex)findfirst(String7 needle, 300 B String), needle's first byte repeatedReverse searches get faster. With a
Stringhaystack, an inline needle now costs the same as aStringneedle (within 5 ns). That is slower than before when the needle's first byte is rare. The generic path happened to usememchrto skip to candidate positions, and Base'sStringsearch loops byte by byte. Amemchr-based kernel would recover that case if it matters, at about 15 more lines.CI notes
The four red checks fail on unchanged
maincode as well:Julia pre(1.14.0-beta1, ubuntu and windows) failsruntests.jl:133,eltype(string.(AbstractString[])) == AbstractString(it evaluates toAny). Locally on 1.14.0-alpha2, it isAnywithmain's source and with this branch. Without InlineStrings loaded, it isAbstractString.Julia min - ubuntucannot start Julia 1.10.0 on the current runner image (libopenlibm.so: cannot enable executable stack).Julia min - windowspasses.TimeZones.jlintegration fails its own system time-zone tests (tz_env.jl:168). TimeZones.jl'smasterCI fails the same tests (runs on Nov 16, 17, and 18).Review notes
I re-read the diff for callers, edge cases, and error paths.
count,findall,split, andreplaceall go through these methods. Error types matchString; error messages now show theStringcopy instead of the inline value. Method ambiguities: Aqua passes on all three versions.🤖 Generated with Claude Code