go-parsekit/parsehandler_stringbuf.go

48 lines
1.8 KiB
Go

package parsekit
// BufLiteral retrieves the contents of the parser's string buffer (all the
// runes that were added to it using ParseAPI.Accept()) as a literal string.
//
// Literal means that if the input had for example the subsequent runes '\' and
// 'n' in it, then the literal string would have a backslash and an 'n' it in,
// not a linefeed (ASCII char 10).
//
// Retrieving the buffer contents will not affect the buffer itself. New runes
// can still be added to it. Only when calling P.BufClear(), the buffer will be
// cleared.
func (p *ParseAPI) BufLiteral() string {
return p.buffer.asLiteralString()
}
// BufInterpreted retrieves the contents of the parser's string buffer (all the
// runes that were added to it using ParseAPI.Accept()) as an interpreted
// string.
//
// Interpreted means that the contents are treated as a Go double quoted
// interpreted string (handling escape codes like \n, \t, \uXXXX, etc.). if the
// input had for example the subsequent runes '\' and 'n' in it, then the
// interpreted string would have an actual linefeed (ASCII char 10) in it.
//
// This method returns a boolean value, indicating whether or not the string
// interpretation was successful. On invalid string data, an error will
// automatically be emitted and the boolean return value will be false.
//
// Retrieving the buffer contents will not affect the buffer itself. New runes
// can still be added to it. Only when calling P.BufClear(), the buffer will be
// cleared.
func (p *ParseAPI) BufInterpreted() (string, bool) {
s, err := p.buffer.asInterpretedString()
if err != nil {
p.Error(
"invalid string: %s (%s, forgot to escape a double quote or backslash maybe?)",
p.buffer.asLiteralString(), err)
return "", false
}
return s, true
}
// BufClear clears the contents of the parser's string buffer.
func (p *ParseAPI) BufClear() {
p.buffer.reset()
}