diff options
author | Wojciech Mandrysz <tetek1@gmail.com> | 2016-10-03 18:59:34 +0200 |
---|---|---|
committer | Wojciech Mandrysz <tetek1@gmail.com> | 2016-10-10 23:00:01 +0200 |
commit | 7332ffb1f08c9414119aa0a59ec8334c7599bfd8 (patch) | |
tree | b4c168df81adba79f3e4694e50679deefd450474 /js/binary/encoder.js | |
parent | fe1d0a1f5a32fe144ee125eb0058927d7a359926 (diff) | |
download | protobuf-7332ffb1f08c9414119aa0a59ec8334c7599bfd8.tar.gz protobuf-7332ffb1f08c9414119aa0a59ec8334c7599bfd8.tar.bz2 protobuf-7332ffb1f08c9414119aa0a59ec8334c7599bfd8.zip |
JS: Replaced fromCodePoint/codePointAt with fromCharCode/charCodeAt because of functions limited availability, fixed typo in tests.
Diffstat (limited to 'js/binary/encoder.js')
-rw-r--r-- | js/binary/encoder.js | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/js/binary/encoder.js b/js/binary/encoder.js index 59c4ccb9..fe5e34e9 100644 --- a/js/binary/encoder.js +++ b/js/binary/encoder.js @@ -409,10 +409,19 @@ jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) { */ jspb.BinaryEncoder.prototype.writeString = function(value) { var oldLength = this.buffer_.length; - - // UTF16 to UTF8 conversion loop swiped from goog.crypt.stringToUtf8ByteArray. + for (var i = 0; i < value.length; i++) { - var c = value.codePointAt(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); } else if (c < 2048) { |