Skip to content

Instantly share code, notes, and snippets.

@hangy
Created June 7, 2012 09:57
Show Gist options
  • Save hangy/2887945 to your computer and use it in GitHub Desktop.
Save hangy/2887945 to your computer and use it in GitHub Desktop.
public class MqlKey {
public static CharSequence unescape(final CharSequence input) {
final StringBuilder output = new StringBuilder();
for (int index = 0; index < input.length(); ++index) {
final char current = input.charAt(index);
if ('$' != current) {
output.append(current);
continue;
}
if (index + 5 > input.length()) {
throw new IllegalArgumentException(
"Unicode char with less than 4 hex digits?!");
}
final CharSequence hex = input.subSequence(index + 1, index + 5);
try {
final char value = (char) Integer.parseInt(hex.toString(), 16);
output.append(value);
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException(
"Sequence cannot be parsed as unicode:" + hex, ex);
}
index += 4; // Skip past the hex value?
}
return output.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment