mirror of
https://github.com/robertkrimen/otto.git
synced 2025-12-24 12:58:05 +08:00
For RegExp.exec, return the rune index in the string, not the byte index
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user