Repository navigation
Conversation
A default-constructed string view represents an empty string, but searching it for an empty string reports no match. Constructing the view from an empty string literal reports a match instead. Return the matching position directly for an empty search. This makes find, rfind, and contains agree for both empty representations. The production change is two early returns. The search unit tests pass in GCC C++11 and C++20. New cases cover read- only views, writable views, owning strings, and reverse searches with a position limit.
Forward searches currently require callers to keep the starting position inside the string. Passing a larger position reaches unchecked pointer and length calculations. The corresponding standard-library search safely returns no match, as requested in issue ashvardanian#275. Check the starting position before those calculations and return npos, the existing no-match value, when it is past the end. Update the documented behavior. Searches starting exactly at the end keep their existing meaning. The search unit tests pass in GCC C++11 and C++20. New cases cover valid end positions and positions beyond the string for substring, character, and character-set searches.
grouville
force-pushed
the
fix-forward-search-bounds
branch
from
October 8, 2026 09:05
3e1a636 to
a61c56c
Compare
This branch has not been deployed
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.
Forward searches currently require callers to keep the starting position inside the string. Passing a larger position reaches unchecked pointer and length calculations. The corresponding standard-library search safely returns no match, as requested in issue #275.
Check the starting position before those calculations and return npos, the existing no-match value, when it is past the end. Update the documented behavior. Searches starting exactly at the end keep their existing meaning.
The search unit tests pass in GCC C++11 and C++20. New cases cover valid end positions and positions beyond the string for substring, character, and character-set searches.
A minimal C++ check is:
These positions previously fell outside the documented contract and could reach unchecked pointer arithmetic. They now return npos, matching the requested STL bounds policy. No new public interface or exception is introduced.
Please merge #346 first. This branch includes that small prerequisite; GitHub removes it from this PR's diff after it merges. You can review just this fix now at grouville/StringZilla@fix-default-empty-view...fix-forward-search-bounds.
Current CI note: full CI is still red. The C++ search tests pass in the failed jobs reviewed so far, but several Python jobs crash. An isolated build of unchanged main also fails a memory check when reversing an empty collection. A separate signed repair is prepared at main...grouville:fix-empty-reverse-sort; its C++ memory checks and 4,537 Python tests pass locally. Windows, macOS and Arm runtime checks remain pending, and not every CI failure has been traced to this cause.