Skip to content

Instantly share code, notes, and snippets.

@tetek
Created August 2, 2016 10:58
Show Gist options
  • Select an option

  • Save tetek/7ce687f53dad389f47121743cbd6913a to your computer and use it in GitHub Desktop.

Select an option

Save tetek/7ce687f53dad389f47121743cbd6913a to your computer and use it in GitHub Desktop.
// Emoji fix
// Replace the function in protobuf/js/binary/encoder.js (at the bottom)
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.charCodeAt(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 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);
}
}
var length = this.buffer_.length - oldLength;
return length;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment