Decodes backslash-escaped characters from the String until the character 'delim' is found. With 'delim'=-1, does not search for a delimiter, with 'delim'=(char)-2, (unescaped) whitespace, commas and semicolons are interpreted as delimiters. Also decodes unicode backslash-u???? characters
(String str, char delim)
| 447 | * whitespace, commas and semicolons are interpreted as delimiters. |
| 448 | * Also decodes unicode backslash-u???? characters */ |
| 449 | public static String decodeEscaped(String str, char delim) { |
| 450 | StringBuilder sb = new StringBuilder(); |
| 451 | for (int i=0; i<str.length(); i++) { |
| 452 | char c = str.charAt(i); |
| 453 | if ((delim == (char)-2 && isDelimiter(c)) || c == delim) break; |
| 454 | if (c == '\\' && i+1 < str.length()) { //escaped by backslash |
| 455 | i++; |
| 456 | c = str.charAt(i); |
| 457 | if (c == 'u' && i+4 < str.length()) |
| 458 | try { |
| 459 | c = (char)Integer.parseInt(str.substring(i+1, i+5), 16); |
| 460 | i += 4; |
| 461 | } catch (NumberFormatException e) {} |
| 462 | else |
| 463 | c = withBackslash(c); // decodes backslash-t for tab etc. |
| 464 | } |
| 465 | sb.append(c); |
| 466 | } |
| 467 | return sb.toString(); |
| 468 | } |
| 469 | |
| 470 | private static boolean isDelimiter(char c) { |
| 471 | return Character.isWhitespace(c) || c==',' || c==';'; |
no test coverage detected