From ea3dcf3ca5285b4d44bad9d9c449e0458b0671f4 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Wed, 6 Nov 2019 21:38:39 +0100 Subject: [PATCH] Fix a crash --- m/matchRanges.go | 5 +++++ m/matchRanges_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/m/matchRanges.go b/m/matchRanges.go index aae381f7..6ac64559 100644 --- a/m/matchRanges.go +++ b/m/matchRanges.go @@ -24,6 +24,11 @@ func _ToRunePositions(byteIndices [][]int, matchedString *string) [][2]int { var returnMe [][2]int + if len(byteIndices) == 0 { + // Nothing to see here, move along + return returnMe + } + fromByte := byteIndices[len(returnMe)][0] toByte := byteIndices[len(returnMe)][1] fromRune := -1 diff --git a/m/matchRanges_test.go b/m/matchRanges_test.go index cef0c500..574ee87e 100644 --- a/m/matchRanges_test.go +++ b/m/matchRanges_test.go @@ -51,3 +51,15 @@ func TestUtf8(t *testing.T) { assert.Assert(t, matchRanges.InRange(3)) // รค assert.Assert(t, !matchRanges.InRange(4)) // - } + +func TestNoMatch(t *testing.T) { + // This test verifies that the match ranges are by rune rather than by byte + unicodes := "gris" + matchRanges := GetMatchRanges(&unicodes, regexp.MustCompile("apa")) + + assert.Assert(t, !matchRanges.InRange(0)) + assert.Assert(t, !matchRanges.InRange(1)) + assert.Assert(t, !matchRanges.InRange(2)) + assert.Assert(t, !matchRanges.InRange(3)) + assert.Assert(t, !matchRanges.InRange(4)) +}