Skip to content

Commit 4b114ff

Browse files
committed
py: handle corner cases for str.find
Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent e4b6e82 commit 4b114ff

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

py/string.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,18 @@ func (s String) find(args Tuple) (Object, error) {
602602
}
603603

604604
var (
605-
beg = int(pybeg.(Int))
606-
end = int(pyend.(Int))
605+
beg = int(pybeg.(Int))
606+
end = int(pyend.(Int))
607+
size = s.len()
607608
)
609+
if beg > size {
610+
beg = size
611+
}
608612
if end < 0 {
609-
end = s.len()
613+
end = size
614+
}
615+
if end > size {
616+
end = size
610617
}
611618

612619
var (

py/string_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ func TestStringFind(t *testing.T) {
6767
end: -1,
6868
idx: 6,
6969
},
70+
{
71+
str: "0123456789",
72+
sub: "6",
73+
beg: 100,
74+
end: -1,
75+
idx: -1,
76+
},
77+
{
78+
str: "0123456789",
79+
sub: "6",
80+
beg: 2,
81+
end: 1,
82+
idx: -1,
83+
},
7084
} {
7185
t.Run(tc.str+":"+tc.sub, func(t *testing.T) {
7286
beg := tc.beg

0 commit comments

Comments
 (0)