Skip to content

Commit

Permalink
Merge pull request #232 from theqabalist/fix/bit-seq-wrong
Browse files Browse the repository at this point in the history
Fix issue where entire (very long) input is passed to bitSeq.
  • Loading branch information
Brian Mock authored Mar 22, 2018
2 parents c1e28fb + 8eb1966 commit 1821f52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/parsimmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ function bitSeq(alignments) {
}

return new Parsimmon(function(input, i) {
if (bytes + i > input.length) {
var newPos = bytes + i;
if (newPos > input.length) {
return makeFailure(i, bytes.toString() + " bytes");
}
return makeSuccess(
i + bytes,
newPos,
reduce(
function(acc, bits) {
var state = consumeBitsFromBuffer(bits, acc.buf);
Expand All @@ -142,7 +143,7 @@ function bitSeq(alignments) {
buf: state.buf
};
},
{ coll: [], buf: input },
{ coll: [], buf: input.slice(i, newPos) },
alignments
).coll
);
Expand Down
11 changes: 11 additions & 0 deletions test/core/bitSeq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ describe("bitSeq", function() {
}, /buffer global/i);
});
});

context("We are bitSeq'ing not at the beginning.", function() {
it("Should still work.", function() {
var b = Buffer.from([0x10, 0xff, 0xff]);
var p = Parsimmon.seqObj(Parsimmon.any, [
"data",
Parsimmon.Binary.bitSeq([3, 5, 5, 3])
]);
assert.deepEqual(p.parse(b).value.data, [7, 31, 31, 7]);
});
});
});

0 comments on commit 1821f52

Please sign in to comment.