Procházet zdrojové kódy

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

Dmitry Panov před 8 roky
rodič
revize
89ee17f7c2
2 změnil soubory, kde provedl 10 přidání a 0 odebrání
  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) {
 	const SCRIPT = `
 	"абвабаб".lastIndexOf("аб", 3)
+	`
 
+	testScript1(SCRIPT, intToValue(3), t)
+}
+
+func TestUnicodeLastIndexOf1(t *testing.T) {
+	const SCRIPT = `
+	"abꞐcde".lastIndexOf("cd");
 	`
 
 	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))
 	}
 
+	if maxStart := int64(len(s) - len(ss)); start > maxStart {
+		start = maxStart
+	}
 	// TODO: optimise
 	for start >= 0 {
 		for i := int64(0); i < int64(len(ss)); i++ {