Skip to content

Commit 429bbf3

Browse files
TocarIPnigeltao
authored andcommitted
strings: fix and reenable amd64 Index for 17-31 byte strings
Fixes #15689 Change-Id: I56d0103738cc35cd5bc5e77a0e0341c0dd55530e Reviewed-on: https://go-review.googlesource.com/23440 Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Nigel Tao <[email protected]>
1 parent 42da35c commit 429bbf3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/runtime/asm_amd64.s

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ partial_success9to15:
17871787
JB loop9to15
17881788
JMP fail
17891789
_16_or_more:
1790-
CMPQ AX, $17
1790+
CMPQ AX, $16
17911791
JA _17_to_31
17921792
MOVOU (BP), X1
17931793
LEAQ -15(DI)(DX*1), DX
@@ -1801,7 +1801,6 @@ loop16:
18011801
CMPQ DI,DX
18021802
JB loop16
18031803
JMP fail
1804-
//TODO: the code below is wrong. Fix it. See #15679.
18051804
_17_to_31:
18061805
LEAQ 1(DI)(DX*1), DX
18071806
SUBQ AX, DX

src/strings/strings_amd64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package strings
77
// indexShortStr returns the index of the first instance of c in s, or -1 if c is not present in s.
88
// indexShortStr requires 2 <= len(c) <= shortStringLen
99
func indexShortStr(s, c string) int // ../runtime/asm_$GOARCH.s
10-
const shortStringLen = 16 // TODO: restore to 31 when #15679 is fixed
10+
const shortStringLen = 31
1111

1212
// Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
1313
func Index(s, sep string) int {

src/strings/strings_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,43 @@ func TestLastIndexByte(t *testing.T) {
190190
}
191191
}
192192

193+
func simpleIndex(s, sep string) int {
194+
n := len(sep)
195+
for i := n; i <= len(s); i++ {
196+
if s[i-n:i] == sep {
197+
return i - n
198+
}
199+
}
200+
return -1
201+
}
202+
203+
func TestIndexRandom(t *testing.T) {
204+
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
205+
for times := 0; times < 10; times++ {
206+
for strLen := 5 + rand.Intn(5); strLen < 140; strLen += 10 { // Arbitrary
207+
s1 := make([]byte, strLen)
208+
for i := range s1 {
209+
s1[i] = chars[rand.Intn(len(chars))]
210+
}
211+
s := string(s1)
212+
for i := 0; i < 50; i++ {
213+
begin := rand.Intn(len(s) + 1)
214+
end := begin + rand.Intn(len(s)+1-begin)
215+
sep := s[begin:end]
216+
if i%4 == 0 {
217+
pos := rand.Intn(len(sep) + 1)
218+
sep = sep[:pos] + "A" + sep[pos:]
219+
}
220+
want := simpleIndex(s, sep)
221+
res := Index(s, sep)
222+
if res != want {
223+
t.Errorf("Index(%s,%s) = %d; want %d", s, sep, res, want)
224+
}
225+
}
226+
}
227+
}
228+
}
229+
193230
var indexRuneTests = []struct {
194231
s string
195232
rune rune

0 commit comments

Comments
 (0)