Browse Source

Fixed panic on lastIndexOf() for unicode strings (fixes #25)

Dmitry Panov 8 years ago
parent
commit
89ee17f7c2
2 changed files with 10 additions and 0 deletions
  1. 7 0
      runtime_test.go
  2. 3 0
      string_unicode.go

+ 7 - 0
runtime_test.go

@@ -204,7 +204,14 @@ func TestLastIndexOf(t *testing.T) {
 func TestUnicodeLastIndexOf(t *testing.T) {
 func TestUnicodeLastIndexOf(t *testing.T) {
 	const SCRIPT = `
 	const SCRIPT = `
 	"абвабаб".lastIndexOf("аб", 3)
 	"абвабаб".lastIndexOf("аб", 3)
+	`
 
 
+	testScript1(SCRIPT, intToValue(3), t)
+}
+
+func TestUnicodeLastIndexOf1(t *testing.T) {
+	const SCRIPT = `
+	"abꞐcde".lastIndexOf("cd");
 	`
 	`
 
 
 	testScript1(SCRIPT, intToValue(3), t)
 	testScript1(SCRIPT, intToValue(3), t)

+ 3 - 0
string_unicode.go

@@ -264,6 +264,9 @@ func (s unicodeString) lastIndex(substr valueString, start int64) int64 {
 		panic(fmt.Errorf("Unknown string type: %T", substr))
 		panic(fmt.Errorf("Unknown string type: %T", substr))
 	}
 	}
 
 
+	if maxStart := int64(len(s) - len(ss)); start > maxStart {
+		start = maxStart
+	}
 	// TODO: optimise
 	// TODO: optimise
 	for start >= 0 {
 	for start >= 0 {
 		for i := int64(0); i < int64(len(ss)); i++ {
 		for i := int64(0); i < int64(len(ss)); i++ {