FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

parser cleanup by andimarek · Pull Request #4442 · graphql-java/graphql-java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .groovy  (1) .java  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
53 changes: 15 additions & 38 deletions src/main/java/graphql/parser/StringValueParsing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import graphql.language.SourceLocation;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Contains parsing code for the StringValue types in the grammar
Expand Down Expand Up @@ -44,45 +41,25 @@ public static String removeIndentation(String rawValue) {
}
}
}
List<String> lineList = new ArrayList<>(Arrays.asList(lines));
if (commonIndent != null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The old code didnt do any work if commonIndent == null - can we do the same ??

for (int i = 0; i < lineList.size(); i++) {
String line = lineList.get(i);
if (i == 0) {
continue;
}
if (line.length() > commonIndent) {
line = line.substring(commonIndent);
lineList.set(i, line);
}
}
int firstLine = 0;
while (firstLine < lines.length && containsOnlyWhiteSpace(lines[firstLine])) {
firstLine++;
}
while (!lineList.isEmpty()) {
String line = lineList.get(0);
if (containsOnlyWhiteSpace(line)) {
lineList.remove(0);
} else {
break;
}
int lastLine = lines.length;
while (lastLine > firstLine && containsOnlyWhiteSpace(lines[lastLine - 1])) {
lastLine--;
}
while (!lineList.isEmpty()) {
int endIndex = lineList.size() - 1;
String line = lineList.get(endIndex);
if (containsOnlyWhiteSpace(line)) {
lineList.remove(endIndex);
} else {
break;

StringBuilder formatted = new StringBuilder(rawValue.length());
for (int i = firstLine; i < lastLine; i++) {
String line = lines[i];
if (commonIndent != null && i > 0 && line.length() > commonIndent) {
line = line.substring(commonIndent);
}
}
StringBuilder formatted = new StringBuilder();
for (int i = 0; i < lineList.size(); i++) {
String line = lineList.get(i);
if (i == 0) {
formatted.append(line);
} else {
formatted.append("\n");
formatted.append(line);
if (i > firstLine) {
formatted.append('\n');
}
formatted.append(line);
}
return formatted.toString();
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql.parser

import graphql.i18n.I18n
import graphql.language.SourceLocation
import graphql.language.StringValue
import spock.lang.Specification

import static java.util.Arrays.asList
Expand Down Expand Up @@ -226,4 +227,26 @@ L 2
L 3'''
parsed == expected
}

def "removes large runs of leading and trailing blank lines"() {
given:
def input = "\n".repeat(20_000) + " value" + "\n ".repeat(20_000)

when:
String parsed = StringValueParsing.removeIndentation(input)

then:
parsed == "value"
}

def "full parser handles block strings with many leading and trailing blank lines"() {
given:
def input = '"""' + "\n".repeat(20_000) + " value" + "\n ".repeat(20_000) + '"""'

when:
StringValue parsed = Parser.parseValue(input)

then:
parsed.getValue() == "value"
}
}
Loading

Back | FazBrowse Home | New Git URL