Skip to content

Commit ddb6283

Browse files
committed
Long overdue varint32 rework
1 parent 5d089a3 commit ddb6283

12 files changed

+310
-362
lines changed

dist/ByteBufferAB.js

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,22 @@
312312
ByteBuffer.isByteBuffer = function(bb) {
313313
return (bb && bb["__isByteBuffer__"]) === true;
314314
};
315+
// TODO
316+
317+
/**
318+
* Registers an additional string encoding.
319+
* @param {string} name Short name of the encoding (i.e. "utf8")
320+
* @param {function(!ByteBuffer, string, number)} fromString A function capable of decoding a string using this encoding
321+
* to a ByteBuffer
322+
* @param {function(!ByteBuffer, number, number)} toString A function capable of encoding a string using this encoding
323+
* from a ByteBuffer
324+
*/
325+
ByteBuffer.registerEncoding = function(name, fromString, toString) {
326+
ByteBuffer.ENCODINGS[name] = {
327+
fromString: fromString,
328+
toString: toString
329+
};
330+
};
315331
/**
316332
* Gets the backing buffer type.
317333
* @returns {Function} `Buffer` under node.js, `ArrayBuffer` in the browser (classes)
@@ -1526,46 +1542,22 @@
15261542
if (offset > capacity10)
15271543
this.resize((capacity10 *= 2) > offset ? capacity10 : offset);
15281544
offset -= size;
1529-
// ref: http://code.google.com/searchframe#WTeibokF6gE/trunk/src/google/protobuf/io/coded_stream.cc
1530-
this.view[offset] = b = value | 0x80;
15311545
value >>>= 0;
1532-
if (value >= 1 << 7) {
1533-
b = (value >> 7) | 0x80;
1534-
this.view[offset+1] = b;
1535-
if (value >= 1 << 14) {
1536-
b = (value >> 14) | 0x80;
1537-
this.view[offset+2] = b;
1538-
if (value >= 1 << 21) {
1539-
b = (value >> 21) | 0x80;
1540-
this.view[offset+3] = b;
1541-
if (value >= 1 << 28) {
1542-
this.view[offset+4] = (value >> 28) & 0x0F;
1543-
size = 5;
1544-
} else {
1545-
this.view[offset+3] = b & 0x7F;
1546-
size = 4;
1547-
}
1548-
} else {
1549-
this.view[offset+2] = b & 0x7F;
1550-
size = 3;
1551-
}
1552-
} else {
1553-
this.view[offset+1] = b & 0x7F;
1554-
size = 2;
1555-
}
1556-
} else {
1557-
this.view[offset] = b & 0x7F;
1558-
size = 1;
1546+
while (value >= 0x80) {
1547+
b = (value & 0x7f) | 0x80;
1548+
this.view[offset++] = b;
1549+
value >>>= 7;
15591550
}
1551+
this.view[offset++] = value;
15601552
if (relative) {
1561-
this.offset += size;
1553+
this.offset = offset;
15621554
return this;
15631555
}
15641556
return size;
15651557
};
15661558

15671559
/**
1568-
* Writes a zig-zag encoded 32bit base 128 variable-length integer.
1560+
* Writes a zig-zag encoded (signed) 32bit base 128 variable-length integer.
15691561
* @param {number} value Value to write
15701562
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
15711563
* written if omitted.
@@ -1596,36 +1588,33 @@
15961588
if (offset < 0 || offset + 1 > this.buffer.byteLength)
15971589
throw RangeError("Illegal offset: 0 <= "+offset+" (+"+1+") <= "+this.buffer.byteLength);
15981590
}
1599-
// ref: src/google/protobuf/io/coded_stream.cc
1600-
var size = 0,
1591+
var c = 0,
16011592
value = 0 >>> 0,
1602-
temp,
1603-
ioffset;
1593+
b;
16041594
do {
1605-
ioffset = offset+size;
1606-
if (!this.noAssert && ioffset > this.limit) {
1595+
if (!this.noAssert && offset > this.limit) {
16071596
var err = Error("Truncated");
16081597
err['truncated'] = true;
16091598
throw err;
16101599
}
1611-
temp = this.view[ioffset];
1612-
if (size < 5)
1613-
value |= ((temp&0x7F)<<(7*size)) >>> 0;
1614-
++size;
1615-
} while ((temp & 0x80) === 0x80);
1616-
value = value | 0; // Make sure to discard the higher order bits
1600+
b = this.view[offset++];
1601+
if (c < 5)
1602+
value |= (b & 0x7f)<<(7*c);
1603+
++c;
1604+
} while ((b & 0x80) !== 0);
1605+
value |= 0;
16171606
if (relative) {
1618-
this.offset += size;
1607+
this.offset = offset;
16191608
return value;
16201609
}
16211610
return {
16221611
"value": value,
1623-
"length": size
1612+
"length": c
16241613
};
16251614
};
16261615

16271616
/**
1628-
* Reads a zig-zag encoded 32bit base 128 variable-length integer.
1617+
* Reads a zig-zag encoded (signed) 32bit base 128 variable-length integer.
16291618
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
16301619
* written if omitted.
16311620
* @returns {number|!{value: number, length: number}} The value read if offset is omitted, else the value read
@@ -1717,6 +1706,9 @@
17171706
return value.shiftRightUnsigned(1).xor(value.and(Long.ONE).toSigned().negate()).toSigned();
17181707
};
17191708

1709+
var Long0x80 = Long.fromNumber(0x80),
1710+
Long0x7f = Long.fromNumber(0x7f);
1711+
17201712
/**
17211713
* Writes a 64bit base 128 variable-length integer.
17221714
* @param {number|Long} value Value to write

0 commit comments

Comments
 (0)