Skip to content

Commit 49654cf

Browse files
committed
Since JsonBuffer is reading character-by-character anyway, there is no need to use String.codePointAt. And doing so is incorrect
unless there is a corresponding call to Character.charCount instead of just incrementing the position by a single character. The fix is simply to use use String.charAt instead of String.codePointAt. JAVA-1793
1 parent 0663d73 commit 49654cf

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

bson/src/main/org/bson/json/JsonBuffer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public void setPosition(final int position) {
3434
}
3535

3636
public int read() {
37-
return (position >= buffer.length()) ? -1 : buffer.codePointAt(position++);
37+
return (position >= buffer.length()) ? -1 : buffer.charAt(position++);
3838
}
3939

4040
public void unread(final int c) {
41-
if (c != -1 && buffer.codePointAt(position - 1) == c) {
41+
if (c != -1 && buffer.charAt(position - 1) == c) {
4242
position--;
4343
}
4444
}

bson/src/test/unit/org/bson/json/JsonReaderTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,32 @@ public void testRegularExpressionStrict() {
486486

487487
@Test
488488
public void testString() {
489-
String json = "\"abc\"";
489+
String str = "abc";
490+
String json = '"' + str + '"';
490491
bsonReader = new JsonReader(json);
491492
assertEquals(BsonType.STRING, bsonReader.readBsonType());
492-
assertEquals("abc", bsonReader.readString());
493+
assertEquals(str, bsonReader.readString());
494+
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
495+
496+
str = "\ud806\udc5c";
497+
json = '"' + str + '"';
498+
bsonReader = new JsonReader(json);
499+
assertEquals(BsonType.STRING, bsonReader.readBsonType());
500+
assertEquals(str, bsonReader.readString());
501+
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
502+
503+
str = "\\ud806\\udc5c";
504+
json = '"' + str + '"';
505+
bsonReader = new JsonReader(json);
506+
assertEquals(BsonType.STRING, bsonReader.readBsonType());
507+
assertEquals("\ud806\udc5c", bsonReader.readString());
508+
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
509+
510+
str = "꼢𑡜ᳫ鉠鮻罖᧭䆔瘉";
511+
json = '"' + str + '"';
512+
bsonReader = new JsonReader(json);
513+
assertEquals(BsonType.STRING, bsonReader.readBsonType());
514+
assertEquals(str, bsonReader.readString());
493515
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
494516
}
495517

0 commit comments

Comments
 (0)