diff --git a/src/tokenizer.go b/src/tokenizer.go index c187529..bc1ca3a 100644 --- a/src/tokenizer.go +++ b/src/tokenizer.go @@ -186,7 +186,13 @@ func Transform(tokens []Token, withNth []Range) *Transformed { } } whole += part - transTokens[idx] = Token{&part, tokens[minIdx].prefixLength} + var prefixLength int + if minIdx < numTokens { + prefixLength = tokens[minIdx].prefixLength + } else { + prefixLength = 0 + } + transTokens[idx] = Token{&part, prefixLength} } return &Transformed{ whole: &whole, diff --git a/src/tokenizer_test.go b/src/tokenizer_test.go index ed77efe..1ae0c7e 100644 --- a/src/tokenizer_test.go +++ b/src/tokenizer_test.go @@ -95,3 +95,7 @@ func TestTransform(t *testing.T) { } } } + +func TestTransformIndexOutOfBounds(t *testing.T) { + Transform([]Token{}, splitNth("1")) +}