py: normalize bounds for string find and count by ac1982 · Pull Request #263 · go-python/gpython
Repository navigation
py: normalize bounds for string find and count - #263
Open
ac1982 wants to merge 1 commit into go-python:main from ac1982:fix/string-search-bounds
Open
py: normalize bounds for string find and count #263 ac1982 wants to merge 1 commit into go-python:main from ac1982:fix/string-search-bounds
Conversation
ac1982 commented Oct 12, 2026
Copy link Copy Markdown
'abcabc'.find('a', -3) and .count('a', -3) panic on negative Go slice bounds. Negative end indices are treated as the string's full length, and empty substring searches can match even when start is past the end or the requested interval is reversed.
Share boundary normalization between find and count: translate negative indices relative to the Unicode character length, clamp overly negative bounds and overly large ends, and return no match for reversed or out-of-range starts. The actual search/count algorithms remain unchanged.
Adds 28 direct cases with expectations independently obtained from CPython, plus six Python interpreter assertions covering ASCII, Unicode, empty strings and empty patterns. Eighteen direct cases fail before the fix, including panics.
Validation: full go test -tags=ci -race ./... on Go 1.27.1 and full GOTOOLCHAIN=go1.19.13 go test -tags=ci ./... pass on Linux/arm64. goimports/diff checks pass. Standalone vet and Staticcheck 2026.2.1 diagnostics are byte-identical to clean upstream 444ae5e (existing unkeyed literals, unreachable code and an unused assignment). Historical Staticcheck and other OS/architecture jobs were not run.
Contributor registration: go-python/license#24 . AI-assisted implementation and validation.
py: normalize bounds for string find and count
b122a93
Sign up for free
to join this conversation on GitHub .
Already have an account?
Sign in to comment