Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the support to Long string #508 #509

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/util/longbits.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,23 @@ LongBits.fromNumber = function fromNumber(value) {

/**
* Constrcuts new long bits from a number or long.
* @param {Long|number} value Value
* @param {Long|number|string} value Value
* @returns {util.LongBits} Instance
*/
LongBits.from = function from(value) {
return typeof value === 'number'
? LongBits.fromNumber(value)
: new LongBits(value.low >>> 0, value.high >>> 0);
var type = typeof value, result=zero;
if(type === 'number')
result = LongBits.fromNumber(value);
else
{
if(type === 'string' && util.Long)
value = util.Long.fromString(value);

if(value.low || value.high)
result = new LongBits(value.low >>> 0, value.high >>> 0);
}

return result;
};

/**
Expand Down
15 changes: 5 additions & 10 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,38 +214,33 @@ function writeVarint64(buf, pos, val) {

/**
* Writes an unsigned 64 bit value as a varint.
* @param {Long|number} value Value to write
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.uint64 = function write_uint64(value) {
var bits;
if (typeof value === 'number')
bits = value ? LongBits.fromNumber(value) : LongBits.zero;
else if (value.low || value.high)
bits = new LongBits(value.low >>> 0, value.high >>> 0);
else
bits = LongBits.zero;
var bits = LongBits.from(value);
return this.push(writeVarint64, bits.length(), bits);
};

/**
* Writes a signed 64 bit value as a varint.
* @function
* @param {Long|number} value Value to write
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.int64 = WriterPrototype.uint64;

/**
* Writes a signed 64 bit value as a varint, zig-zag encoded.
* @param {Long|number} value Value to write
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.sint64 = function sint64(value) {
var bits = LongBits.from(value).zzEncode();
return this.push(writeVarint64, bits.length(), bits);
};


/**
* Writes a boolish value as a varint.
* @param {boolean} value Value to write
Expand Down