From 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Thu, 17 Nov 2016 16:48:38 -0800 Subject: Integrated internal changes from Google --- js/binary/writer.js | 415 +++++++++++++++++++++++++++------------------------- 1 file changed, 214 insertions(+), 201 deletions(-) (limited to 'js/binary/writer.js') diff --git a/js/binary/writer.js b/js/binary/writer.js index 3eb2f1bd..c3009dbb 100644 --- a/js/binary/writer.js +++ b/js/binary/writer.js @@ -434,6 +434,20 @@ jspb.BinaryWriter.prototype.writeZigzagVarint64_ = function(field, value) { }; +/** + * Writes a zigzag varint field to the buffer without range checking. + * @param {number} field The field number. + * @param {string?} value The value to write. + * @private + */ +jspb.BinaryWriter.prototype.writeZigzagVarint64String_ = function( + field, value) { + if (value == null) return; + this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.VARINT); + this.encoder_.writeZigzagVarint64String(value); +}; + + /** * Writes an int32 field to the buffer. Numbers outside the range [-2^31,2^31) * will be truncated. @@ -574,6 +588,20 @@ jspb.BinaryWriter.prototype.writeSint64 = function(field, value) { }; +/** + * Writes a sint64 field to the buffer. Numbers outside the range [-2^63,2^63) + * will be truncated. + * @param {number} field The field number. + * @param {string?} value The decimal string to write. + */ +jspb.BinaryWriter.prototype.writeSint64String = function(field, value) { + if (value == null) return; + goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) && + (value < jspb.BinaryConstants.TWO_TO_63)); + this.writeZigzagVarint64String_(field, value); +}; + + /** * Writes a fixed32 field to the buffer. Numbers outside the range [0,2^32) * will be truncated. @@ -604,6 +632,19 @@ jspb.BinaryWriter.prototype.writeFixed64 = function(field, value) { }; +/** + * Writes a fixed64 field (with value as a string) to the buffer. + * @param {number} field The field number. + * @param {string?} value The value to write. + */ +jspb.BinaryWriter.prototype.writeFixed64String = function(field, value) { + if (value == null) return; + var num = jspb.arith.UInt64.fromString(value); + this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); + this.encoder_.writeSplitFixed64(num.lo, num.hi); +}; + + /** * Writes a sfixed32 field to the buffer. Numbers outside the range * [-2^31,2^31) will be truncated. @@ -634,6 +675,20 @@ jspb.BinaryWriter.prototype.writeSfixed64 = function(field, value) { }; +/** + * Writes a sfixed64 string field to the buffer. Numbers outside the range + * [-2^63,2^63) will be truncated. + * @param {number} field The field number. + * @param {string?} value The value to write. + */ +jspb.BinaryWriter.prototype.writeSfixed64String = function(field, value) { + if (value == null) return; + var num = jspb.arith.Int64.fromString(value); + this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.FIXED64); + this.encoder_.writeSplitFixed64(num.lo, num.hi); +}; + + /** * Writes a single-precision floating point field to the buffer. Numbers * requiring more than 32 bits of precision will be truncated. @@ -796,28 +851,11 @@ jspb.BinaryWriter.prototype.writeVarintHash64 = function(field, value) { /** - * Writes an array of numbers to the buffer as a repeated varint field. + * Writes an array of numbers to the buffer as a repeated 32-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. - * @private */ -jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_ = - function(field, value) { - if (value == null) return; - for (var i = 0; i < value.length; i++) { - this.writeUnsignedVarint32_(field, value[i]); - } -}; - - -/** - * Writes an array of numbers to the buffer as a repeated varint field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - * @private - */ -jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_ = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedInt32 = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { this.writeSignedVarint32_(field, value[i]); @@ -826,28 +864,25 @@ jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_ = /** - * Writes an array of numbers to the buffer as a repeated varint field. + * Writes an array of numbers formatted as strings to the buffer as a repeated + * 32-bit int field. * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - * @private + * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint64_ = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedInt32String = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeUnsignedVarint64_(field, value[i]); + this.writeInt32String(field, value[i]); } }; /** - * Writes an array of numbers to the buffer as a repeated varint field. + * Writes an array of numbers to the buffer as a repeated 64-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. - * @private */ -jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_ = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedInt64 = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { this.writeSignedVarint64_(field, value[i]); @@ -856,147 +891,112 @@ jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_ = /** - * Writes an array of numbers to the buffer as a repeated zigzag field. + * Writes an array of numbers formatted as strings to the buffer as a repeated + * 64-bit int field. * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - * @private + * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedZigzag32_ = function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedInt64String = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeZigzagVarint32_(field, value[i]); + this.writeInt64String(field, value[i]); } }; /** - * Writes an array of numbers to the buffer as a repeated zigzag field. + * Writes an array numbers to the buffer as a repeated unsigned 32-bit int + * field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. - * @private */ -jspb.BinaryWriter.prototype.writeRepeatedZigzag_ = function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedUint32 = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeZigzagVarint64_(field, value[i]); + this.writeUnsignedVarint32_(field, value[i]); } }; -/** - * Writes an array of numbers to the buffer as a repeated 32-bit int field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - */ -jspb.BinaryWriter.prototype.writeRepeatedInt32 = - jspb.BinaryWriter.prototype.writeRepeatedSignedVarint32_; - - /** * Writes an array of numbers formatted as strings to the buffer as a repeated - * 32-bit int field. + * unsigned 32-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedInt32String = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedUint32String = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeInt32String(field, value[i]); + this.writeUint32String(field, value[i]); } }; /** - * Writes an array of numbers to the buffer as a repeated 64-bit int field. + * Writes an array numbers to the buffer as a repeated unsigned 64-bit int + * field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedInt64 = - jspb.BinaryWriter.prototype.writeRepeatedSignedVarint64_; - - -/** - * Writes an array of numbers formatted as strings to the buffer as a repeated - * 64-bit int field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - */ -jspb.BinaryWriter.prototype.writeRepeatedInt64String = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedUint64 = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeInt64String(field, value[i]); + this.writeUnsignedVarint64_(field, value[i]); } }; -/** - * Writes an array numbers to the buffer as a repeated unsigned 32-bit int - * field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - */ -jspb.BinaryWriter.prototype.writeRepeatedUint32 = - jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint32_; - - /** * Writes an array of numbers formatted as strings to the buffer as a repeated - * unsigned 32-bit int field. + * unsigned 64-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedUint32String = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedUint64String = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeUint32String(field, value[i]); + this.writeUint64String(field, value[i]); } }; /** - * Writes an array numbers to the buffer as a repeated unsigned 64-bit int - * field. + * Writes an array numbers to the buffer as a repeated signed 32-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedUint64 = - jspb.BinaryWriter.prototype.writeRepeatedUnsignedVarint64_; - - -/** - * Writes an array of numbers formatted as strings to the buffer as a repeated - * unsigned 64-bit int field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - */ -jspb.BinaryWriter.prototype.writeRepeatedUint64String = - function(field, value) { +jspb.BinaryWriter.prototype.writeRepeatedSint32 = function(field, value) { if (value == null) return; for (var i = 0; i < value.length; i++) { - this.writeUint64String(field, value[i]); + this.writeZigzagVarint32_(field, value[i]); } }; /** - * Writes an array numbers to the buffer as a repeated signed 32-bit int field. + * Writes an array numbers to the buffer as a repeated signed 64-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedSint32 = - jspb.BinaryWriter.prototype.writeRepeatedZigzag32_; +jspb.BinaryWriter.prototype.writeRepeatedSint64 = function(field, value) { + if (value == null) return; + for (var i = 0; i < value.length; i++) { + this.writeZigzagVarint64_(field, value[i]); + } +}; /** * Writes an array numbers to the buffer as a repeated signed 64-bit int field. * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. + * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writeRepeatedSint64 = - jspb.BinaryWriter.prototype.writeRepeatedZigzag_; +jspb.BinaryWriter.prototype.writeRepeatedSint64String = function(field, value) { + if (value == null) return; + for (var i = 0; i < value.length; i++) { + this.writeZigzagVarint64String_(field, value[i]); + } +}; /** @@ -1027,6 +1027,21 @@ jspb.BinaryWriter.prototype.writeRepeatedFixed64 = function(field, value) { }; +/** + * Writes an array of numbers to the buffer as a repeated fixed64 field. This + * works for both signed and unsigned fixed64s. + * @param {number} field The field number. + * @param {?Array.} value The array of decimal strings to write. + */ +jspb.BinaryWriter.prototype.writeRepeatedFixed64String = function( + field, value) { + if (value == null) return; + for (var i = 0; i < value.length; i++) { + this.writeFixed64String(field, value[i]); + } +}; + + /** * Writes an array of numbers to the buffer as a repeated sfixed32 field. * @param {number} field The field number. @@ -1053,6 +1068,20 @@ jspb.BinaryWriter.prototype.writeRepeatedSfixed64 = function(field, value) { }; +/** + * Writes an array of decimal strings to the buffer as a repeated sfixed64 + * field. + * @param {number} field The field number. + * @param {?Array.} value The array of decimal strings to write. + */ +jspb.BinaryWriter.prototype.writeRepeatedSfixed64String = function(field, value) { + if (value == null) return; + for (var i = 0; i < value.length; i++) { + this.writeSfixed64String(field, value[i]); + } +}; + + /** * Writes an array of numbers to the buffer as a repeated float field. * @param {number} field The field number. @@ -1203,151 +1232,127 @@ jspb.BinaryWriter.prototype.writeRepeatedVarintHash64 = /** - * Writes an array of numbers to the buffer as a packed varint field. + * Writes an array of numbers to the buffer as a packed 32-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. - * @private */ -jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_ = function( - field, value) { +jspb.BinaryWriter.prototype.writePackedInt32 = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeUnsignedVarint32(value[i]); + this.encoder_.writeSignedVarint32(value[i]); } this.endDelimited_(bookmark); }; /** - * Writes an array of numbers to the buffer as a packed varint field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - * @private + * Writes an array of numbers represented as strings to the buffer as a packed + * 32-bit int field. + * @param {number} field + * @param {?Array.} value */ -jspb.BinaryWriter.prototype.writePackedSignedVarint32_ = function( - field, value) { +jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeSignedVarint32(value[i]); + this.encoder_.writeSignedVarint32(parseInt(value[i], 10)); } this.endDelimited_(bookmark); }; /** - * Writes an array of numbers to the buffer as a packed varint field. + * Writes an array of numbers to the buffer as a packed 64-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. - * @private */ -jspb.BinaryWriter.prototype.writePackedUnsignedVarint64_ = function( - field, value) { +jspb.BinaryWriter.prototype.writePackedInt64 = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeUnsignedVarint64(value[i]); + this.encoder_.writeSignedVarint64(value[i]); } this.endDelimited_(bookmark); }; /** - * Writes an array of numbers to the buffer as a packed varint field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - * @private + * Writes an array of numbers represented as strings to the buffer as a packed + * 64-bit int field. + * @param {number} field + * @param {?Array.} value */ -jspb.BinaryWriter.prototype.writePackedSignedVarint64_ = function( - field, value) { +jspb.BinaryWriter.prototype.writePackedInt64String = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeSignedVarint64(value[i]); + var num = jspb.arith.Int64.fromString(value[i]); + this.encoder_.writeSplitVarint64(num.lo, num.hi); } this.endDelimited_(bookmark); }; /** - * Writes an array of numbers to the buffer as a packed zigzag field. + * Writes an array numbers to the buffer as a packed unsigned 32-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. - * @private */ -jspb.BinaryWriter.prototype.writePackedZigzag32_ = function(field, value) { +jspb.BinaryWriter.prototype.writePackedUint32 = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeZigzagVarint32(value[i]); + this.encoder_.writeUnsignedVarint32(value[i]); } this.endDelimited_(bookmark); }; /** - * Writes an array of numbers to the buffer as a packed zigzag field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - * @private + * Writes an array of numbers represented as strings to the buffer as a packed + * unsigned 32-bit int field. + * @param {number} field + * @param {?Array.} value */ -jspb.BinaryWriter.prototype.writePackedZigzag64_ = function(field, value) { +jspb.BinaryWriter.prototype.writePackedUint32String = + function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeZigzagVarint64(value[i]); + this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10)); } this.endDelimited_(bookmark); }; /** - * Writes an array of numbers to the buffer as a packed 32-bit int field. + * Writes an array numbers to the buffer as a packed unsigned 64-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writePackedInt32 = - jspb.BinaryWriter.prototype.writePackedSignedVarint32_; - - -/** - * Writes an array of numbers represented as strings to the buffer as a packed - * 32-bit int field. - * @param {number} field - * @param {?Array.} value - */ -jspb.BinaryWriter.prototype.writePackedInt32String = function(field, value) { +jspb.BinaryWriter.prototype.writePackedUint64 = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeSignedVarint32(parseInt(value[i], 10)); + this.encoder_.writeUnsignedVarint64(value[i]); } this.endDelimited_(bookmark); }; -/** - * Writes an array of numbers to the buffer as a packed 64-bit int field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - */ -jspb.BinaryWriter.prototype.writePackedInt64 = - jspb.BinaryWriter.prototype.writePackedSignedVarint64_; - - /** * Writes an array of numbers represented as strings to the buffer as a packed - * 64-bit int field. + * unsigned 64-bit int field. * @param {number} field * @param {?Array.} value */ -jspb.BinaryWriter.prototype.writePackedInt64String = +jspb.BinaryWriter.prototype.writePackedUint64String = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - var num = jspb.arith.Int64.fromString(value[i]); + var num = jspb.arith.UInt64.fromString(value[i]); this.encoder_.writeSplitVarint64(num.lo, num.hi); } this.endDelimited_(bookmark); @@ -1355,74 +1360,50 @@ jspb.BinaryWriter.prototype.writePackedInt64String = /** - * Writes an array numbers to the buffer as a packed unsigned 32-bit int field. + * Writes an array numbers to the buffer as a packed signed 32-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writePackedUint32 = - jspb.BinaryWriter.prototype.writePackedUnsignedVarint32_; - - -/** - * Writes an array of numbers represented as strings to the buffer as a packed - * unsigned 32-bit int field. - * @param {number} field - * @param {?Array.} value - */ -jspb.BinaryWriter.prototype.writePackedUint32String = - function(field, value) { +jspb.BinaryWriter.prototype.writePackedSint32 = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - this.encoder_.writeUnsignedVarint32(parseInt(value[i], 10)); + this.encoder_.writeZigzagVarint32(value[i]); } this.endDelimited_(bookmark); }; /** - * Writes an array numbers to the buffer as a packed unsigned 64-bit int field. + * Writes an array of numbers to the buffer as a packed signed 64-bit int field. * @param {number} field The field number. * @param {?Array.} value The array of ints to write. */ -jspb.BinaryWriter.prototype.writePackedUint64 = - jspb.BinaryWriter.prototype.writePackedUnsignedVarint64_; - - -/** - * Writes an array of numbers represented as strings to the buffer as a packed - * unsigned 64-bit int field. - * @param {number} field - * @param {?Array.} value - */ -jspb.BinaryWriter.prototype.writePackedUint64String = - function(field, value) { +jspb.BinaryWriter.prototype.writePackedSint64 = function(field, value) { if (value == null || !value.length) return; var bookmark = this.beginDelimited_(field); for (var i = 0; i < value.length; i++) { - var num = jspb.arith.UInt64.fromString(value[i]); - this.encoder_.writeSplitVarint64(num.lo, num.hi); + this.encoder_.writeZigzagVarint64(value[i]); } this.endDelimited_(bookmark); }; /** - * Writes an array numbers to the buffer as a packed signed 32-bit int field. + * Writes an array of decimal strings to the buffer as a packed signed 64-bit + * int field. * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. + * @param {?Array.} value The array of decimal strings to write. */ -jspb.BinaryWriter.prototype.writePackedSint32 = - jspb.BinaryWriter.prototype.writePackedZigzag32_; - - -/** - * Writes an array numbers to the buffer as a packed signed 64-bit int field. - * @param {number} field The field number. - * @param {?Array.} value The array of ints to write. - */ -jspb.BinaryWriter.prototype.writePackedSint64 = - jspb.BinaryWriter.prototype.writePackedZigzag64_; +jspb.BinaryWriter.prototype.writePackedSint64String = function(field, value) { + if (value == null || !value.length) return; + var bookmark = this.beginDelimited_(field); + for (var i = 0; i < value.length; i++) { + // TODO(haberman): make lossless + this.encoder_.writeZigzagVarint64(parseInt(value[i], 10)); + } + this.endDelimited_(bookmark); +}; /** @@ -1455,6 +1436,23 @@ jspb.BinaryWriter.prototype.writePackedFixed64 = function(field, value) { }; +/** + * Writes an array of numbers represented as strings to the buffer as a packed + * fixed64 field. + * @param {number} field The field number. + * @param {?Array.} value The array of strings to write. + */ +jspb.BinaryWriter.prototype.writePackedFixed64String = function(field, value) { + if (value == null || !value.length) return; + this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); + this.encoder_.writeUnsignedVarint32(value.length * 8); + for (var i = 0; i < value.length; i++) { + var num = jspb.arith.UInt64.fromString(value[i]); + this.encoder_.writeSplitFixed64(num.lo, num.hi); + } +}; + + /** * Writes an array of numbers to the buffer as a packed sfixed32 field. * @param {number} field The field number. @@ -1485,6 +1483,21 @@ jspb.BinaryWriter.prototype.writePackedSfixed64 = function(field, value) { }; +/** + * Writes an array of numbers to the buffer as a packed sfixed64 field. + * @param {number} field The field number. + * @param {?Array.} value The array of decimal strings to write. + */ +jspb.BinaryWriter.prototype.writePackedSfixed64String = function(field, value) { + if (value == null || !value.length) return; + this.writeFieldHeader_(field, jspb.BinaryConstants.WireType.DELIMITED); + this.encoder_.writeUnsignedVarint32(value.length * 8); + for (var i = 0; i < value.length; i++) { + this.encoder_.writeInt64String(value[i]); + } +}; + + /** * Writes an array of numbers to the buffer as a packed float field. * @param {number} field The field number. -- cgit v1.2.3