Skip to content

Commit

Permalink
Fix parsing python multiline string
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Sep 6, 2021
1 parent b52868c commit f79afbf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
4 changes: 2 additions & 2 deletions ini_python_multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func TestMultiline(t *testing.T) {
var testData testData
err = defaultSection.MapTo(&testData)
So(err, ShouldBeNil)
So(testData.Value1, ShouldEqual, "some text here\nsome more text here\n\nthere is an empty line above and below\n")
So(testData.Value2, ShouldEqual, "there is an empty line above\nthat is not indented so it should not be part\nof the value")
So(testData.Value1, ShouldEqual, "some text here\n\tsome more text here\n\t\n\tthere is an empty line above and below\n\t")
So(testData.Value2, ShouldEqual, "there is an empty line above\n that is not indented so it should not be part\n of the value")
So(testData.Value3, ShouldEqual, `.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu consequat ac felis donec et odio pellentesque diam volutpat. Mauris commodo quis imperdiet massa tincidunt nunc. Interdum velit euismod in pellentesque. Nisl condimentum id venenatis a condimentum vitae sapien pellentesque. Nascetur ridiculus mus mauris vitae. Posuere urna nec tincidunt praesent semper feugiat. Lorem donec massa sapien faucibus et molestie ac feugiat sed. Ipsum dolor sit amet consectetur adipiscing elit. Enim sed faucibus turpis in eu mi. A diam sollicitudin tempor id. Quam nulla porttitor massa id neque aliquam vestibulum morbi blandit.
Expand Down
17 changes: 1 addition & 16 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ func (p *parser) readPythonMultilines(line string, bufferSize int) (string, erro
parserBufferPeekResult, _ := p.buf.Peek(bufferSize)
peekBuffer := bytes.NewBuffer(parserBufferPeekResult)

indentSize := 0
for {
peekData, peekErr := peekBuffer.ReadBytes('\n')
if peekErr != nil {
Expand All @@ -329,28 +328,14 @@ func (p *parser) readPythonMultilines(line string, bufferSize int) (string, erro
return line, nil
}

// Determine indent size and line prefix.
currentIndentSize := len(peekMatches[1])
if indentSize < 1 {
indentSize = currentIndentSize
p.debug("readPythonMultilines: indent size is %d", indentSize)
}

// Make sure each line is indented at least as far as first line.
if currentIndentSize < indentSize {
p.debug("readPythonMultilines: end of value, current indent: %d, expected indent: %d, line: %q", currentIndentSize, indentSize, line)
return line, nil
}

// Advance the parser reader (buffer) in-sync with the peek buffer.
_, err := p.buf.Discard(len(peekData))
if err != nil {
p.debug("readPythonMultilines: failed to skip to the end, returning error")
return "", err
}

// Handle indented empty line.
line += "\n" + peekMatches[1][indentSize:] + peekMatches[2]
line += "\n" + peekMatches[0]
}
}

Expand Down

0 comments on commit f79afbf

Please sign in to comment.