aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/binary
diff options
context:
space:
mode:
authorGravatar Wojciech Mandrysz <tetek1@gmail.com>2016-11-15 12:44:15 +0100
committerGravatar Wojciech Mandrysz <tetek1@gmail.com>2016-11-15 12:44:15 +0100
commit292c2c91cfc16eda5dc8f835ef6073febef118e5 (patch)
tree768e8f58c603bb6aff7c437dffe0bc7fb26da3ef /js/binary
parent7332ffb1f08c9414119aa0a59ec8334c7599bfd8 (diff)
JS: Re-added comment, moved surrogates code to the right place
Diffstat (limited to 'js/binary')
-rw-r--r--js/binary/decoder.js3
-rw-r--r--js/binary/encoder.js16
2 files changed, 10 insertions, 9 deletions
diff --git a/js/binary/decoder.js b/js/binary/decoder.js
index e4fb9148..040cf715 100644
--- a/js/binary/decoder.js
+++ b/js/binary/decoder.js
@@ -941,7 +941,8 @@ jspb.BinaryDecoder.prototype.readString = function(length) {
codeUnits.push(high, low)
}
}
-
+ // String.fromCharCode.apply is faster than manually appending characters on
+ // Chrome 25+, and generates no additional cons string garbage.
var result = String.fromCharCode.apply(null, codeUnits);
this.cursor_ = cursor;
return result;
diff --git a/js/binary/encoder.js b/js/binary/encoder.js
index fe5e34e9..a9d09d72 100644
--- a/js/binary/encoder.js
+++ b/js/binary/encoder.js
@@ -413,14 +413,6 @@ jspb.BinaryEncoder.prototype.writeString = function(value) {
for (var i = 0; i < value.length; i++) {
var c = value.charCodeAt(i);
- // Look for surrogates
- if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) {
- var second = value.charCodeAt(i + 1);
- if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
- // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
- c = (c - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
- }
- }
if (c < 128) {
this.buffer_.push(c);
@@ -428,6 +420,14 @@ jspb.BinaryEncoder.prototype.writeString = function(value) {
this.buffer_.push((c >> 6) | 192);
this.buffer_.push((c & 63) | 128);
} else if (c < 65536) {
+ // Look for surrogates
+ if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) {
+ var second = value.charCodeAt(i + 1);
+ if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
+ // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
+ c = (c - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
+ }
+ }
this.buffer_.push((c >> 12) | 224);
this.buffer_.push(((c >> 6) & 63) | 128);
this.buffer_.push((c & 63) | 128);