Skip to content

Commit ff464f4

Browse files
authored
Merge pull request #4442 from graphql-java/parser-cleanup
parser cleanup
2 parents 963aada + 5b07d3c commit ff464f4

2 files changed

Lines changed: 38 additions & 38 deletions

File tree

src/main/java/graphql/parser/StringValueParsing.java

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import graphql.language.SourceLocation;
77

88
import java.io.StringWriter;
9-
import java.util.ArrayList;
10-
import java.util.Arrays;
11-
import java.util.List;
129

1310
/**
1411
* Contains parsing code for the StringValue types in the grammar
@@ -44,45 +41,25 @@ public static String removeIndentation(String rawValue) {
4441
}
4542
}
4643
}
47-
List<String> lineList = new ArrayList<>(Arrays.asList(lines));
48-
if (commonIndent != null) {
49-
for (int i = 0; i < lineList.size(); i++) {
50-
String line = lineList.get(i);
51-
if (i == 0) {
52-
continue;
53-
}
54-
if (line.length() > commonIndent) {
55-
line = line.substring(commonIndent);
56-
lineList.set(i, line);
57-
}
58-
}
44+
int firstLine = 0;
45+
while (firstLine < lines.length && containsOnlyWhiteSpace(lines[firstLine])) {
46+
firstLine++;
5947
}
60-
while (!lineList.isEmpty()) {
61-
String line = lineList.get(0);
62-
if (containsOnlyWhiteSpace(line)) {
63-
lineList.remove(0);
64-
} else {
65-
break;
66-
}
48+
int lastLine = lines.length;
49+
while (lastLine > firstLine && containsOnlyWhiteSpace(lines[lastLine - 1])) {
50+
lastLine--;
6751
}
68-
while (!lineList.isEmpty()) {
69-
int endIndex = lineList.size() - 1;
70-
String line = lineList.get(endIndex);
71-
if (containsOnlyWhiteSpace(line)) {
72-
lineList.remove(endIndex);
73-
} else {
74-
break;
52+
53+
StringBuilder formatted = new StringBuilder(rawValue.length());
54+
for (int i = firstLine; i < lastLine; i++) {
55+
String line = lines[i];
56+
if (commonIndent != null && i > 0 && line.length() > commonIndent) {
57+
line = line.substring(commonIndent);
7558
}
76-
}
77-
StringBuilder formatted = new StringBuilder();
78-
for (int i = 0; i < lineList.size(); i++) {
79-
String line = lineList.get(i);
80-
if (i == 0) {
81-
formatted.append(line);
82-
} else {
83-
formatted.append("\n");
84-
formatted.append(line);
59+
if (i > firstLine) {
60+
formatted.append('\n');
8561
}
62+
formatted.append(line);
8663
}
8764
return formatted.toString();
8865
}

src/test/groovy/graphql/parser/StringValueParsingTest.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphql.parser
22

33
import graphql.i18n.I18n
44
import graphql.language.SourceLocation
5+
import graphql.language.StringValue
56
import spock.lang.Specification
67

78
import static java.util.Arrays.asList
@@ -226,4 +227,26 @@ L 2
226227
L 3'''
227228
parsed == expected
228229
}
230+
231+
def "removes large runs of leading and trailing blank lines"() {
232+
given:
233+
def input = "\n".repeat(20_000) + " value" + "\n ".repeat(20_000)
234+
235+
when:
236+
String parsed = StringValueParsing.removeIndentation(input)
237+
238+
then:
239+
parsed == "value"
240+
}
241+
242+
def "full parser handles block strings with many leading and trailing blank lines"() {
243+
given:
244+
def input = '"""' + "\n".repeat(20_000) + " value" + "\n ".repeat(20_000) + '"""'
245+
246+
when:
247+
StringValue parsed = Parser.parseValue(input)
248+
249+
then:
250+
parsed.getValue() == "value"
251+
}
229252
}

0 commit comments

Comments
 (0)