|
6 | 6 | import graphql.language.SourceLocation; |
7 | 7 |
|
8 | 8 | import java.io.StringWriter; |
9 | | -import java.util.ArrayList; |
10 | | -import java.util.Arrays; |
11 | | -import java.util.List; |
12 | 9 |
|
13 | 10 | /** |
14 | 11 | * Contains parsing code for the StringValue types in the grammar |
@@ -44,45 +41,25 @@ public static String removeIndentation(String rawValue) { |
44 | 41 | } |
45 | 42 | } |
46 | 43 | } |
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++; |
59 | 47 | } |
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--; |
67 | 51 | } |
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); |
75 | 58 | } |
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'); |
85 | 61 | } |
| 62 | + formatted.append(line); |
86 | 63 | } |
87 | 64 | return formatted.toString(); |
88 | 65 | } |
|
0 commit comments