diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/parserutils/StringDescaper.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/parserutils/StringDescaper.java | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/base/src/main/java/bjc/utils/parserutils/StringDescaper.java b/base/src/main/java/bjc/utils/parserutils/StringDescaper.java index 8d5dda4..0895ad5 100644 --- a/base/src/main/java/bjc/utils/parserutils/StringDescaper.java +++ b/base/src/main/java/bjc/utils/parserutils/StringDescaper.java @@ -12,8 +12,7 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; /** -<<<<<<< Updated upstream - * Customizable string escapes. + * <<<<<<< Updated upstream Customizable string escapes. * * @author Benjamin Culkin */ @@ -46,7 +45,8 @@ public class StringDescaper { specialEscapes = new HashMap<>(); /* Set up the hard-coded escapes. */ - rEscapeString = String.format("\\\\(%1$s|%2$s|%3$s)", rShortEscape, rOctalEscape, rUnicodeEscape); + rEscapeString = String.format("\\\\(%1$s|%2$s|%3$s)", rShortEscape, rOctalEscape, + rUnicodeEscape); escapePatt = Pattern.compile(rEscapeString); } @@ -54,12 +54,12 @@ public class StringDescaper { * Add a new literal escape. * * @param escape - * The custom escape to add. + * The custom escape to add. * @param val - * The value for the escape. + * The value for the escape. */ public void addLiteralEscape(String escape, String val) { - if(literalEscapes.containsKey(escape)) { + if (literalEscapes.containsKey(escape)) { LOGGER.warning(String.format("Shadowing literal escape '%s'\n", escape)); } @@ -68,11 +68,11 @@ public class StringDescaper { /** * Create a new custom escape. - * + * * @param escape - * The escape to add. + * The escape to add. * @param val - * The implementation of the escape. + * The implementation of the escape. */ public void addSpecialEscape(String escape, UnaryOperator<String> val) { /* @@ -82,7 +82,7 @@ public class StringDescaper { Pattern patt = null; try { patt = Pattern.compile(escape); - } catch(PatternSyntaxException psex) { + } catch (PatternSyntaxException psex) { String msg = String.format("Invalid special escape '%s'", escape); IllegalArgumentException iaex = new IllegalArgumentException(msg); @@ -91,7 +91,7 @@ public class StringDescaper { throw psex; } - if(specialEscapes.containsKey(patt)) { + if (specialEscapes.containsKey(patt)) { LOGGER.warning(String.format("Shadowing special escape '%s'\n", escape)); } @@ -104,24 +104,24 @@ public class StringDescaper { public void compileEscapes() { StringBuilder work = new StringBuilder(); - for(String litEscape : literalEscapes.keySet()) { + for (String litEscape : literalEscapes.keySet()) { work.append("|(?:"); work.append(Pattern.quote(litEscape)); work.append(")"); } - for(Pattern specEscape : specialEscapes.keySet()) { + for (Pattern specEscape : specialEscapes.keySet()) { work.append("|(?:"); work.append(specEscape.toString()); work.append(")"); } /* - * Convert user-defined escapes to a regex for matching. We - * don't need a bar before %4 because the string has it. + * Convert user-defined escapes to a regex for matching. We don't need a bar + * before %4 because the string has it. */ - rEscapeString = String.format("\\(%1$s|%2$s|%3$s%4$s)", rShortEscape, rOctalEscape, rUnicodeEscape, - work.toString()); + rEscapeString = String.format("\\(%1$s|%2$s|%3$s%4$s)", rShortEscape, + rOctalEscape, rUnicodeEscape, work.toString()); escapePatt = Pattern.compile(rEscapeString); } @@ -129,13 +129,13 @@ public class StringDescaper { * Replace escape characters with their actual equivalents. * * @param inp - * The string to replace escape sequences in. + * The string to replace escape sequences in. * * @return The string with escape sequences replaced by their equivalent * characters. */ public String descapeString(final String inp) { - if(inp == null) { + if (inp == null) { throw new NullPointerException("Input to descapeString must not be null"); } @@ -147,11 +147,10 @@ public class StringDescaper { final Matcher escapeFinder = escapePatt.matcher(inp); /* Go through each escape. */ - while(possibleEscapeFinder.find()) { - if(!escapeFinder.find()) { + while (possibleEscapeFinder.find()) { + if (!escapeFinder.find()) { /* - * Found a possible escape that isn't actually - * an escape. + * Found a possible escape that isn't actually an escape. */ final String msg = String.format( "Illegal escape sequence '%s' at position %d of string '%s'", @@ -165,7 +164,7 @@ public class StringDescaper { * Convert the escape to a string. */ String escapeRep = ""; - switch(escapeSeq) { + switch (escapeSeq) { case "\\b": escapeRep = "\b"; break; @@ -195,22 +194,23 @@ public class StringDescaper { escapeRep = "\\"; break; default: - if(escapeSeq.startsWith("u")) { + if (escapeSeq.startsWith("u")) { /* Handle a unicode escape. */ escapeRep = handleUnicodeEscape(escapeSeq.substring(1)); - } else if(escapeSeq.startsWith("O")) { + } else if (escapeSeq.startsWith("O")) { /* Handle an octal escape. */ escapeRep = handleOctalEscape(escapeSeq.substring(1)); - } else if(literalEscapes.containsKey(escapeSeq)) { + } else if (literalEscapes.containsKey(escapeSeq)) { /* Handle a custom literal escape. */ escapeRep = literalEscapes.get(escapeSeq); } else { /* Handle a custom special escape. */ - for(Entry<Pattern, UnaryOperator<String>> ent : specialEscapes.entrySet()) { + for (Entry<Pattern, UnaryOperator<String>> ent : specialEscapes + .entrySet()) { Pattern pat = ent.getKey(); Matcher mat = pat.matcher(escapeSeq); - if(mat.matches()) { + if (mat.matches()) { escapeRep = ent.getValue().apply(escapeSeq); break; } @@ -234,8 +234,9 @@ public class StringDescaper { final int codepoint = Integer.parseInt(seq, 16); return new String(Character.toChars(codepoint)); - } catch(final IllegalArgumentException iaex) { - final String msg = String.format("'%s' is not a valid Unicode escape sequence'", seq); + } catch (final IllegalArgumentException iaex) { + final String msg + = String.format("'%s' is not a valid Unicode escape sequence'", seq); final IllegalArgumentException reiaex = new IllegalArgumentException(msg); @@ -252,7 +253,7 @@ public class StringDescaper { try { final int codepoint = Integer.parseInt(seq, 8); - if(codepoint > 255) { + if (codepoint > 255) { final String msg = String .format("'%d' is outside the range of octal escapes', codepoint"); @@ -260,8 +261,9 @@ public class StringDescaper { } return new String(Character.toChars(codepoint)); - } catch(final IllegalArgumentException iaex) { - final String msg = String.format("'%s' is not a valid octal escape sequence'", seq); + } catch (final IllegalArgumentException iaex) { + final String msg + = String.format("'%s' is not a valid octal escape sequence'", seq); final IllegalArgumentException reiaex = new IllegalArgumentException(msg); |
