OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 402 |
403 | 403 |
404 /** | 404 /** |
405 * Writes a UTF16 Javascript string to the buffer encoded as UTF8. | 405 * Writes a UTF16 Javascript string to the buffer encoded as UTF8. |
406 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates. | 406 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates. |
407 * @param {string} value The string to write. | 407 * @param {string} value The string to write. |
408 * @return {number} The number of bytes used to encode the string. | 408 * @return {number} The number of bytes used to encode the string. |
409 */ | 409 */ |
410 jspb.BinaryEncoder.prototype.writeString = function(value) { | 410 jspb.BinaryEncoder.prototype.writeString = function(value) { |
411 var oldLength = this.buffer_.length; | 411 var oldLength = this.buffer_.length; |
| 412 |
| 413 for (var i = 0; i < value.length; i++) { |
| 414 |
| 415 var c = value.charCodeAt(i); |
412 | 416 |
413 // UTF16 to UTF8 conversion loop swiped from goog.crypt.stringToUtf8ByteArray. | |
414 for (var i = 0; i < value.length; i++) { | |
415 var c = value.charCodeAt(i); | |
416 if (c < 128) { | 417 if (c < 128) { |
417 this.buffer_.push(c); | 418 this.buffer_.push(c); |
418 } else if (c < 2048) { | 419 } else if (c < 2048) { |
419 this.buffer_.push((c >> 6) | 192); | 420 this.buffer_.push((c >> 6) | 192); |
420 this.buffer_.push((c & 63) | 128); | 421 this.buffer_.push((c & 63) | 128); |
421 } else { | 422 } else if (c < 65536) { |
422 this.buffer_.push((c >> 12) | 224); | 423 // Look for surrogates |
423 this.buffer_.push(((c >> 6) & 63) | 128); | 424 if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) { |
424 this.buffer_.push((c & 63) | 128); | 425 var second = value.charCodeAt(i + 1); |
| 426 if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate |
| 427 // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formula
e |
| 428 c = (c - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; |
| 429 |
| 430 this.buffer_.push((c >> 18) | 240); |
| 431 this.buffer_.push(((c >> 12) & 63 ) | 128); |
| 432 this.buffer_.push(((c >> 6) & 63) | 128); |
| 433 this.buffer_.push((c & 63) | 128); |
| 434 i++; |
| 435 } |
| 436 } |
| 437 else { |
| 438 this.buffer_.push((c >> 12) | 224); |
| 439 this.buffer_.push(((c >> 6) & 63) | 128); |
| 440 this.buffer_.push((c & 63) | 128); |
| 441 } |
425 } | 442 } |
426 } | 443 } |
427 | 444 |
428 var length = this.buffer_.length - oldLength; | 445 var length = this.buffer_.length - oldLength; |
429 return length; | 446 return length; |
430 }; | 447 }; |
OLD | NEW |