Skip to content

Commit

Permalink
Avoid panic by not exceeding bounds of string..
Browse files Browse the repository at this point in the history
Signed-off-by: James Phillips <jamesdphillips@gmail.com>
  • Loading branch information
jamesdphillips committed Dec 12, 2017
1 parent 0629778 commit e05986a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
37 changes: 20 additions & 17 deletions language/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,23 @@ func blockStringValue(in string) string {
// Expand a block string's raw value into independent lines.
lines := splitLinesRegex.Split(in, -1)

// Remove leading blank lines.
for {
if isBlank := lineIsBlank(lines[0]); !isBlank {
break
}
lines = lines[1:]
}

// Remove trailing blank lines.
for {
i := len(lines) - 1
if isBlank := lineIsBlank(lines[i]); !isBlank {
break
}
lines = append(lines[:i], lines[i+1:]...)
}

// Remove common indentation from all lines but first
commonIndent := -1
for i := 1; i < len(lines); i++ {
Expand All @@ -395,27 +412,13 @@ func blockStringValue(in string) string {
}
if commonIndent > 0 {
for i, line := range lines {
if commonIndent > len(line) {
continue
}
lines[i] = line[commonIndent:]
}
}

// Remove leading blank lines.
for {
if isBlank := lineIsBlank(lines[0]); !isBlank {
break
}
lines = lines[1:]
}

// Remove trailing blank lines.
for {
i := len(lines) - 1
if isBlank := lineIsBlank(lines[i]); !isBlank {
break
}
lines = append(lines[:i], lines[i+1:]...)
}

// Return a string of the lines joined with U+000A.
return strings.Join(lines, "\n")
}
Expand Down
16 changes: 16 additions & 0 deletions language/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ func TestLexer_LexesBlockStrings(t *testing.T) {
Value: " white space ",
},
},
{
Body: `
"""
my great description
spans multiple lines
with breaks
"""
`,
Expected: Token{
Kind: TokenKind[BLOCK_STRING],
Start: 5,
End: 89,
Value: "my great description\nspans multiple lines\n\nwith breaks",
},
},
{
Body: `"""contains " quote"""`,
Expected: Token{
Expand Down

0 comments on commit e05986a

Please # to comment.