Skip to content

Commit

Permalink
Add unicode support to unescapeBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohnstn committed Mar 15, 2024
1 parent 2ccf2ae commit 18cdfa5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,22 @@ private static List<String> unescapeBlock(String escapedText) {
List<String> parts= new ArrayList<>();

while ((bsIndex= escapedText.indexOf("\\", readIndex)) >= 0) { //$NON-NLS-1$ "\"
if (escapedText.startsWith("\\n", bsIndex)) { //$NON-NLS-1$ "\n"
if (escapedText.startsWith("\\n", bsIndex) || escapedText.startsWith("\\u005cn", bsIndex)) { //$NON-NLS-1$ //$NON-NLS-2$ "\n"
transformed.append(escapedText.substring(readIndex, bsIndex));
parts.add(escapeTrailingWhitespace(transformed.toString())+ System.lineSeparator());
transformed= new StringBuilder();
readIndex= bsIndex + 2;
} else if (escapedText.startsWith("\\\"", bsIndex)) { //$NON-NLS-1$ "\""
readIndex= bsIndex + (escapedText.startsWith("\\n", bsIndex) ? 2 : 7); //$NON-NLS-1$
} else if (escapedText.startsWith("\\\"", bsIndex) || escapedText.startsWith("\\u005c\"", bsIndex)) { //$NON-NLS-1$ //$NON-NLS-2$ "\""
// if there are more than three quotes in a row, escape the first quote of every triplet to
// avoid it being interpreted as a text block terminator. This code would be much simpler if
// we could escape the third quote of each triplet, but the text block spec recommends this way.

transformed.append(escapedText.substring(readIndex, bsIndex));
int quoteCount= 1;
while (escapedText.startsWith("\\\"", bsIndex + 2 * quoteCount)) { //$NON-NLS-1$
int index= (escapedText.startsWith("\\\"", bsIndex) ? 2 : 7); //$NON-NLS-1$
while (escapedText.startsWith("\\\"", bsIndex + index) || escapedText.startsWith("\\u005c\"", bsIndex + index)) { //$NON-NLS-1$ //$NON-NLS-2$
quoteCount++;
index += (escapedText.startsWith("\\\"", bsIndex + index) ? 2 : 7); //$NON-NLS-1$
}
int i= 0;
while (i < quoteCount / 3) {
Expand All @@ -394,11 +396,11 @@ private static List<String> unescapeBlock(String escapedText) {
for (int j = 0; j < quoteCount % 3; j++) {
transformed.append("\""); //$NON-NLS-1$
}
readIndex= bsIndex + 2 * quoteCount;
} else if (escapedText.startsWith("\\t", bsIndex)) { //$NON-NLS-1$ "\t"
readIndex= bsIndex + index;
} else if (escapedText.startsWith("\\t", bsIndex) || escapedText.startsWith("\\u005ct", bsIndex)) { //$NON-NLS-1$ //$NON-NLS-2$ "\t"
transformed.append(escapedText.substring(readIndex, bsIndex));
transformed.append("\t"); //$NON-NLS-1$
readIndex= bsIndex+2;
readIndex= bsIndex + (escapedText.startsWith("\\t", bsIndex) ? 2 : 7); //$NON-NLS-1$
} else {
transformed.append(escapedText.substring(readIndex, bsIndex));
transformed.append("\\").append(escapedText.charAt(bsIndex + 1)); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public void testConcatToTextBlock() throws Exception {
String sample= "" //
+ "package test1;\n" //
+ "\n" //
+ "/**\n" //
+ " * Performs:\u005cn" //
+ " * <pre>{@code\n" //
+ " * for (String s : strings) {\u005cn" //
+ " * if (s.equals(value)) {\n" //
+ " * return \\u0030;\n" //
+ " * }\n" //
+ " * if (s.startsWith(value)) {\n" //
+ " * return 1;\n" //
+ " * }\n" //
+ " * }\n" //
+ " * return -1;\n" //
+ " * }</pre>\n" //
+ " */\n" //
+ "public class E {\n" //
+ " static String str = \"\" + //$NON-NLS-1$\n" //
+ " \"public class B { \\n\" + //$NON-NLS-1$\n" //
Expand Down Expand Up @@ -144,6 +158,20 @@ public void testConcatToTextBlock() throws Exception {
String expected1= "" //
+ "package test1;\n" //
+ "\n" //
+ "/**\n" //
+ " * Performs:\n" //
+ " * <pre>{@code\n" //
+ " * for (String s : strings) {\n" //
+ " * if (s.equals(value)) {\n" //
+ " * return \\u0030;\n" //
+ " * }\n" //
+ " * if (s.startsWith(value)) {\n" //
+ " * return 1;\n" //
+ " * }\n" //
+ " * }\n" //
+ " * return -1;\n" //
+ " * }</pre>\n" //
+ " */\n" //
+ "public class E {\n" //
+ " static String str = \"\"\"\n" //
+ " public class B {\\s\n" //
Expand Down

0 comments on commit 18cdfa5

Please sign in to comment.