Created
June 7, 2012 09:57
-
-
Save hangy/2887945 to your computer and use it in GitHub Desktop.
Unescape MQL keys (wiki.freebase.com/wiki/MQL_key_escaping) - adapted from https://github.com/apache/commons-lang/blob/trunk/src/main/java/org/apache/commons/lang3/text/translate/UnicodeUnescaper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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