| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 while (highBits > 0 || lowBits > 127) { | 93 while (highBits > 0 || lowBits > 127) { |
| 94 this.buffer_.push((lowBits & 0x7f) | 0x80); | 94 this.buffer_.push((lowBits & 0x7f) | 0x80); |
| 95 lowBits = ((lowBits >>> 7) | (highBits << 25)) >>> 0; | 95 lowBits = ((lowBits >>> 7) | (highBits << 25)) >>> 0; |
| 96 highBits = highBits >>> 7; | 96 highBits = highBits >>> 7; |
| 97 } | 97 } |
| 98 this.buffer_.push(lowBits); | 98 this.buffer_.push(lowBits); |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Encodes a 64-bit integer in 32:32 split representation into its wire-format | |
| 104 * fixed representation and stores it in the buffer. | |
| 105 * @param {number} lowBits The low 32 bits of the int. | |
| 106 * @param {number} highBits The high 32 bits of the int. | |
| 107 */ | |
| 108 jspb.BinaryEncoder.prototype.writeSplitFixed64 = function(lowBits, highBits) { | |
| 109 goog.asserts.assert(lowBits == Math.floor(lowBits)); | |
| 110 goog.asserts.assert(highBits == Math.floor(highBits)); | |
| 111 goog.asserts.assert((lowBits >= 0) && | |
| 112 (lowBits < jspb.BinaryConstants.TWO_TO_32)); | |
| 113 goog.asserts.assert((highBits >= 0) && | |
| 114 (highBits < jspb.BinaryConstants.TWO_TO_32)); | |
| 115 this.writeUint32(lowBits); | |
| 116 this.writeUint32(highBits); | |
| 117 }; | |
| 118 | |
| 119 | |
| 120 /** | |
| 121 * Encodes a 32-bit unsigned integer into its wire-format varint representation | 103 * Encodes a 32-bit unsigned integer into its wire-format varint representation |
| 122 * and stores it in the buffer. | 104 * and stores it in the buffer. |
| 123 * @param {number} value The integer to convert. | 105 * @param {number} value The integer to convert. |
| 124 */ | 106 */ |
| 125 jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) { | 107 jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) { |
| 126 goog.asserts.assert(value == Math.floor(value)); | 108 goog.asserts.assert(value == Math.floor(value)); |
| 127 goog.asserts.assert((value >= 0) && | 109 goog.asserts.assert((value >= 0) && |
| 128 (value < jspb.BinaryConstants.TWO_TO_32)); | 110 (value < jspb.BinaryConstants.TWO_TO_32)); |
| 129 | 111 |
| 130 while (value > 127) { | 112 while (value > 127) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 goog.asserts.assert(value == Math.floor(value)); | 201 goog.asserts.assert(value == Math.floor(value)); |
| 220 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && | 202 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 221 (value < jspb.BinaryConstants.TWO_TO_63)); | 203 (value < jspb.BinaryConstants.TWO_TO_63)); |
| 222 jspb.utils.splitZigzag64(value); | 204 jspb.utils.splitZigzag64(value); |
| 223 this.writeSplitVarint64(jspb.utils.split64Low, | 205 this.writeSplitVarint64(jspb.utils.split64Low, |
| 224 jspb.utils.split64High); | 206 jspb.utils.split64High); |
| 225 }; | 207 }; |
| 226 | 208 |
| 227 | 209 |
| 228 /** | 210 /** |
| 229 * Encodes a JavaScript decimal string into its wire-format, zigzag-encoded | |
| 230 * varint representation and stores it in the buffer. Integers not representable | |
| 231 * in 64 bits will be truncated. | |
| 232 * @param {string} value The integer to convert. | |
| 233 */ | |
| 234 jspb.BinaryEncoder.prototype.writeZigzagVarint64String = function(value) { | |
| 235 // TODO(haberman): write lossless 64-bit zig-zag math. | |
| 236 this.writeZigzagVarint64(parseInt(value, 10)); | |
| 237 }; | |
| 238 | |
| 239 | |
| 240 /** | |
| 241 * Writes a 8-bit unsigned integer to the buffer. Numbers outside the range | 211 * Writes a 8-bit unsigned integer to the buffer. Numbers outside the range |
| 242 * [0,2^8) will be truncated. | 212 * [0,2^8) will be truncated. |
| 243 * @param {number} value The value to write. | 213 * @param {number} value The value to write. |
| 244 */ | 214 */ |
| 245 jspb.BinaryEncoder.prototype.writeUint8 = function(value) { | 215 jspb.BinaryEncoder.prototype.writeUint8 = function(value) { |
| 246 goog.asserts.assert(value == Math.floor(value)); | 216 goog.asserts.assert(value == Math.floor(value)); |
| 247 goog.asserts.assert((value >= 0) && (value < 256)); | 217 goog.asserts.assert((value >= 0) && (value < 256)); |
| 248 this.buffer_.push((value >>> 0) & 0xFF); | 218 this.buffer_.push((value >>> 0) & 0xFF); |
| 249 }; | 219 }; |
| 250 | 220 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 /** | 307 /** |
| 338 * Writes a 64-bit integer to the buffer. Numbers outside the range | 308 * Writes a 64-bit integer to the buffer. Numbers outside the range |
| 339 * [-2^63,2^63) will be truncated. | 309 * [-2^63,2^63) will be truncated. |
| 340 * @param {number} value The value to write. | 310 * @param {number} value The value to write. |
| 341 */ | 311 */ |
| 342 jspb.BinaryEncoder.prototype.writeInt64 = function(value) { | 312 jspb.BinaryEncoder.prototype.writeInt64 = function(value) { |
| 343 goog.asserts.assert(value == Math.floor(value)); | 313 goog.asserts.assert(value == Math.floor(value)); |
| 344 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && | 314 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && |
| 345 (value < jspb.BinaryConstants.TWO_TO_63)); | 315 (value < jspb.BinaryConstants.TWO_TO_63)); |
| 346 jspb.utils.splitInt64(value); | 316 jspb.utils.splitInt64(value); |
| 347 this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High); | 317 this.writeUint32(jspb.utils.split64Low); |
| 318 this.writeUint32(jspb.utils.split64High); |
| 348 }; | 319 }; |
| 349 | 320 |
| 350 | 321 |
| 351 /** | |
| 352 * Writes a 64-bit integer decimal strings to the buffer. Numbers outside the | |
| 353 * range [-2^63,2^63) will be truncated. | |
| 354 * @param {string} value The value to write. | |
| 355 */ | |
| 356 jspb.BinaryEncoder.prototype.writeInt64String = function(value) { | |
| 357 goog.asserts.assert(value == Math.floor(value)); | |
| 358 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && | |
| 359 (value < jspb.BinaryConstants.TWO_TO_63)); | |
| 360 jspb.utils.splitHash64(jspb.utils.decimalStringToHash64(value)); | |
| 361 this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High); | |
| 362 }; | |
| 363 | |
| 364 | |
| 365 /** | 322 /** |
| 366 * Writes a single-precision floating point value to the buffer. Numbers | 323 * Writes a single-precision floating point value to the buffer. Numbers |
| 367 * requiring more than 32 bits of precision will be truncated. | 324 * requiring more than 32 bits of precision will be truncated. |
| 368 * @param {number} value The value to write. | 325 * @param {number} value The value to write. |
| 369 */ | 326 */ |
| 370 jspb.BinaryEncoder.prototype.writeFloat = function(value) { | 327 jspb.BinaryEncoder.prototype.writeFloat = function(value) { |
| 371 goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT32_MAX) && | 328 goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT32_MAX) && |
| 372 (value <= jspb.BinaryConstants.FLOAT32_MAX)); | 329 (value <= jspb.BinaryConstants.FLOAT32_MAX)); |
| 373 jspb.utils.splitFloat32(value); | 330 jspb.utils.splitFloat32(value); |
| 374 this.writeUint32(jspb.utils.split64Low); | 331 this.writeUint32(jspb.utils.split64Low); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 402 |
| 446 | 403 |
| 447 /** | 404 /** |
| 448 * Writes a UTF16 Javascript string to the buffer encoded as UTF8. | 405 * Writes a UTF16 Javascript string to the buffer encoded as UTF8. |
| 449 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates. | 406 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates. |
| 450 * @param {string} value The string to write. | 407 * @param {string} value The string to write. |
| 451 * @return {number} The number of bytes used to encode the string. | 408 * @return {number} The number of bytes used to encode the string. |
| 452 */ | 409 */ |
| 453 jspb.BinaryEncoder.prototype.writeString = function(value) { | 410 jspb.BinaryEncoder.prototype.writeString = function(value) { |
| 454 var oldLength = this.buffer_.length; | 411 var oldLength = this.buffer_.length; |
| 455 | 412 |
| 413 // UTF16 to UTF8 conversion loop swiped from goog.crypt.stringToUtf8ByteArray. |
| 456 for (var i = 0; i < value.length; i++) { | 414 for (var i = 0; i < value.length; i++) { |
| 457 | |
| 458 var c = value.charCodeAt(i); | 415 var c = value.charCodeAt(i); |
| 459 | |
| 460 if (c < 128) { | 416 if (c < 128) { |
| 461 this.buffer_.push(c); | 417 this.buffer_.push(c); |
| 462 } else if (c < 2048) { | 418 } else if (c < 2048) { |
| 463 this.buffer_.push((c >> 6) | 192); | 419 this.buffer_.push((c >> 6) | 192); |
| 464 this.buffer_.push((c & 63) | 128); | 420 this.buffer_.push((c & 63) | 128); |
| 465 } else if (c < 65536) { | 421 } else { |
| 466 // Look for surrogates | 422 this.buffer_.push((c >> 12) | 224); |
| 467 if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) { | 423 this.buffer_.push(((c >> 6) & 63) | 128); |
| 468 var second = value.charCodeAt(i + 1); | 424 this.buffer_.push((c & 63) | 128); |
| 469 if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate | |
| 470 // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formula
e | |
| 471 c = (c - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; | |
| 472 | |
| 473 this.buffer_.push((c >> 18) | 240); | |
| 474 this.buffer_.push(((c >> 12) & 63 ) | 128); | |
| 475 this.buffer_.push(((c >> 6) & 63) | 128); | |
| 476 this.buffer_.push((c & 63) | 128); | |
| 477 i++; | |
| 478 } | |
| 479 } | |
| 480 else { | |
| 481 this.buffer_.push((c >> 12) | 224); | |
| 482 this.buffer_.push(((c >> 6) & 63) | 128); | |
| 483 this.buffer_.push((c & 63) | 128); | |
| 484 } | |
| 485 } | 425 } |
| 486 } | 426 } |
| 487 | 427 |
| 488 var length = this.buffer_.length - oldLength; | 428 var length = this.buffer_.length - oldLength; |
| 489 return length; | 429 return length; |
| 490 }; | 430 }; |
| OLD | NEW |