runtime: make stringNext use byteindex only, fix index offset

Use stringIterator.byteindex as the loop index, and remove
stringIterator.rangeindex, as "the index of the loop is the starting
position of the current rune, measured in bytes".  This patch also fixes
the current loop index returned by stringNext, using `it.byteindex'
before - not after - `length' is added.
Этот коммит содержится в:
Michael Teichgraeber 2019-01-27 00:08:23 +01:00
родитель 9092dbcc53
коммит 7461c298dd
3 изменённых файлов: 23 добавлений и 4 удалений

Просмотреть файл

@ -14,8 +14,7 @@ type _string struct {
// The iterator state for a range over a string.
type stringIterator struct {
byteindex uintptr
rangeindex uintptr
byteindex uintptr
}
// Return true iff the strings match.
@ -105,10 +104,10 @@ func stringNext(s string, it *stringIterator) (bool, int, rune) {
if len(s) <= int(it.byteindex) {
return false, 0, 0
}
i := int(it.byteindex)
r, length := decodeUTF8(s, it.byteindex)
it.byteindex += length
it.rangeindex += 1
return true, int(it.rangeindex), r
return true, i, r
}
// Convert a Unicode code point into an array of bytes and its length.

11
testdata/rangestring.go предоставленный Обычный файл
Просмотреть файл

@ -0,0 +1,11 @@
package main
func testRangeString() {
for i, c := range "abcü¢€𐍈°x" {
println(i, c)
}
}
func main() {
testRangeString()
}

9
testdata/rangestring.txt предоставленный Обычный файл
Просмотреть файл

@ -0,0 +1,9 @@
0 97
1 98
2 99
3 252
5 162
7 8364
10 66376
14 176
16 120