Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: third_party/protobuf/js/binary/encoder.js

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 /**
103 * Encodes a 32-bit unsigned integer into its wire-format varint representation 121 * Encodes a 32-bit unsigned integer into its wire-format varint representation
104 * and stores it in the buffer. 122 * and stores it in the buffer.
105 * @param {number} value The integer to convert. 123 * @param {number} value The integer to convert.
106 */ 124 */
107 jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) { 125 jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) {
108 goog.asserts.assert(value == Math.floor(value)); 126 goog.asserts.assert(value == Math.floor(value));
109 goog.asserts.assert((value >= 0) && 127 goog.asserts.assert((value >= 0) &&
110 (value < jspb.BinaryConstants.TWO_TO_32)); 128 (value < jspb.BinaryConstants.TWO_TO_32));
111 129
112 while (value > 127) { 130 while (value > 127) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 goog.asserts.assert(value == Math.floor(value)); 219 goog.asserts.assert(value == Math.floor(value));
202 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && 220 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
203 (value < jspb.BinaryConstants.TWO_TO_63)); 221 (value < jspb.BinaryConstants.TWO_TO_63));
204 jspb.utils.splitZigzag64(value); 222 jspb.utils.splitZigzag64(value);
205 this.writeSplitVarint64(jspb.utils.split64Low, 223 this.writeSplitVarint64(jspb.utils.split64Low,
206 jspb.utils.split64High); 224 jspb.utils.split64High);
207 }; 225 };
208 226
209 227
210 /** 228 /**
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 /**
211 * Writes a 8-bit unsigned integer to the buffer. Numbers outside the range 241 * Writes a 8-bit unsigned integer to the buffer. Numbers outside the range
212 * [0,2^8) will be truncated. 242 * [0,2^8) will be truncated.
213 * @param {number} value The value to write. 243 * @param {number} value The value to write.
214 */ 244 */
215 jspb.BinaryEncoder.prototype.writeUint8 = function(value) { 245 jspb.BinaryEncoder.prototype.writeUint8 = function(value) {
216 goog.asserts.assert(value == Math.floor(value)); 246 goog.asserts.assert(value == Math.floor(value));
217 goog.asserts.assert((value >= 0) && (value < 256)); 247 goog.asserts.assert((value >= 0) && (value < 256));
218 this.buffer_.push((value >>> 0) & 0xFF); 248 this.buffer_.push((value >>> 0) & 0xFF);
219 }; 249 };
220 250
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 /** 337 /**
308 * Writes a 64-bit integer to the buffer. Numbers outside the range 338 * Writes a 64-bit integer to the buffer. Numbers outside the range
309 * [-2^63,2^63) will be truncated. 339 * [-2^63,2^63) will be truncated.
310 * @param {number} value The value to write. 340 * @param {number} value The value to write.
311 */ 341 */
312 jspb.BinaryEncoder.prototype.writeInt64 = function(value) { 342 jspb.BinaryEncoder.prototype.writeInt64 = function(value) {
313 goog.asserts.assert(value == Math.floor(value)); 343 goog.asserts.assert(value == Math.floor(value));
314 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && 344 goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
315 (value < jspb.BinaryConstants.TWO_TO_63)); 345 (value < jspb.BinaryConstants.TWO_TO_63));
316 jspb.utils.splitInt64(value); 346 jspb.utils.splitInt64(value);
317 this.writeUint32(jspb.utils.split64Low); 347 this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High);
318 this.writeUint32(jspb.utils.split64High);
319 }; 348 };
320 349
321 350
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
322 /** 365 /**
323 * Writes a single-precision floating point value to the buffer. Numbers 366 * Writes a single-precision floating point value to the buffer. Numbers
324 * requiring more than 32 bits of precision will be truncated. 367 * requiring more than 32 bits of precision will be truncated.
325 * @param {number} value The value to write. 368 * @param {number} value The value to write.
326 */ 369 */
327 jspb.BinaryEncoder.prototype.writeFloat = function(value) { 370 jspb.BinaryEncoder.prototype.writeFloat = function(value) {
328 goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT32_MAX) && 371 goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT32_MAX) &&
329 (value <= jspb.BinaryConstants.FLOAT32_MAX)); 372 (value <= jspb.BinaryConstants.FLOAT32_MAX));
330 jspb.utils.splitFloat32(value); 373 jspb.utils.splitFloat32(value);
331 this.writeUint32(jspb.utils.split64Low); 374 this.writeUint32(jspb.utils.split64Low);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 445
403 446
404 /** 447 /**
405 * Writes a UTF16 Javascript string to the buffer encoded as UTF8. 448 * Writes a UTF16 Javascript string to the buffer encoded as UTF8.
406 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates. 449 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates.
407 * @param {string} value The string to write. 450 * @param {string} value The string to write.
408 * @return {number} The number of bytes used to encode the string. 451 * @return {number} The number of bytes used to encode the string.
409 */ 452 */
410 jspb.BinaryEncoder.prototype.writeString = function(value) { 453 jspb.BinaryEncoder.prototype.writeString = function(value) {
411 var oldLength = this.buffer_.length; 454 var oldLength = this.buffer_.length;
455
456 for (var i = 0; i < value.length; i++) {
457
458 var c = value.charCodeAt(i);
412 459
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) { 460 if (c < 128) {
417 this.buffer_.push(c); 461 this.buffer_.push(c);
418 } else if (c < 2048) { 462 } else if (c < 2048) {
419 this.buffer_.push((c >> 6) | 192); 463 this.buffer_.push((c >> 6) | 192);
420 this.buffer_.push((c & 63) | 128); 464 this.buffer_.push((c & 63) | 128);
421 } else { 465 } else if (c < 65536) {
422 this.buffer_.push((c >> 12) | 224); 466 // Look for surrogates
423 this.buffer_.push(((c >> 6) & 63) | 128); 467 if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) {
424 this.buffer_.push((c & 63) | 128); 468 var second = value.charCodeAt(i + 1);
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 }
425 } 485 }
426 } 486 }
427 487
428 var length = this.buffer_.length - oldLength; 488 var length = this.buffer_.length - oldLength;
429 return length; 489 return length;
430 }; 490 };
OLDNEW
« no previous file with comments | « third_party/protobuf/js/binary/decoder_test.js ('k') | third_party/protobuf/js/binary/proto_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698