Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4b6e82

Browse files
committed
py: handle end=-1 in str.find
Signed-off-by: Sebastien Binet <[email protected]>
1 parent ab6c445 commit e4b6e82

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

py/string.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ func (s String) find(args Tuple) (Object, error) {
593593
var (
594594
pysub Object
595595
pybeg Object = Int(0)
596-
pyend Object = Int(len(s))
596+
pyend Object = Int(s.len())
597597
pyfmt = "s|ii:find"
598598
)
599599
err := ParseTuple(args, pyfmt, &pysub, &pybeg, &pyend)
@@ -604,6 +604,12 @@ func (s String) find(args Tuple) (Object, error) {
604604
var (
605605
beg = int(pybeg.(Int))
606606
end = int(pyend.(Int))
607+
)
608+
if end < 0 {
609+
end = s.len()
610+
}
611+
612+
var (
607613
off = s.slice(0, beg, s.len()).len()
608614
str = string(s.slice(beg, end, s.len()))
609615
sub = string(pysub.(String))

py/string_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ func TestStringFind(t *testing.T) {
6060
end: 7,
6161
idx: 6,
6262
},
63+
{
64+
str: "0123456789",
65+
sub: "6",
66+
beg: 1,
67+
end: -1,
68+
idx: 6,
69+
},
6370
} {
6471
t.Run(tc.str+":"+tc.sub, func(t *testing.T) {
6572
beg := tc.beg

0 commit comments

Comments
 (0)