Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.io.Writer;

import org.apache.commons.lang3.CharUtils;

/**
* Translates escaped Unicode values of the form \\u+\d\d\d\d back to
* Unicode. It supports multiple 'u' characters and will work with or
Expand Down Expand Up @@ -54,7 +56,12 @@ public int translate(final CharSequence input, final int index, final Writer wri
if (index + i + 4 <= input.length()) {
// Get 4 hex digits
final CharSequence unicode = input.subSequence(index + i, index + i + 4);

// Integer.parseInt also accepts a leading sign, which is not a hex digit
for (int j = 0; j < unicode.length(); j++) {
if (!CharUtils.isHex(unicode.charAt(j))) {
throw new IllegalArgumentException("Unable to parse unicode value: " + unicode);
}
}
try {
final int value = Integer.parseInt(unicode.toString(), 16);
writer.write((char) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ void testLessThanFour() {
assertThrows(IllegalArgumentException.class, () -> escaper.translate(input));
}

@Test
void testSignNotHexDigit() {
final UnicodeUnescaper escaper = new UnicodeUnescaper();
assertThrows(IllegalArgumentException.class, () -> escaper.translate("\\u-03C"));
assertThrows(IllegalArgumentException.class, () -> escaper.translate("\\u++03C"));
assertThrows(IllegalArgumentException.class, () -> escaper.translate("\\u+-03C"));
}

@Test
void testTooShort() {
assertThrows(IllegalArgumentException.class, () -> new UnicodeUnescaper().translate("\\u"));
Expand Down