Extend the text format parser to allow concatenation of string literals in single quotes. Signed-off-by: David Symonds <dsymonds@golang.org>
diff --git a/proto/text_parser.go b/proto/text_parser.go index 6d0cf25..4513232 100644 --- a/proto/text_parser.go +++ b/proto/text_parser.go
@@ -119,6 +119,14 @@ return false } +func isQuote(c byte) bool { + switch c { + case '"', '\'': + return true + } + return false +} + func (p *textParser) skipWhitespace() { i := 0 for i < len(p.s) && (isWhitespace(p.s[i]) || p.s[i] == '#') { @@ -333,13 +341,13 @@ p.advance() if p.done { p.cur.value = "" - } else if len(p.cur.value) > 0 && p.cur.value[0] == '"' { + } else if len(p.cur.value) > 0 && isQuote(p.cur.value[0]) { // Look for multiple quoted strings separated by whitespace, // and concatenate them. cat := p.cur for { p.skipWhitespace() - if p.done || p.s[0] != '"' { + if p.done || !isQuote(p.s[0]) { break } p.advance()
diff --git a/proto/text_parser_test.go b/proto/text_parser_test.go index d02c61d..d2e4cca 100644 --- a/proto/text_parser_test.go +++ b/proto/text_parser_test.go
@@ -95,7 +95,7 @@ }, }, - // Quoted string concatenation + // Quoted string concatenation with double quotes { in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`, out: &MyMessage{ @@ -104,6 +104,31 @@ }, }, + // Quoted string concatenation with single quotes + { + in: "count:42 name: 'My name is '\n'elsewhere'", + out: &MyMessage{ + Count: Int32(42), + Name: String("My name is elsewhere"), + }, + }, + + // Quoted string concatenations with mixed quotes + { + in: "count:42 name: 'My name is '\n\"elsewhere\"", + out: &MyMessage{ + Count: Int32(42), + Name: String("My name is elsewhere"), + }, + }, + { + in: "count:42 name: \"My name is \"\n'elsewhere'", + out: &MyMessage{ + Count: Int32(42), + Name: String("My name is elsewhere"), + }, + }, + // Quoted string with escaped apostrophe { in: `count:42 name: "HOLIDAY - New Year\'s Day"`,