From ee2ee025993421b243ef668e4d4ee395a5201820 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 5 Jan 2015 19:32:44 +0900 Subject: [PATCH] Fix index out of bounds error during Transform --- src/tokenizer.go | 8 +++++++- src/tokenizer_test.go | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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")) +}