From 4c4adc1e51094565d0a2fabd37975206f0d1251c Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 5 Jan 2025 20:27:43 +0530 Subject: [PATCH 1/5] Updating tests and config file --- .../practice/sgf-parsing/.meta/config.json | 3 + .../practice/sgf-parsing/.meta/tests.toml | 35 ++++++ .../src/test/java/SgfParsingTest.java | 109 +++++++++++++++++- 3 files changed, 144 insertions(+), 3 deletions(-) diff --git a/exercises/practice/sgf-parsing/.meta/config.json b/exercises/practice/sgf-parsing/.meta/config.json index aec3220c4..e5b63f0b0 100644 --- a/exercises/practice/sgf-parsing/.meta/config.json +++ b/exercises/practice/sgf-parsing/.meta/config.json @@ -2,6 +2,9 @@ "authors": [ "tlphat" ], + "contributoes": [ + "jagdish-15" + ], "files": { "solution": [ "src/main/java/SgfParsing.java" diff --git a/exercises/practice/sgf-parsing/.meta/tests.toml b/exercises/practice/sgf-parsing/.meta/tests.toml index c17626345..2a9d7d927 100644 --- a/exercises/practice/sgf-parsing/.meta/tests.toml +++ b/exercises/practice/sgf-parsing/.meta/tests.toml @@ -45,5 +45,40 @@ description = "two child trees" [724eeda6-00db-41b1-8aa9-4d5238ca0130] description = "multiple property values" +[28092c06-275f-4b9f-a6be-95663e69d4db] +description = "within property values, whitespace characters such as tab are converted to spaces" + +[deaecb9d-b6df-4658-aa92-dcd70f4d472a] +description = "within property values, newlines remain as newlines" + +[8e4c970e-42d7-440e-bfef-5d7a296868ef] +description = "escaped closing bracket within property value becomes just a closing bracket" + +[cf371fa8-ba4a-45ec-82fb-38668edcb15f] +description = "escaped backslash in property value becomes just a backslash" + +[dc13ca67-fac0-4b65-b3fe-c584d6a2c523] +description = "opening bracket within property value doesn't need to be escaped" + +[a780b97e-8dbb-474e-8f7e-4031902190e8] +description = "semicolon in property value doesn't need to be escaped" + +[0b57a79e-8d89-49e5-82b6-2eaaa6b88ed7] +description = "parentheses in property value don't need to be escaped" + +[c72a33af-9e04-4cc5-9890-1b92262813ac] +description = "escaped tab in property value is converted to space" + +[3a1023d2-7484-4498-8d73-3666bb386e81] +description = "escaped newline in property value is converted to nothing at all" + +[25abf1a4-5205-46f1-8c72-53273b94d009] +description = "escaped t and n in property value are just letters, not whitespace" + +[08e4b8ba-bb07-4431-a3d9-b1f4cdea6dab] +description = "mixing various kinds of whitespace and escaped characters in property value" +reimplements = "11c36323-93fc-495d-bb23-c88ee5844b8c" + [11c36323-93fc-495d-bb23-c88ee5844b8c] description = "escaped property" +include = false diff --git a/exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java b/exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java index d4246921b..d1b49cd59 100644 --- a/exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java +++ b/exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java @@ -121,11 +121,114 @@ public void multiplePropertyValues() throws SgfParsingException { @Test @Disabled("Remove to run test") - public void escapedProperty() throws SgfParsingException { - String input = "(;A[\\]b\nc\nd\t\te \n\\]])"; - SgfNode expected = new SgfNode(Map.of("A", List.of("]b\nc\nd\t\te \n]"))); + public void withinPropertyValueWhitespace() throws SgfParsingException { + String input = "(;A[hello\\t\\tworld])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("hello world"))); SgfNode actual = new SgfParsing().parse(input); assertThat(actual).isEqualTo(expected); } + @Test + @Disabled("Remove to run test") + public void withinPropertyValueNewline() throws SgfParsingException { + String input = "(;A[hello\n\nworld])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("hello\n\nworld"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void escapedClosingBracket() throws SgfParsingException { + String input = "(;A[\\]])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("]"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void escapedBacklash() throws SgfParsingException { + String input = "(;A[\\\\])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("\\"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void openingBracketNeedNotToBeEscaped() throws SgfParsingException { + String input = "(;A[x[y\\]z][foo]B[bar];C[baz])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("x[y]z", "foo"), + "B", List.of("bar")), + List.of( + new SgfNode(Map.of("C", List.of("baz"))) + )); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void semicolonNeedNotToBeEscaped() throws SgfParsingException { + String input = "(;A[a;b][foo]B[bar];C[baz])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("a;b", "foo"), + "B", List.of("bar")), + List.of( + new SgfNode(Map.of("C", List.of("baz"))) + )); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void paranthesesNeedNotToBeEscaped() throws SgfParsingException { + String input = "(;A[x(y)z][foo]B[bar];C[baz])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("x(y)z", "foo"), + "B", List.of("bar")), + List.of( + new SgfNode(Map.of("C", List.of("baz"))) + )); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void escapedTab() throws SgfParsingException { + String input = "(;A[hello\\\tworld])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("hello world"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + @Test + @Disabled("Remove to run test") + public void escapedNewline() throws SgfParsingException { + String input = "(;A[hello\\\nworld])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("helloworld"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + + @Test + @Disabled("Remove to run test") + public void escapedTAndN() throws SgfParsingException { + String input = "(;A[\\t = t and \\n = n])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("t = t and n = n"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } + + + @Test + @Disabled("Remove to run test") + public void mixOfEscapedCharactersAndWhitespaces() throws SgfParsingException { + String input = "(;A[\\]b\nc\\\nd\t\te\\\\ \\\n\\]])"; + SgfNode expected = new SgfNode(Map.of("A", List.of("]b\ncd e\\ ]"))); + SgfNode actual = new SgfParsing().parse(input); + assertThat(actual).isEqualTo(expected); + } } From c4f11a4c2937f97ae4a0f6bf7db57c2b608a787a Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 5 Jan 2025 21:42:18 +0530 Subject: [PATCH 2/5] Formatting config.json --- exercises/practice/sgf-parsing/.meta/config.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/exercises/practice/sgf-parsing/.meta/config.json b/exercises/practice/sgf-parsing/.meta/config.json index e5b63f0b0..aec3220c4 100644 --- a/exercises/practice/sgf-parsing/.meta/config.json +++ b/exercises/practice/sgf-parsing/.meta/config.json @@ -2,9 +2,6 @@ "authors": [ "tlphat" ], - "contributoes": [ - "jagdish-15" - ], "files": { "solution": [ "src/main/java/SgfParsing.java" From 877560a2113482db7a7737ebe959bd6c24b57d47 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Mon, 6 Jan 2025 00:09:58 +0530 Subject: [PATCH 3/5] Fixing config.json --- exercises/practice/sgf-parsing/.meta/config.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exercises/practice/sgf-parsing/.meta/config.json b/exercises/practice/sgf-parsing/.meta/config.json index aec3220c4..4a630c3c8 100644 --- a/exercises/practice/sgf-parsing/.meta/config.json +++ b/exercises/practice/sgf-parsing/.meta/config.json @@ -2,6 +2,9 @@ "authors": [ "tlphat" ], + "contributors": [ + "jagdish-15" + ], "files": { "solution": [ "src/main/java/SgfParsing.java" From 56cc2eea1847adcc4dda6e9bfa03f9127e021377 Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Tue, 28 Jan 2025 21:17:30 +0800 Subject: [PATCH 4/5] Fix sgf-parsing --- .../.meta/src/reference/java/SgfParsing.java | 77 ++++++++++++------- .../src/test/java/SgfParsingTest.java | 2 +- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/exercises/practice/sgf-parsing/.meta/src/reference/java/SgfParsing.java b/exercises/practice/sgf-parsing/.meta/src/reference/java/SgfParsing.java index 94422d3e7..6a9f4d684 100644 --- a/exercises/practice/sgf-parsing/.meta/src/reference/java/SgfParsing.java +++ b/exercises/practice/sgf-parsing/.meta/src/reference/java/SgfParsing.java @@ -29,26 +29,52 @@ private int parseFromIndex(String input, int index, SgfNode root) throws SgfPars StringBuilder buffer = new StringBuilder(); Map> properties = new HashMap<>(); String key = null; + boolean escape = false; + boolean inValue = false; while (index < input.length()) { - switch (input.charAt(index)) { - case '(': - index = addNewChild(input, index, root); - break; - case ')': - break; - case '[': - key = loadKeyFromBuffer(buffer, properties); - break; - case ']': - properties.get(key).add(popStringFromBuffer(buffer)); - if (input.charAt(index + 1) == ')') { - root.setProperties(properties); - return index + 1; - } - index = examineNextNode(input, index, root, properties); - break; - default: - index = appendCharToBuffer(input, index, buffer); + char nextChar = input.charAt(index); + if (escape) { + if (nextChar != '\n') { + appendChar(buffer, nextChar); + } + escape = false; + } else { + switch (nextChar) { + case '(': + if (inValue) { + buffer.append(nextChar); + } else { + index = addNewChild(input, index, root); + } + break; + case ')': + if (inValue) { + buffer.append(nextChar); + } + break; + case '[': + if (inValue) { + buffer.append(nextChar); + } else { + key = loadKeyFromBuffer(buffer, properties); + inValue = true; + } + break; + case ']': + properties.get(key).add(popStringFromBuffer(buffer)); + if (input.charAt(index + 1) == ')') { + root.setProperties(properties); + return index + 1; + } + index = examineNextNode(input, index, root, properties); + inValue = false; + break; + case '\\': + escape = true; + break; + default: + appendChar(buffer, nextChar); + } } ++index; } @@ -101,14 +127,13 @@ private int examineNextNode(String input, int index, SgfNode root, Map Date: Tue, 28 Jan 2025 19:29:03 +0530 Subject: [PATCH 5/5] Adding contributor --- exercises/practice/sgf-parsing/.meta/config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/practice/sgf-parsing/.meta/config.json b/exercises/practice/sgf-parsing/.meta/config.json index 4a630c3c8..a793a42b4 100644 --- a/exercises/practice/sgf-parsing/.meta/config.json +++ b/exercises/practice/sgf-parsing/.meta/config.json @@ -3,7 +3,8 @@ "tlphat" ], "contributors": [ - "jagdish-15" + "jagdish-15", + "kahgoh" ], "files": { "solution": [