Project import generated by Copybara

PiperOrigin-RevId: 299970447
This commit is contained in:
Rafi Kamal 2020-03-09 17:00:07 -07:00 committed by Copybara-Service
parent 460376190b
commit 4fa3c8e883
4 changed files with 11 additions and 11 deletions

View File

@ -279,15 +279,14 @@ class BufferDecoder {
}
/**
* Skips over a varint at a given index.
* @param {number} index Start of the data.
* Skips over a varint from the current cursor position.
* @package
*/
skipVarint(index) {
this.cursor_ = index;
skipVarint() {
const startIndex = this.cursor_;
while (this.dataView_.getUint8(this.cursor_++) & 0x80) {
}
checkCriticalPositionIndex(this.cursor_, index + 10);
checkCriticalPositionIndex(this.cursor_, startIndex + 10);
}
/**

View File

@ -41,7 +41,7 @@ describe('Skip varint does', () => {
it('skip a varint', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01));
bufferDecoder.skipVarint(0);
bufferDecoder.skipVarint();
expect(bufferDecoder.cursor()).toBe(1);
});
@ -50,13 +50,13 @@ describe('Skip varint does', () => {
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
if (CHECK_CRITICAL_STATE) {
expect(() => bufferDecoder.skipVarint(0)).toThrow();
expect(() => bufferDecoder.skipVarint()).toThrow();
} else {
// Note in unchecked mode we produce invalid output for invalid inputs.
// This test just documents our behavior in those cases.
// These values might change at any point and are not considered
// what the implementation should be doing here.
bufferDecoder.skipVarint(0);
bufferDecoder.skipVarint();
expect(bufferDecoder.cursor()).toBe(11);
}
});
@ -64,7 +64,7 @@ describe('Skip varint does', () => {
it('fail when varint is beyond end of underlying array', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x80));
expect(() => bufferDecoder.skipVarint(0)).toThrow();
expect(() => bufferDecoder.skipVarint()).toThrow();
});
});

View File

@ -85,7 +85,7 @@ function skipField_(bufferDecoder, wireType, fieldNumber) {
case WireType.VARINT:
checkCriticalElementIndex(
bufferDecoder.cursor(), bufferDecoder.endIndex());
bufferDecoder.skipVarint(bufferDecoder.cursor());
bufferDecoder.skipVarint();
return false;
case WireType.FIXED64:
bufferDecoder.skip(8);

View File

@ -451,7 +451,8 @@ class Writer {
getLength_(bufferDecoder, start, wireType) {
switch (wireType) {
case WireType.VARINT:
bufferDecoder.skipVarint(start);
bufferDecoder.setCursor(start);
bufferDecoder.skipVarint();
return bufferDecoder.cursor() - start;
case WireType.FIXED64:
return 8;