Skip to content

Commit

Permalink
Use FindAllSubmatchIndexASCII() instead of FindAllSubmatchIndexUTF8()…
Browse files Browse the repository at this point in the history
… for non-Unicode strings. This improves performance a bit.
  • Loading branch information
dop251 committed Feb 24, 2017
1 parent 9178844 commit 2acb688
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (r *regexp2Wrapper) findAllSubmatchIndexUTF16(s unicodeString, n int) [][]i
func (r *regexp2Wrapper) FindAllSubmatchIndex(s valueString, n int) [][]int {
switch s := s.(type) {
case asciiString:
return r.FindAllSubmatchIndexUTF8(string(s), n)
return r.FindAllSubmatchIndexASCII(string(s), n)
case unicodeString:
return r.findAllSubmatchIndexUTF16(s, n)
default:
Expand Down
24 changes: 24 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,27 @@ func TestRegexpDotMatchSlashRInGroup(t *testing.T) {

testScript1(SCRIPT, valueFalse, t)
}

func TestRegexpSplitWithBackRef(t *testing.T) {
const SCRIPT = `
"a++b+-c".split(/([+-])\1/).join(" $$ ")
`

testScript1(SCRIPT, asciiString("a $$ + $$ b+-c"), t)
}

func BenchmarkRegexpSplitWithBackRef(b *testing.B) {
const SCRIPT = `
"aaaaaaaaaaaaaaaaaaaaaaaaa++bbbbbbbbbbbbbbbbbbbbbb+-ccccccccccccccccccccccc".split(/([+-])\1/)
`
b.StopTimer()
prg, err := Compile("test.js", SCRIPT, false)
if err != nil {
b.Fatal(err)
}
vm := New()
b.StartTimer()
for i := 0; i < b.N; i++ {
vm.RunProgram(prg)
}
}

0 comments on commit 2acb688

Please sign in to comment.