Skip to content

py: normalize bounds for string find and count - #263

Open
ac1982 wants to merge 1 commit into
go-python:mainfrom
ac1982:fix/string-search-bounds
Open

ac1982 wants to merge 1 commit into
go-python:mainfrom
ac1982:fix/string-search-bounds

Conversation

@ac1982

@ac1982 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.

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