Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StringConcatToTextBlockFixCore escape logic when string is size 1 #1263

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -424,15 +426,12 @@ private static String escapeTrailingWhitespace(String unescaped) {
}
int whitespaceStart= unescaped.length()-1;
StringBuilder trailingWhitespace= new StringBuilder();
while (whitespaceStart > 0) {
if (unescaped.charAt(whitespaceStart) == ' ') {
whitespaceStart--;
trailingWhitespace.append("\\s"); //$NON-NLS-1$
} else if (unescaped.charAt(whitespaceStart) == '\t') {
whitespaceStart--;
trailingWhitespace.append("\\t"); //$NON-NLS-1$
}
break;
if (unescaped.charAt(whitespaceStart) == ' ') {
--whitespaceStart;
trailingWhitespace.append("\\s"); //$NON-NLS-1$
} else if (unescaped.charAt(whitespaceStart) == '\t') {
--whitespaceStart;
trailingWhitespace.append("\\t"); //$NON-NLS-1$
}

return unescaped.substring(0, whitespaceStart + 1) + trailingWhitespace;
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 All @@ -62,6 +76,11 @@ public void testConcatToTextBlock() throws Exception {
+ " \" }\\n\" + //$NON-NLS-1$\n" //
+ " \"}\"; //$NON-NLS-1$\n" //
+ "\n" //
+ " private static final String CU_POSTFIX= \" {\\n\" +\n" //
+ " \" \\n\" +\n" //
+ " \"}\\n\" +\n" //
+ " \"}\\n\";\n" //
+ "\n" //
+ " public void testSimple() {\n" //
+ " // comment 1\n" //
+ " String x = \"\" + //$NON-NLS-1$\n" //
Expand Down Expand Up @@ -139,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 All @@ -148,6 +181,13 @@ public void testConcatToTextBlock() throws Exception {
+ " }\n" //
+ " }\"\"\"; //$NON-NLS-1$\n" //
+ "\n" //
+ " private static final String CU_POSTFIX= \"\"\"\n" //
+ " {\n" //
+ " \\t\n" //
+ " }\n" //
+ " }\n" //
+ " \"\"\";\n" //
+ "\n" //
+ " public void testSimple() {\n" //
+ " // comment 1\n" //
+ " String x = \"\"\"\n" //
Expand Down
Loading