Skip to content

Commit

Permalink
Merge pull request square#556 from square/jw/concat
Browse files Browse the repository at this point in the history
Concat adjacent strings.
  • Loading branch information
JakeWharton committed Jan 27, 2016
2 parents 67edd67 + c20fade commit 10f0fdd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,14 @@ private String readQuotedString() {
StringBuilder result = new StringBuilder();
while (pos < data.length) {
char c = data[pos++];
if (c == startQuote) return result.toString();
if (c == startQuote) {
if (peekChar() == '"' || peekChar() == '\'') {
// Adjacent strings are concatenated. Consume new quote and continue reading.
startQuote = readChar();
continue;
}
return result.toString();
}

if (c == '\\') {
if (pos == data.length) throw unexpected("unexpected end of file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,33 @@ public final class ProtoParserTest {
assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected);
}

@Test public void adjacentStringsConcatenated() {
String proto = ""
+ "message Foo {\n"
+ " optional string name = 1 [\n"
+ " default = \"concat \"\n"
+ " 'these '\n"
+ " \"please\"\n"
+ " ];\n"
+ "}";

FieldElement field = FieldElement.builder(location.at(2, 3))
.label(OPTIONAL)
.type("string")
.name("name")
.tag(1)
.defaultValue("concat these please")
.build();
TypeElement messageElement = MessageElement.builder(location.at(1, 1))
.name("Foo")
.fields(ImmutableList.of(field))
.build();
ProtoFileElement expected = ProtoFileElement.builder(location)
.types(ImmutableList.of(messageElement))
.build();
assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected);
}

@Test public void invalidHexStringEscape() throws Exception {
String proto = ""
+ "message Foo {\n"
Expand Down

0 comments on commit 10f0fdd

Please sign in to comment.