For RegExp.exec, return the rune index in the string, not the byte index

This commit is contained in:
Robert Krimen
2013-05-02 20:23:06 +02:00
parent 70e8c14c94
commit 1628647e05
2 changed files with 18 additions and 2 deletions

View File

@@ -94,13 +94,18 @@ func TestRegExp_exec(t *testing.T) {
test(`
var abc = /[abc](\d)?/.exec("a0 b c1 d3");
[ abc.length, abc.input, abc.index, abc ]
[ abc.length, abc.input, abc.index, abc ];
`, "2,a0 b c1 d3,0,a0,0")
test(`raise:
var exec = RegExp.prototype.exec;
exec("Xyzzy");
`, "TypeError: Calling RegExp.exec on a non-RegExp object")
test(`
var abc = /\w{3}\d?/.exec("CE\uFFFFL\uFFDDbox127");
[ abc.input.length, abc.length, abc.input, abc.index, abc ];
`, "11,1,CE\uFFFFL\uFFDDbox127,5,box1")
}
func TestRegExp_zaacbbbcac(t *testing.T) {

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"regexp"
"strings"
"unicode/utf8"
)
type _regExpObject struct {
@@ -106,9 +107,19 @@ func execResultToArray(runtime *_runtime, target string, result []int) *_object
valueArray[index] = UndefinedValue()
}
}
matchIndex := result[0]
if matchIndex != 0 {
matchIndex = 0
// Find the rune index in the string, not the byte index
for index := 0; index < result[0]; {
_, size := utf8.DecodeRuneInString(target[index:])
matchIndex += 1
index += size
}
}
match := runtime.newArray(valueArray)
match.set("input", toValue(target), false)
match.set("index", toValue(result[0]), false)
match.set("index", toValue(matchIndex), false)
return match
}