aboutsummaryrefslogtreecommitdiff
path: root/js/binary/encoder.js
diff options
context:
space:
mode:
authorWojciech Mandrysz <tetek1@gmail.com>2016-08-02 17:12:27 +0200
committerWojciech Mandrysz <tetek1@gmail.com>2016-10-03 01:44:15 +0200
commit23f108d471ec6488efbef1a5b96d7a2abbf785c2 (patch)
tree39d72c7c5cd46a488c450152c54aed8a2ec87ba7 /js/binary/encoder.js
parent8d8115bf524044f2ab5928d7b4050d04f64d3f1c (diff)
downloadprotobuf-23f108d471ec6488efbef1a5b96d7a2abbf785c2.tar.gz
protobuf-23f108d471ec6488efbef1a5b96d7a2abbf785c2.tar.bz2
protobuf-23f108d471ec6488efbef1a5b96d7a2abbf785c2.zip
JS: Fixed UTF-8 string encoder/decoder for high codepoints.
Diffstat (limited to 'js/binary/encoder.js')
-rw-r--r--js/binary/encoder.js10
1 files changed, 8 insertions, 2 deletions
diff --git a/js/binary/encoder.js b/js/binary/encoder.js
index c9b0c2ae..59c4ccb9 100644
--- a/js/binary/encoder.js
+++ b/js/binary/encoder.js
@@ -412,16 +412,22 @@ jspb.BinaryEncoder.prototype.writeString = function(value) {
// UTF16 to UTF8 conversion loop swiped from goog.crypt.stringToUtf8ByteArray.
for (var i = 0; i < value.length; i++) {
- var c = value.charCodeAt(i);
+ var c = value.codePointAt(i);
if (c < 128) {
this.buffer_.push(c);
} else if (c < 2048) {
this.buffer_.push((c >> 6) | 192);
this.buffer_.push((c & 63) | 128);
- } else {
+ } else if (c < 65536) {
this.buffer_.push((c >> 12) | 224);
this.buffer_.push(((c >> 6) & 63) | 128);
this.buffer_.push((c & 63) | 128);
+ } else {
+ this.buffer_.push((c >> 18) | 240);
+ this.buffer_.push(((c >> 12) & 63 ) | 128);
+ this.buffer_.push(((c >> 6) & 63) | 128);
+ this.buffer_.push((c & 63) | 128);
+ i++;
}
}