don't escape already escaped additional characters when escapeSlash is false

This commit is contained in:
Bas Leijdekkers
2012-01-25 14:44:25 +01:00
parent aa766d80fb
commit 305bc31b47
2 changed files with 9 additions and 1 deletions
@@ -570,6 +570,7 @@ public class StringUtil {
@Nullable String additionalChars,
boolean escapeSlash,
@NotNull @NonNls StringBuilder buffer) {
char prev = 0;
for (int idx = 0; idx < length; idx++) {
char ch = str.charAt(idx);
switch (ch) {
@@ -597,7 +598,7 @@ public class StringUtil {
if (escapeSlash && ch == '\\') {
buffer.append("\\\\");
}
else if (additionalChars != null && additionalChars.indexOf(ch) > -1) {
else if (additionalChars != null && additionalChars.indexOf(ch) > -1 && (escapeSlash || prev != '\\')) {
buffer.append("\\").append(ch);
}
else if (Character.isISOControl(ch)) {
@@ -613,6 +614,7 @@ public class StringUtil {
buffer.append(ch);
}
}
prev = ch;
}
return buffer;
}
@@ -111,4 +111,10 @@ public class StringUtilTest extends TestCase {
public void testTitleCase() {
assertEquals("Couldn't Connect to Debugger", StringUtil.wordsToBeginFromUpperCase("Couldn't connect to debugger"));
}
public void testEscapeStringCharacters() {
assertEquals("\\\"\\n", StringUtil.escapeStringCharacters(3, "\\\"\n", "\"", false, new StringBuilder()).toString());
assertEquals("\\\"\\n", StringUtil.escapeStringCharacters(2, "\"\n", "\"", false, new StringBuilder()).toString());
assertEquals("\\\\\\\"\\n", StringUtil.escapeStringCharacters(3, "\\\"\n", "\"", true, new StringBuilder()).toString());
}
}