Skip to content

Commit b122a93

Browse files
committed
py: normalize bounds for string find and count
1 parent 444ae5e commit b122a93

3 files changed

Lines changed: 90 additions & 16 deletions

File tree

‎py/string.go‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,26 @@ func (s String) M__contains__(item Object) (Object, error) {
619619
return NewBool(strings.Contains(string(s), string(needle))), nil
620620
}
621621

622+
// stringSearchBounds adjusts negative indices without clamping a start
623+
// beyond the end, which must not match even an empty substring.
624+
func stringSearchBounds(start, end, length int) (int, int) {
625+
if start < 0 {
626+
start += length
627+
if start < 0 {
628+
start = 0
629+
}
630+
}
631+
if end < 0 {
632+
end += length
633+
if end < 0 {
634+
end = 0
635+
}
636+
} else if end > length {
637+
end = length
638+
}
639+
return start, end
640+
}
641+
622642
func (s String) Count(args Tuple) (Object, error) {
623643
var (
624644
pysub Object
@@ -636,14 +656,9 @@ func (s String) Count(args Tuple) (Object, error) {
636656
end = int(pyend.(Int))
637657
size = s.len()
638658
)
639-
if beg > size {
640-
beg = size
641-
}
642-
if end < 0 {
643-
end = size
644-
}
645-
if end > size {
646-
end = size
659+
beg, end = stringSearchBounds(beg, end, size)
660+
if beg > end {
661+
return Int(0), nil
647662
}
648663

649664
var (
@@ -670,14 +685,9 @@ func (s String) find(args Tuple) (Object, error) {
670685
end = int(pyend.(Int))
671686
size = s.len()
672687
)
673-
if beg > size {
674-
beg = size
675-
}
676-
if end < 0 {
677-
end = size
678-
}
679-
if end > size {
680-
end = size
688+
beg, end = stringSearchBounds(beg, end, size)
689+
if beg > end {
690+
return Int(-1), nil
681691
}
682692

683693
var (

‎py/string_search_bounds_test.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2026 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package py
6+
7+
import (
8+
"fmt"
9+
"testing"
10+
)
11+
12+
func TestStringSearchBounds(t *testing.T) {
13+
// Expected results were checked against CPython.
14+
for _, tc := range []struct {
15+
s, sub string
16+
start, end, find, count int
17+
}{
18+
{"abcabc", "a", -3, 6, 3, 1},
19+
{"abcabc", "a", 0, -1, 0, 2},
20+
{"abcabc", "c", 0, -1, 2, 1},
21+
{"abcabc", "a", -100, 100, 0, 2},
22+
{"abcabc", "a", 0, -100, -1, 0},
23+
{"abcabc", "", 7, 100, -1, 0},
24+
{"abcabc", "", 4, 2, -1, 0},
25+
{"abcabc", "", 6, 6, 6, 1},
26+
{"abcabc", "", 0, -1, 0, 6},
27+
{"é世界é", "é", -1, 4, 3, 1},
28+
{"é世界é", "界", 0, -1, 2, 1},
29+
{"é世界é", "", -3, -1, 1, 3},
30+
{"", "", 0, 0, 0, 1},
31+
{"", "", 1, 1, -1, 0},
32+
} {
33+
for _, method := range []string{"find", "count"} {
34+
t.Run(fmt.Sprintf("%s/%q/%q/%d:%d", method, tc.s, tc.sub, tc.start, tc.end), func(t *testing.T) {
35+
defer func() {
36+
if p := recover(); p != nil {
37+
t.Errorf("unexpected panic: %v", p)
38+
}
39+
}()
40+
args := Tuple{String(tc.sub), Int(tc.start), Int(tc.end)}
41+
var got Object
42+
var err error
43+
want := tc.find
44+
if method == "find" {
45+
got, err = String(tc.s).find(args)
46+
} else {
47+
got, err = String(tc.s).Count(args)
48+
want = tc.count
49+
}
50+
if err != nil || got != Int(want) {
51+
t.Errorf("got %v, %v; want %d", got, err, want)
52+
}
53+
})
54+
}
55+
}
56+
}

‎py/tests/string.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,3 +955,11 @@ def __index__(self):
955955

956956

957957
doc="finished"
958+
959+
# Search bounds count Unicode characters and accept negative indices.
960+
assert 'abcabc'.find('a', -3) == 3
961+
assert 'abcabc'.count('c', 0, -1) == 1
962+
assert 'abc'.find('', 4) == -1
963+
assert 'abc'.count('', 4) == 0
964+
assert 'é世界é'.find('é', -1) == 3
965+
assert 'é世界é'.count('', -3, -1) == 3

0 commit comments

Comments
 (0)