Simplified the argument check in BitBuffer.appendBits() by tightening the input range, also removed some parentheses.

pull/16/head
Project Nayuki 7 years ago
parent 7e512971df
commit 97e0388cb7

@ -39,7 +39,7 @@ std::vector<std::uint8_t> BitBuffer::getBytes() const {
void BitBuffer::appendBits(std::uint32_t val, int len) { void BitBuffer::appendBits(std::uint32_t val, int len) {
if (len < 0 || len > 32 || (len < 32 && (val >> len) != 0)) if (len < 0 || len > 31 || val >> len != 0)
throw "Value out of range"; throw "Value out of range";
for (int i = len - 1; i >= 0; i--) // Append bit by bit for (int i = len - 1; i >= 0; i--) // Append bit by bit
this->push_back(((val >> i) & 1) != 0); this->push_back(((val >> i) & 1) != 0);

@ -77,7 +77,7 @@ public final class BitBuffer implements Cloneable {
// Appends the given number of bits of the given value to this sequence. // Appends the given number of bits of the given value to this sequence.
// If 0 <= len <= 31, then this requires 0 <= val < 2^len. // If 0 <= len <= 31, then this requires 0 <= val < 2^len.
public void appendBits(int val, int len) { public void appendBits(int val, int len) {
if (len < 0 || len > 32 || len < 32 && (val >>> len) != 0) if (len < 0 || len > 31 || val >>> len != 0)
throw new IllegalArgumentException("Value out of range"); throw new IllegalArgumentException("Value out of range");
for (int i = len - 1; i >= 0; i--, bitLength++) // Append bit by bit for (int i = len - 1; i >= 0; i--, bitLength++) // Append bit by bit
data.set(bitLength, ((val >>> i) & 1) != 0); data.set(bitLength, ((val >>> i) & 1) != 0);

@ -1006,7 +1006,7 @@ var qrcodegen = new function() {
// Appends the given number of bits of the given value to this sequence. // Appends the given number of bits of the given value to this sequence.
// If 0 <= len <= 31, then this requires 0 <= val < 2^len. // If 0 <= len <= 31, then this requires 0 <= val < 2^len.
this.appendBits = function(val, len) { this.appendBits = function(val, len) {
if (len < 0 || len > 32 || len < 32 && (val >>> len) != 0) if (len < 0 || len > 31 || val >>> len != 0)
throw "Value out of range"; throw "Value out of range";
for (var i = len - 1; i >= 0; i--) // Append bit by bit for (var i = len - 1; i >= 0; i--) // Append bit by bit
this.push((val >>> i) & 1); this.push((val >>> i) & 1);

Loading…
Cancel
Save