Created
September 28, 2013 12:50
-
-
Save uklimaschewski/6741769 to your computer and use it in GitHub Desktop.
Unescapes a string that contains standard Java escape sequences: - \b \f \n \r \t \" \' : BS, FF, NL, CR, TAB, single and double quote
- \X \XX \XXX : Octal character from 0 to 377 (that is in Hex: 0x00 to 0xFF)
- \uXXXX : Hexadecimal based Unicode character
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
| /** | |
| * Unescapes a string that contains standard Java escape sequences. | |
| * <ul> | |
| * <li><strong>\b \f \n \r \t \" \'</strong> : | |
| * BS, FF, NL, CR, TAB, double and single quote.</li> | |
| * <li><strong>\X \XX \XXX</strong> : Octal character | |
| * specification (0 - 377, 0x00 - 0xFF).</li> | |
| * <li><strong>\uXXXX</strong> : Hexadecimal based Unicode character.</li> | |
| * </ul> | |
| * | |
| * @param st | |
| * A string optionally containing standard java escape sequences. | |
| * @return The translated string. | |
| */ | |
| public String unescapeJavaString(String st) { | |
| StringBuilder sb = new StringBuilder(st.length()); | |
| for (int i = 0; i < st.length(); i++) { | |
| char ch = st.charAt(i); | |
| if (ch == '\\') { | |
| char nextChar = (i == st.length() - 1) ? '\\' : st | |
| .charAt(i + 1); | |
| // Octal escape? | |
| if (nextChar >= '0' && nextChar <= '7') { | |
| String code = "" + nextChar; | |
| i++; | |
| if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' | |
| && st.charAt(i + 1) <= '7') { | |
| code += st.charAt(i + 1); | |
| i++; | |
| if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' | |
| && st.charAt(i + 1) <= '7') { | |
| code += st.charAt(i + 1); | |
| i++; | |
| } | |
| } | |
| sb.append((char) Integer.parseInt(code, 8)); | |
| continue; | |
| } | |
| switch (nextChar) { | |
| case '\\': | |
| ch = '\\'; | |
| break; | |
| case 'b': | |
| ch = '\b'; | |
| break; | |
| case 'f': | |
| ch = '\f'; | |
| break; | |
| case 'n': | |
| ch = '\n'; | |
| break; | |
| case 'r': | |
| ch = '\r'; | |
| break; | |
| case 't': | |
| ch = '\t'; | |
| break; | |
| case '\"': | |
| ch = '\"'; | |
| break; | |
| case '\'': | |
| ch = '\''; | |
| break; | |
| // Hex Unicode: u???? | |
| case 'u': | |
| if (i >= st.length() - 5) { | |
| ch = 'u'; | |
| break; | |
| } | |
| int code = Integer.parseInt( | |
| "" + st.charAt(i + 2) + st.charAt(i + 3) | |
| + st.charAt(i + 4) + st.charAt(i + 5), 16); | |
| sb.append(Character.toChars(code)); | |
| i += 5; | |
| continue; | |
| } | |
| i++; | |
| } | |
| sb.append(ch); | |
| } | |
| return sb.toString(); | |
| } |
Author
No license, it is absolutely free
Hi Udo, JLine copied your unescape method from this gist into SystemRegistryImpl.java, which ships in Debian's jline3 package. Debian needs an explicit licence for it. Would you be fine stating that this gist is under the MIT licence (or CC0, or the BSD licence JLine uses)? A one-line reply here is enough. Thanks!
Author
Hi,That unescape gist is under MIT license. Thanks!Von: Juan Manuel Méndez Rey ***@***.***>Gesendet: Mittwoch, Oktober 28, 2026 10:12:34 AMAn: vejeta ***@***.***>Cc: Author ***@***.***>; Comment ***@***.***>Betreff: Re: ***@***.*** commented on this gist.Hi Udo, JLine copied your unescape method from this gist into SystemRegistryImpl.java, which ships in Debian's jline3 package. Debian needs an explicit licence for it. Would you be fine stating that this gist is under the MIT licence (or CC0, or the BSD licence JLine uses)? A one-line reply here is enough. Thanks!—Reply to this email directly, view it on GitHub or unsubscribe.You are receiving this email because you authored the thread.Triage notifications on the go with GitHub Mobile for iOS or Android.
Thanks Udo, that was quick. this will go in https://salsa.debian.org/java-team/jline3/-/merge_requests/5
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What license is this under?