Fix index out of bounds error during Transform

This commit is contained in:
Junegunn Choi 2015-01-05 19:32:44 +09:00
parent b42dcdb7a7
commit ee2ee02599
2 changed files with 11 additions and 1 deletions

View File

@ -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,

View File

@ -95,3 +95,7 @@ func TestTransform(t *testing.T) {
}
}
}
func TestTransformIndexOutOfBounds(t *testing.T) {
Transform([]Token{}, splitNth("1"))
}