Skip to content

Commit 0bad3ed

Browse files
committed
Fix ByteBuffer.fromUTF8
1 parent 774f658 commit 0bad3ed

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

dist/ByteBufferAB.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,9 +3148,15 @@
31483148
if (typeof str !== 'string')
31493149
throw(new TypeError("Illegal str: Not a string"));
31503150
}
3151-
var bb = new ByteBuffer(utf8_calc_string(str), littleEndian, noAssert);
3151+
var bb = new ByteBuffer(utf8_calc_string(str), littleEndian, noAssert),
3152+
cp;
31523153
for (var i=0, j=0, k=str.length; i<k; ++i) {
3153-
j += utf8_encode_char(str.codePointAt(i), bb, j);
3154+
cp = str.charCodeAt(i);
3155+
if (cp >= 0xD800 && cp <= 0xDFFF) {
3156+
cp = str.codePointAt(i);
3157+
if (cp > 0xFFFF) i++;
3158+
}
3159+
j += utf8_encode_char(cp, bb, j);
31543160
}
31553161
bb.limit = j;
31563162
return bb;

dist/ByteBufferAB.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ByteBufferAB.min.js.gz

6 Bytes
Binary file not shown.

dist/ByteBufferAB.min.map

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/encodings/utf8.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ ByteBuffer.fromUTF8 = function(str, littleEndian, noAssert) {
5656
bb.view = new BufferView(bb.buffer);
5757
bb.limit = bb.buffer.length;
5858
//? } else {
59-
var bb = new ByteBuffer(utf8_calc_string(str), littleEndian, noAssert);
59+
var bb = new ByteBuffer(utf8_calc_string(str), littleEndian, noAssert),
60+
cp;
6061
for (var i=0, j=0, k=str.length; i<k; ++i) {
61-
j += utf8_encode_char(str.codePointAt(i), bb, j);
62+
cp = str.charCodeAt(i);
63+
if (cp >= 0xD800 && cp <= 0xDFFF) {
64+
cp = str.codePointAt(i);
65+
if (cp > 0xFFFF) i++;
66+
}
67+
j += utf8_encode_char(cp, bb, j);
6268
}
6369
bb.limit = j;
6470
//? }

0 commit comments

Comments
 (0)