Skip to content

Commit

Permalink
Merge pull request #3311 from maxonfjvipon/fix/#3251/remove-unescapin…
Browse files Browse the repository at this point in the history
…g-in-runtime

fix(#3251): remove unescaping in eo-runtime
  • Loading branch information
yegor256 authored Aug 2, 2024
2 parents f31a4dd + e3a2777 commit f258c11
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public TjSmart foreign() {
* @return Workspace after executing Mojo.
* @throws java.io.IOException If some problem with filesystem has happened.
* @checkstyle ExecutableStatementCountCheck (100 lines)
* @checkstyle JavaNCSSCheck (100 lines)
*/
public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IOException {
if (this.defaults) {
Expand Down
111 changes: 1 addition & 110 deletions eo-runtime/src/main/java/org/eolang/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ private static Phi toPhi(final Object obj) {
bytes = new BytesOf(((Number) obj).doubleValue()).take();
} else if (obj instanceof String) {
phi = eolang.take("string").copy();
bytes = Data.ToPhi.unescapeJavaString(
(String) obj
).getBytes(StandardCharsets.UTF_8);
bytes = ((String) obj).getBytes(StandardCharsets.UTF_8);
} else {
throw new IllegalArgumentException(
String.format(
Expand All @@ -190,112 +188,5 @@ private static Phi toPhi(final Object obj) {
}
return phi;
}

/**
* Unescapes a string that contains standard Java escape sequences.
* <ul>
* <li><strong>&#92;b &#92;f &#92;n &#92;r &#92;t &#92;" &#92;'</strong> :
* BS, FF, NL, CR, TAB, double and single quote.</li>
* <li><strong>&#92;X &#92;XX &#92;XXX</strong> : Octal character
* specification (0 - 377, 0x00 - 0xFF).</li>
* <li><strong>&#92;uXXXX</strong> : Hexadecimal based Unicode character.</li>
* </ul>
* @param str A string optionally containing standard java escape sequences.
* @return The translated string
* @todo #3160:90min This method should be refactored because it has high cognitive
* complexity and other problems. All {@code @checkstyle} warnings suppression and
* {@code SuppressWarnings("PMD.WarningName")} annotations for this method should be
* removed as a result of refactoring.
* @checkstyle CyclomaticComplexityCheck (100 lines)
* @checkstyle JavaNCSSCheck (100 lines)
* @checkstyle NestedIfDepthCheck (100 lines)
* @checkstyle ModifiedControlVariableCheck (100 lines)
*/
@SuppressWarnings({
"PMD.AvoidReassigningLoopVariables",
"PMD.CognitiveComplexity",
"PMD.NPathComplexity"
})
private static String unescapeJavaString(final String str) {
final StringBuilder unescaped = new StringBuilder(str.length());
for (int idx = 0; idx < str.length(); ++idx) {
char chr = str.charAt(idx);
if (chr == '\\') {
final char next;
if (idx == str.length() - 1) {
next = '\\';
} else {
next = str.charAt(idx + 1);
}
if (next >= '0' && next <= '7') {
final StringBuilder code = new StringBuilder(String.valueOf(next));
++idx;
if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0'
&& str.charAt(idx + 1) <= '7') {
code.append(str.charAt(idx + 1));
++idx;
if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0'
&& str.charAt(idx + 1) <= '7') {
code.append(str.charAt(idx + 1));
++idx;
}
}
unescaped.append((char) Integer.parseInt(code.toString(), 8));
continue;
}
switch (next) {
case '\\':
break;
case 'b':
chr = '\b';
break;
case 'f':
chr = '\f';
break;
case 'n':
chr = '\n';
break;
case 'r':
chr = '\r';
break;
case 't':
chr = '\t';
break;
case '\"':
chr = '\"';
break;
case '\'':
chr = '\'';
break;
case 'u':
if (idx >= str.length() - 5) {
chr = 'u';
break;
}
unescaped.append(
Character.toChars(
Integer.parseInt(
String.join(
"",
String.valueOf(str.charAt(idx + 2)),
String.valueOf(str.charAt(idx + 3)),
String.valueOf(str.charAt(idx + 4)),
String.valueOf(str.charAt(idx + 5))
),
16
)
)
);
idx += 5;
continue;
default:
break;
}
++idx;
}
unescaped.append(chr);
}
return unescaped.toString();
}
}
}
12 changes: 12 additions & 0 deletions eo-runtime/src/test/eo/org/eolang/runtime-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@
^.func m > @
0

# Test.
[] > unescapes-slashes
eq. > @
"x\\b\\f\\u\\r\\t\\n\\'"
78-5C-62-5C-66-5C-75-5C-72-5C-74-5C-6E-5C-27

# Test.
[] > unescapes-symbols
eq. > @
"\b\f\n\r\t\u27E6"
08-0C-0A-0D-09-E2-9F-A6

# Test.
[] > compiles-correctly-with-long-duplicate-names
[] > long-object-name
Expand Down

1 comment on commit f258c11

@0pdd
Copy link

@0pdd 0pdd commented on f258c11 Aug 2, 2024

Choose a reason for hiding this comment

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

Puzzle 3160-edcd4ffd disappeared from eo-runtime/src/main/java/org/eolang/Data.java), that's why I closed #3240. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.