Issue #197 : handle escaped backslash preceding terminator

This commit is contained in:
Sean Owen 2014-07-30 12:18:04 +01:00
parent 58be32036a
commit e4ff7d9bb9
2 changed files with 21 additions and 4 deletions

View file

@ -116,7 +116,7 @@ public abstract class ResultParser {
}
protected static String unescapeBackslash(String escaped) {
int backslash = escaped.indexOf((int) '\\');
int backslash = escaped.indexOf('\\');
if (backslash < 0) {
return escaped;
}
@ -208,13 +208,13 @@ public abstract class ResultParser {
int start = i; // Found the start of a match here
boolean more = true;
while (more) {
i = rawText.indexOf((int) endChar, i);
i = rawText.indexOf(endChar, i);
if (i < 0) {
// No terminating end character? uh, done. Set i such that loop terminates and break
i = rawText.length();
more = false;
} else if (rawText.charAt(i - 1) == '\\') {
// semicolon was escaped so continue
} else if (countPrecedingBackslashes(rawText, i) % 2 != 0) {
// semicolon was escaped (odd count of preceding backslashes) so continue
i++;
} else {
// found a match
@ -239,6 +239,18 @@ public abstract class ResultParser {
return matches.toArray(new String[matches.size()]);
}
private static int countPrecedingBackslashes(CharSequence s, int pos) {
int count = 0;
for (int i = pos - 1; i >= 0; i--) {
if (s.charAt(i) == '\\') {
count++;
} else {
break;
}
}
return count;
}
static String matchSinglePrefixedField(String prefix, String rawText, char endChar, boolean trim) {
String[] matches = matchPrefixedField(prefix, rawText, endChar, trim);
return matches == null ? null : matches[0];

View file

@ -64,6 +64,11 @@ public final class WifiParsedResultTestCase extends Assert {
doTest("WIFI:S:TenChars;P:hello\\:there;T:WEP;;", "TenChars", "hello:there", "WEP");
}
@Test
public void testEscape() {
doTest("WIFI:T:WPA;S:test;P:my_password\\\\;;", "test", "my_password\\", "WPA");
}
/**
* Given the string contents for the barcode, check that it matches our expectations
*/