Skip to content

Commit dc20222

Browse files
committed
Prefer String.fromCharCode over .fromCodePoint
1 parent 109404f commit dc20222

File tree

7 files changed

+131
-222
lines changed

7 files changed

+131
-222
lines changed

dist/ByteBufferAB.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@
17911791
}
17921792
var k;
17931793
var start = offset,
1794-
i = 0, cp;
1794+
cp;
17951795
k = utf8_calc_string(str);
17961796
// <ENSURE_CAPACITY size=k>
17971797
offset += k;
@@ -1800,9 +1800,13 @@
18001800
this.resize((capacity14 *= 2) > offset ? capacity14 : offset);
18011801
offset -= k;
18021802
// </ENSURE_CAPACITY>
1803-
while (i < str.length) {
1804-
offset += utf8_encode_char(cp = str.codePointAt(i), this, offset);
1805-
i += cp < 0xFFFF ? 1 : 2;
1803+
for (var i=0; i<str.length; i++) {
1804+
cp = str.charCodeAt(i);
1805+
if (cp >= 0xD800 && cp <= 0xDFFF) {
1806+
cp = str.codePointAt(i);
1807+
if (cp > 0xFFFF) i++;
1808+
}
1809+
offset += utf8_encode_char(cp, this, offset);
18061810
}
18071811
if (relative) {
18081812
this.offset = offset;
@@ -1830,23 +1834,26 @@
18301834
* @expose
18311835
*/
18321836
ByteBuffer.calculateUTF8Chars = function(str) {
1833-
var i = 0, n = 0;
1834-
while (i < str.length) {
1835-
i += str.codePointAt(i) < 0xFFFF ? 1 : 2;
1836-
++n;
1837+
var n = 0, cp;
1838+
for (var i=0; i<str.length; i++) {
1839+
cp = str.charCodeAt(i);
1840+
if (cp >= 0xD800 && cp <= 0xDFFF) {
1841+
cp = str.codePointAt(i);
1842+
if (cp > 0xFFFF) i++;
1843+
}
1844+
n++;
18371845
}
18381846
return n;
18391847
};
18401848

18411849
/**
18421850
* Calculates the number of UTF8 bytes of a string.
1851+
* @function
18431852
* @param {string} str String to calculate
18441853
* @returns {number} Number of UTF8 bytes
18451854
* @expose
18461855
*/
1847-
ByteBuffer.calculateUTF8Bytes = function(str) {
1848-
return utf8_calc_string(str);
1849-
};
1856+
ByteBuffer.calculateUTF8Bytes = utf8_calc_string;
18501857

18511858
/**
18521859
* Reads an UTF8 encoded string.
@@ -3272,7 +3279,7 @@
32723279
*/
32733280
function utf8_decode_char(bb, offset) {
32743281
var start = offset,
3275-
a, b, c, d, e, f,
3282+
a, b, c, d,
32763283
codePoint;
32773284
a = bb.view.getUint8(offset++);
32783285
if ((a&0x80) === 0) {
@@ -3321,10 +3328,14 @@
33213328
* @inner
33223329
*/
33233330
function utf8_calc_string(str) {
3324-
var i = 0, cp, n = 0;
3325-
while (i < str.length) {
3326-
n += utf8_calc_char(cp = str.codePointAt(i));
3327-
i += cp < 0xFFFF ? 1 : 2;
3331+
var cp, n = 0;
3332+
for (var i=0; i<str.length; i++) {
3333+
cp = str.charCodeAt(i);
3334+
if (cp >= 0xD800 && cp <= 0xDFFF) {
3335+
cp = str.codePointAt(i);
3336+
if (cp > 0xFFFF) i++;
3337+
}
3338+
n += utf8_calc_char(cp);
33283339
}
33293340
return n;
33303341
}

dist/ByteBufferAB.min.js

Lines changed: 0 additions & 87 deletions
Large diffs are not rendered by default.

dist/ByteBufferAB.min.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

dist/ByteBufferNB.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,23 +2042,26 @@ module.exports = (function() {
20422042
* @expose
20432043
*/
20442044
ByteBuffer.calculateUTF8Chars = function(str) {
2045-
var i = 0, n = 0;
2046-
while (i < str.length) {
2047-
i += str.codePointAt(i) < 0xFFFF ? 1 : 2;
2048-
++n;
2045+
var n = 0, cp;
2046+
for (var i=0; i<str.length; i++) {
2047+
cp = str.charCodeAt(i);
2048+
if (cp >= 0xD800 && cp <= 0xDFFF) {
2049+
cp = str.codePointAt(i);
2050+
if (cp > 0xFFFF) i++;
2051+
}
2052+
n++;
20492053
}
20502054
return n;
20512055
};
20522056

20532057
/**
20542058
* Calculates the number of UTF8 bytes of a string.
2059+
* @function
20552060
* @param {string} str String to calculate
20562061
* @returns {number} Number of UTF8 bytes
20572062
* @expose
20582063
*/
2059-
ByteBuffer.calculateUTF8Bytes = function(str) {
2060-
return utf8_calc_string(str);
2061-
};
2064+
ByteBuffer.calculateUTF8Bytes = utf8_calc_string;
20622065

20632066
/**
20642067
* Reads an UTF8 encoded string.
@@ -3303,7 +3306,7 @@ module.exports = (function() {
33033306
*/
33043307
function utf8_decode_char(bb, offset) {
33053308
var start = offset,
3306-
a, b, c, d, e, f,
3309+
a, b, c, d,
33073310
codePoint;
33083311
if (offset+1 > bb.buffer.length)
33093312
throw(new RangeError("Index out of range: "+offset+" + 1 <= "+bb.buffer.length));
@@ -3360,10 +3363,14 @@ module.exports = (function() {
33603363
* @inner
33613364
*/
33623365
function utf8_calc_string(str) {
3363-
var i = 0, cp, n = 0;
3364-
while (i < str.length) {
3365-
n += utf8_calc_char(cp = str.codePointAt(i));
3366-
i += cp < 0xFFFF ? 1 : 2;
3366+
var cp, n = 0;
3367+
for (var i=0; i<str.length; i++) {
3368+
cp = str.charCodeAt(i);
3369+
if (cp >= 0xD800 && cp <= 0xDFFF) {
3370+
cp = str.codePointAt(i);
3371+
if (cp > 0xFFFF) i++;
3372+
}
3373+
n += utf8_calc_char(cp);
33673374
}
33683375
return n;
33693376
}

src/encodings/utf8/native.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
function utf8_decode_char(bb, offset) { //? // Also required for node to extract CStrings and such
1212
var start = offset,
13-
a, b, c, d, e, f,
13+
a, b, c, d,
1414
codePoint;
1515
//? if (NODE) {
1616
if (offset+1 > bb.buffer.length)
@@ -25,10 +25,7 @@ function utf8_decode_char(bb, offset) { //? // Also required for node to extract
2525
//? if (NODE) {
2626
if (offset+1 > bb.buffer.length)
2727
throw(new RangeError("Index out of range: "+offset+" + 1 <= "+bb.buffer.length));
28-
//? if (INLINE)
2928
b = bb.buffer[offset++];
30-
//? else
31-
b = bb.buffer.readUInt8(offset++, true);
3229
//? } else { // getUint8 asserts on its own
3330
b = bb.view.getUint8(offset++);
3431
//? }
@@ -37,13 +34,8 @@ function utf8_decode_char(bb, offset) { //? // Also required for node to extract
3734
//? if (NODE) {
3835
if (offset+2 > bb.buffer.length)
3936
throw(new RangeError("Index out of range: "+offset+" + 2 <= "+bb.buffer.length));
40-
//? if (INLINE) {
4137
b = bb.buffer[offset++];
4238
c = bb.buffer[offset++];
43-
//? } else {
44-
b = bb.buffer.readUInt8(offset++, true);
45-
c = bb.buffer.readUInt8(offset++, true);
46-
//? }
4739
//? } else {
4840
b = bb.view.getUint8(offset++);
4941
c = bb.view.getUint8(offset++);
@@ -94,10 +86,14 @@ function utf8_calc_char(codePoint) {
9486
* @inner
9587
*/
9688
function utf8_calc_string(str) {
97-
var i = 0, cp, n = 0;
98-
while (i < str.length) {
99-
n += utf8_calc_char(cp = str.codePointAt(i));
100-
i += cp < 0xFFFF ? 1 : 2;
89+
var cp, n = 0;
90+
for (var i=0; i<str.length; i++) {
91+
cp = str.charCodeAt(i);
92+
if (cp >= 0xD800 && cp <= 0xDFFF) {
93+
cp = str.codePointAt(i);
94+
if (cp > 0xFFFF) i++;
95+
}
96+
n += utf8_calc_char(cp);
10197
}
10298
return n;
10399
}
@@ -137,4 +133,4 @@ function utf8_encode_char(codePoint, bb, offset) {
137133
return offset - start;
138134
}
139135

140-
//? }
136+
//? }

src/types/strings/utf8string.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ ByteBuffer.prototype.writeUTF8String = function(str, offset) {
4242
return k;
4343
//? } else {
4444
var start = offset,
45-
i = 0, cp;
45+
cp;
4646
k = utf8_calc_string(str);
4747
//? ENSURE_CAPACITY('k');
48-
while (i < str.length) {
49-
offset += utf8_encode_char(cp = str.codePointAt(i), this, offset);
50-
i += cp < 0xFFFF ? 1 : 2;
48+
for (var i=0; i<str.length; i++) {
49+
cp = str.charCodeAt(i);
50+
if (cp >= 0xD800 && cp <= 0xDFFF) {
51+
cp = str.codePointAt(i);
52+
if (cp > 0xFFFF) i++;
53+
}
54+
offset += utf8_encode_char(cp, this, offset);
5155
}
5256
if (relative) {
5357
this.offset = offset;
@@ -78,23 +82,26 @@ ByteBuffer.prototype.writeString = ByteBuffer.prototype.writeUTF8String;
7882
* @expose
7983
*/
8084
ByteBuffer.calculateUTF8Chars = function(str) {
81-
var i = 0, n = 0;
82-
while (i < str.length) {
83-
i += str.codePointAt(i) < 0xFFFF ? 1 : 2;
84-
++n;
85+
var n = 0, cp;
86+
for (var i=0; i<str.length; i++) {
87+
cp = str.charCodeAt(i);
88+
if (cp >= 0xD800 && cp <= 0xDFFF) {
89+
cp = str.codePointAt(i);
90+
if (cp > 0xFFFF) i++;
91+
}
92+
n++;
8593
}
8694
return n;
8795
};
8896

8997
/**
9098
* Calculates the number of UTF8 bytes of a string.
99+
* @function
91100
* @param {string} str String to calculate
92101
* @returns {number} Number of UTF8 bytes
93102
* @expose
94103
*/
95-
ByteBuffer.calculateUTF8Bytes = function(str) {
96-
return utf8_calc_string(str);
97-
};
104+
ByteBuffer.calculateUTF8Bytes = utf8_calc_string;
98105

99106
/**
100107
* Reads an UTF8 encoded string.

0 commit comments

Comments
 (0)