Skip to content

Make findlast, findprev, and inline-needle searches match String - #98

Merged
quinnj-bot[bot] merged 2 commits into
mainfrom
claude/search-string-semantics
Nov 19, 2026
Merged

quinnj-bot[bot] merged 2 commits into
mainfrom
claude/search-string-semantics

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

Inline strings are meant to behave like String. In 2.0, findfirst, occursin, and friends on an inline string haystack use String's byte-level search. Three neighboring cases still fell back to Base's generic AbstractString search:

  • findlast and findprev on an inline string haystack.
  • Any search with an inline string needle in a String or SubString{String} haystack. This includes replace(s::InlineString, t::InlineString => r), because replace converts the haystack to String.
  • findprev(==(c), s::InlineString, i) with a character.

The generic path compares characters instead of bytes. So these calls disagreed with String, and sometimes with findfirst on the same inline string:

julia> findfirst("x\xe9", String7("x\xe9\xa9")), findlast("x\xe9", String7("x\xe9\xa9"))
(1:2, nothing)          # String gives (1:2, 1:2)

julia> occursin(String7("x\xe9"), "x\xe9\xa9")
false                   # String needle: true

julia> findlast("", String7(""))
ERROR: BoundsError      # findlast("", "") == 1:0

julia> findprev(==(','), String7("a,é"), 4)    # start index inside 'é'
ERROR: StringIndexError # String returns 2

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 String found 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 match String on 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 other AbstractString type, and this PR leaves it alone.

Verification

  • A new block in the search testset checks findfirst, findlast, occursin, replace, and findprev at every start index against String. 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: search 1491/1491 and Aqua 11/11, including the ambiguity check.
  • The harness reran on the fixed branch with fresh seeds on all three versions (about 6.7M checks each). No search mismatches remain for inline strings. On 1.10, the only remaining forward-search differences are 1.10's own String bug, where findfirst("\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 with invoke.

case before after
findlast(",", ::String15) 106 ns 16 ns
findlast("ab", ::String255) 230 ns 56 ns
findprev(==('/'), ::String255, lastindex) 1.50 µs 31 ns
findprev(==(','), ::String15, lastindex) 13 ns 12 ns
findfirst(String7 needle, 300 B String), needle's first byte repeated 3.4 µs 0.75 µs
same, English text, word at the end (281 B) 95 ns 119 ns
same, English text, word absent (281 B) 94 ns 150 ns
same, needle's first byte absent from a 300 B haystack 21 ns 306 ns

Reverse searches get faster. With a String haystack, an inline needle now costs the same as a String needle (within 5 ns). That is slower than before when the needle's first byte is rare. The generic path happened to use memchr to skip to candidate positions, and Base's String search loops byte by byte. A memchr-based kernel would recover that case if it matters, at about 15 more lines.

CI notes

The four red checks fail on unchanged main code as well:

  • Julia pre (1.14.0-beta1, ubuntu and windows) fails runtests.jl:133, eltype(string.(AbstractString[])) == AbstractString (it evaluates to Any). Locally on 1.14.0-alpha2, it is Any with main's source and with this branch. Without InlineStrings loaded, it is AbstractString.
  • Julia min - ubuntu cannot start Julia 1.10.0 on the current runner image (libopenlibm.so: cannot enable executable stack). Julia min - windows passes.
  • TimeZones.jl integration fails its own system time-zone tests (tz_env.jl:168). TimeZones.jl's master CI 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, and replace all go through these methods. Error types match String; error messages now show the String copy instead of the inline value. Method ambiguities: Aqua passes on all three versions.

🤖 Generated with Claude Code

quinnj and others added 2 commits November 18, 2026 21:48
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

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.50%. Comparing base (1bbaafe) to head (c12a9af).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@quinnj-bot
quinnj-bot Bot merged commit 678e1ac into main Nov 19, 2026
10 of 14 checks passed
@quinnj-bot
quinnj-bot Bot deleted the claude/search-string-semantics branch November 19, 2026 05:50
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.

1 participant