Skip to content

Commit 56cc2ee

Browse files
kahgohjagdish-15
authored andcommitted
Fix sgf-parsing
1 parent 707048b commit 56cc2ee

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

exercises/practice/sgf-parsing/.meta/src/reference/java/SgfParsing.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,52 @@ private int parseFromIndex(String input, int index, SgfNode root) throws SgfPars
2929
StringBuilder buffer = new StringBuilder();
3030
Map<String, List<String>> properties = new HashMap<>();
3131
String key = null;
32+
boolean escape = false;
33+
boolean inValue = false;
3234
while (index < input.length()) {
33-
switch (input.charAt(index)) {
34-
case '(':
35-
index = addNewChild(input, index, root);
36-
break;
37-
case ')':
38-
break;
39-
case '[':
40-
key = loadKeyFromBuffer(buffer, properties);
41-
break;
42-
case ']':
43-
properties.get(key).add(popStringFromBuffer(buffer));
44-
if (input.charAt(index + 1) == ')') {
45-
root.setProperties(properties);
46-
return index + 1;
47-
}
48-
index = examineNextNode(input, index, root, properties);
49-
break;
50-
default:
51-
index = appendCharToBuffer(input, index, buffer);
35+
char nextChar = input.charAt(index);
36+
if (escape) {
37+
if (nextChar != '\n') {
38+
appendChar(buffer, nextChar);
39+
}
40+
escape = false;
41+
} else {
42+
switch (nextChar) {
43+
case '(':
44+
if (inValue) {
45+
buffer.append(nextChar);
46+
} else {
47+
index = addNewChild(input, index, root);
48+
}
49+
break;
50+
case ')':
51+
if (inValue) {
52+
buffer.append(nextChar);
53+
}
54+
break;
55+
case '[':
56+
if (inValue) {
57+
buffer.append(nextChar);
58+
} else {
59+
key = loadKeyFromBuffer(buffer, properties);
60+
inValue = true;
61+
}
62+
break;
63+
case ']':
64+
properties.get(key).add(popStringFromBuffer(buffer));
65+
if (input.charAt(index + 1) == ')') {
66+
root.setProperties(properties);
67+
return index + 1;
68+
}
69+
index = examineNextNode(input, index, root, properties);
70+
inValue = false;
71+
break;
72+
case '\\':
73+
escape = true;
74+
break;
75+
default:
76+
appendChar(buffer, nextChar);
77+
}
5278
}
5379
++index;
5480
}
@@ -101,14 +127,13 @@ private int examineNextNode(String input, int index, SgfNode root, Map<String, L
101127
}
102128
return index;
103129
}
104-
105-
private int appendCharToBuffer(String input, int index, StringBuilder buffer) {
106-
char character = input.charAt(index);
107-
while (character == '\\') {
108-
character = input.charAt(++index);
130+
131+
private void appendChar(StringBuilder builder, char charToAdd) {
132+
if (charToAdd != '\n' && Character.isWhitespace(charToAdd)) {
133+
builder.append(" ");
134+
} else {
135+
builder.append(charToAdd);
109136
}
110-
buffer.append(character);
111-
return index;
112137
}
113138

114139
private void checkIfThereAreDelimiters(StringBuilder buffer) throws SgfParsingException {

exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void multiplePropertyValues() throws SgfParsingException {
122122
@Test
123123
@Disabled("Remove to run test")
124124
public void withinPropertyValueWhitespace() throws SgfParsingException {
125-
String input = "(;A[hello\\t\\tworld])";
125+
String input = "(;A[hello\t\tworld])";
126126
SgfNode expected = new SgfNode(Map.of("A", List.of("hello world")));
127127
SgfNode actual = new SgfParsing().parse(input);
128128
assertThat(actual).isEqualTo(expected);

0 commit comments

Comments
 (0)