Skip to content

Commit 3f97b6a

Browse files
committed
prevent panic with multi-byte unicode bmPrefix that partially match. fixes #6
1 parent b93eddc commit 3f97b6a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

regexp_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,24 @@ func TestThreeByteUnicode_InputOnly(t *testing.T) {
747747
}
748748
}
749749

750+
func TestMultibyteUnicode_MatchPartialPattern(t *testing.T) {
751+
re := MustCompile("猟な", 0)
752+
if m, err := re.MatchString("なあ🍺な"); err != nil {
753+
t.Fatal(err)
754+
} else if m {
755+
t.Fatal("Expected no match")
756+
}
757+
}
758+
759+
func TestMultibyteUnicode_Match(t *testing.T) {
760+
re := MustCompile("猟な", 0)
761+
if m, err := re.MatchString("なあ🍺猟な"); err != nil {
762+
t.Fatal(err)
763+
} else if !m {
764+
t.Fatal("Expected match")
765+
}
766+
}
767+
750768
/*
751769
func TestPcreStuff(t *testing.T) {
752770
re := MustCompile(`(?(?=(a))a)`, Debug)

syntax/prefix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,9 @@ func (b *BmPrefix) Scan(text []rune, index, beglimit, endlimit int) int {
704704
advance = b.positive[match]
705705
if (chTest & 0xFF80) == 0 {
706706
test2 = (match - startmatch) + b.negativeASCII[chTest]
707-
} else if nil != b.negativeUnicode {
707+
} else if chTest < 0xffff && len(b.negativeUnicode) > 0 {
708708
unicodeLookup = b.negativeUnicode[chTest>>8]
709-
if unicodeLookup != nil {
709+
if len(unicodeLookup) > 0 {
710710
test2 = (match - startmatch) + unicodeLookup[chTest&0xFF]
711711
} else {
712712
test += advance

0 commit comments

Comments
 (0)