Skip to content

Commit 190c5b9

Browse files
committed
Fix unicode parser in string and lodash plugins.
1 parent f659a80 commit 190c5b9

File tree

4 files changed

+78
-48
lines changed
  • lodash-plugin/src
  • string-plugin/src

4 files changed

+78
-48
lines changed

lodash-plugin/src/main/java/com/github/underscore/lodash/$.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,14 +1822,20 @@ private void readEscape() {
18221822
break;
18231823
case 'u':
18241824
char[] hexChars = new char[4];
1825+
boolean isHexCharsDigits = true;
18251826
for (int i = 0; i < 4; i++) {
18261827
read();
18271828
if (!isHexDigit()) {
1828-
throw expected("hexadecimal digit");
1829+
isHexCharsDigits = false;
18291830
}
18301831
hexChars[i] = (char) current;
18311832
}
1832-
captureBuffer.append((char) Integer.parseInt(new String(hexChars), 16));
1833+
if (isHexCharsDigits) {
1834+
captureBuffer.append((char) Integer.parseInt(new String(hexChars), 16));
1835+
} else {
1836+
captureBuffer.append("\\u").append(hexChars[0]).append(hexChars[1]).append(hexChars[2])
1837+
.append(hexChars[3]);
1838+
}
18331839
break;
18341840
default:
18351841
throw expected("valid escape sequence");

lodash-plugin/src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -798,17 +798,28 @@ public void testDecodeUnicode() {
798798
assertEquals("[\n \"abc\u0A00\"\n]", $.toJson((List<Object>) $.fromJson("[\"abc\\u0A00\"]")));
799799
}
800800

801-
@Test(expected = $.ParseException.class)
802-
public void testDecodeUnicodeErr1() {
803-
try {
804-
$.fromJson("[\"abc\\u0$00\"]");
805-
fail("Expected ParseException");
806-
} catch ($.ParseException ex) {
807-
ex.getOffset();
808-
ex.getLine();
809-
ex.getColumn();
810-
throw ex;
811-
}
801+
@Test
802+
public void testDecodeSpecialCharacter() {
803+
assertEquals("{\n \"description\": \"c:\\\\userDescription.txt\"\n}", $.toJson(
804+
(Map<String, Object>) $.fromJson("{\"description\":\"c:\\userDescription.txt\"}")));
805+
assertEquals("{description=c:\\userDescription.txt}", $.fromJson(
806+
$.toJson(new LinkedHashMap<String, String>() { {
807+
put("description", "c:\\userDescription.txt"); } })).toString());
808+
}
809+
810+
@Test
811+
public void testDecodeUnicode1() {
812+
assertEquals("[abc\\u0$00]", $.fromJson("[\"abc\\u0$00\"]").toString());
813+
}
814+
815+
@Test
816+
public void testDecodeUnicode2() {
817+
assertEquals("[abc\\u001g/]", $.fromJson("[\"abc\\u001g\\/\"]").toString());
818+
}
819+
820+
@Test
821+
public void testDecodeUnicode3() {
822+
assertEquals("[abc\\u001G/]", $.fromJson("[\"abc\\u001G\\/\"]").toString());
812823
}
813824

814825
@Test(expected = $.ParseException.class)
@@ -841,11 +852,6 @@ public void testDecodeParseErr6() {
841852
$.fromJson("[ture]");
842853
}
843854

844-
@Test(expected = $.ParseException.class)
845-
public void testDecodeParseErr7() {
846-
$.fromJson("[\"abc\\u001g\\/\"]");
847-
}
848-
849855
@Test(expected = $.ParseException.class)
850856
public void testDecodeParseErr8() {
851857
$.fromJson("[\"\\abc\"]");
@@ -858,7 +864,15 @@ public void testDecodeParseErr9() {
858864

859865
@Test(expected = $.ParseException.class)
860866
public void testDecodeParseErr10() {
861-
$.fromJson("[123.a]");
867+
try {
868+
$.fromJson("[123.a]");
869+
fail("Expected ParseException");
870+
} catch ($.ParseException ex) {
871+
ex.getOffset();
872+
ex.getLine();
873+
ex.getColumn();
874+
throw ex;
875+
}
862876
}
863877

864878
@Test(expected = $.ParseException.class)
@@ -881,11 +895,6 @@ public void testDecodeParseErr14() {
881895
$.fromJson("[\"abc\"][]");
882896
}
883897

884-
@Test(expected = $.ParseException.class)
885-
public void testDecodeParseErr15() {
886-
$.fromJson("[\"abc\\u001G\\/\"]");
887-
}
888-
889898
@SuppressWarnings("unchecked")
890899
@Test
891900
public void main() throws Exception {

string-plugin/src/main/java/com/github/underscore/string/$.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,14 +1379,20 @@ private void readEscape() {
13791379
break;
13801380
case 'u':
13811381
char[] hexChars = new char[4];
1382+
boolean isHexCharsDigits = true;
13821383
for (int i = 0; i < 4; i++) {
13831384
read();
13841385
if (!isHexDigit()) {
1385-
throw expected("hexadecimal digit");
1386+
isHexCharsDigits = false;
13861387
}
13871388
hexChars[i] = (char) current;
13881389
}
1389-
captureBuffer.append((char) Integer.parseInt(new String(hexChars), 16));
1390+
if (isHexCharsDigits) {
1391+
captureBuffer.append((char) Integer.parseInt(new String(hexChars), 16));
1392+
} else {
1393+
captureBuffer.append("\\u").append(hexChars[0]).append(hexChars[1]).append(hexChars[2])
1394+
.append(hexChars[3]);
1395+
}
13901396
break;
13911397
default:
13921398
throw expected("valid escape sequence");

string-plugin/src/test/java/com/github/underscore/string/StringTest.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -800,17 +800,28 @@ public void testDecodeUnicode() {
800800
assertEquals("[\n \"abc\u0A00\"\n]", $.toJson((List<Object>) $.fromJson("[\"abc\\u0A00\"]")));
801801
}
802802

803-
@Test(expected = $.ParseException.class)
804-
public void testDecodeUnicodeErr1() {
805-
try {
806-
$.fromJson("[\"abc\\u0$00\"]");
807-
fail("Expected ParseException");
808-
} catch ($.ParseException ex) {
809-
ex.getOffset();
810-
ex.getLine();
811-
ex.getColumn();
812-
throw ex;
813-
}
803+
@Test
804+
public void testDecodeSpecialCharacter() {
805+
assertEquals("{\n \"description\": \"c:\\\\userDescription.txt\"\n}", $.toJson(
806+
(Map<String, Object>) $.fromJson("{\"description\":\"c:\\userDescription.txt\"}")));
807+
assertEquals("{description=c:\\userDescription.txt}", $.fromJson(
808+
$.toJson(new LinkedHashMap<String, String>() { {
809+
put("description", "c:\\userDescription.txt"); } })).toString());
810+
}
811+
812+
@Test
813+
public void testDecodeUnicode1() {
814+
assertEquals("[abc\\u0$00]", $.fromJson("[\"abc\\u0$00\"]").toString());
815+
}
816+
817+
@Test
818+
public void testDecodeUnicode2() {
819+
assertEquals("[abc\\u001g/]", $.fromJson("[\"abc\\u001g\\/\"]").toString());
820+
}
821+
822+
@Test
823+
public void testDecodeUnicode3() {
824+
assertEquals("[abc\\u001G/]", $.fromJson("[\"abc\\u001G\\/\"]").toString());
814825
}
815826

816827
@Test(expected = $.ParseException.class)
@@ -843,11 +854,6 @@ public void testDecodeParseErr6() {
843854
$.fromJson("[ture]");
844855
}
845856

846-
@Test(expected = $.ParseException.class)
847-
public void testDecodeParseErr7() {
848-
$.fromJson("[\"abc\\u001g\\/\"]");
849-
}
850-
851857
@Test(expected = $.ParseException.class)
852858
public void testDecodeParseErr8() {
853859
$.fromJson("[\"\\abc\"]");
@@ -860,7 +866,15 @@ public void testDecodeParseErr9() {
860866

861867
@Test(expected = $.ParseException.class)
862868
public void testDecodeParseErr10() {
863-
$.fromJson("[123.a]");
869+
try {
870+
$.fromJson("[123.a]");
871+
fail("Expected ParseException");
872+
} catch ($.ParseException ex) {
873+
ex.getOffset();
874+
ex.getLine();
875+
ex.getColumn();
876+
throw ex;
877+
}
864878
}
865879

866880
@Test(expected = $.ParseException.class)
@@ -883,11 +897,6 @@ public void testDecodeParseErr14() {
883897
$.fromJson("[\"abc\"][]");
884898
}
885899

886-
@Test(expected = $.ParseException.class)
887-
public void testDecodeParseErr15() {
888-
$.fromJson("[\"abc\\u001G\\/\"]");
889-
}
890-
891900
@SuppressWarnings("unchecked")
892901
@Test
893902
public void main() throws Exception {

0 commit comments

Comments
 (0)