Updated class BitBuffer's documentation comments, in all languages.

pull/39/merge
Project Nayuki 7 years ago
parent 1f2de4bbba
commit 29479efedf

@ -176,8 +176,8 @@ bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcod
} }
// Appends the given sequence of bits to the given byte-based bit buffer, increasing the bit length. // Appends the given number of low-order bits of the given value to the given byte-based
// Requires 0 <= numBits <= 16 and 0 <= val < 2^numBits. // bit buffer, increasing the bit length. Requires 0 <= numBits <= 16 and val < 2^numBits.
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) { testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {
assert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0); assert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0);
for (int i = numBits - 1; i >= 0; i--, (*bitLen)++) for (int i = numBits - 1; i >= 0; i--, (*bitLen)++)

@ -30,7 +30,7 @@
namespace qrcodegen { namespace qrcodegen {
/* /*
* An appendable sequence of bits (0s and 1s). * An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
*/ */
class BitBuffer final : public std::vector<bool> { class BitBuffer final : public std::vector<bool> {
@ -43,13 +43,13 @@ class BitBuffer final : public std::vector<bool> {
/*---- Methods ----*/ /*---- Methods ----*/
// Packs this buffer's bits into bytes in big endian, // Returns a vector representing this buffer's bits packed into bytes in big endian. If the
// padding with '0' bit values, and returns the new vector. // bit length isn't a multiple of 8, then the remaining bits of the final byte are all '0'.
public: std::vector<std::uint8_t> getBytes() const; public: std::vector<std::uint8_t> getBytes() const;
// Appends the given number of low bits of the given value to // Appends the given number of low-order bits of the given value
// this sequence. Requires 0 <= len <= 31 and 0 <= val < 2^len. // to this buffer. Requires 0 <= len <= 31 and val < 2^len.
public: void appendBits(std::uint32_t val, int len); public: void appendBits(std::uint32_t val, int len);
}; };

@ -28,7 +28,7 @@ import java.util.Objects;
/** /**
* An appendable sequence of bits (0s and 1s). * An appendable sequence of bits (0s and 1s). Mainly used by {@link QrSegment}.
*/ */
public final class BitBuffer implements Cloneable { public final class BitBuffer implements Cloneable {
@ -78,9 +78,9 @@ public final class BitBuffer implements Cloneable {
/** /**
* Packs this buffer's bits into bytes in big endian, * Returns an array representing this buffer's bits packed into bytes in big endian. If the
* padding with '0' bit values, and returns the new array. * bit length isn't a multiple of 8, then the remaining bits of the final byte are all '0'.
* @return this sequence as a new array of bytes (not {@code null}) * @return a new byte array (not {@code null}) representing this bit sequence
*/ */
public byte[] getBytes() { public byte[] getBytes() {
byte[] result = new byte[(bitLength + 7) >>> 3]; // Round up to whole byte, won't overflow byte[] result = new byte[(bitLength + 7) >>> 3]; // Round up to whole byte, won't overflow
@ -93,10 +93,10 @@ public final class BitBuffer implements Cloneable {
/** /**
* Appends the specified number of low bits of the specified value to this * Appends the specified number of low-order bits of the specified value to this
* sequence. Requires 0 &#x2264; len &#x2264; 31 and 0 &#x2264; val &lt; 2<sup>len</sup>. * buffer. Requires 0 &#x2264; len &#x2264; 31 and 0 &#x2264; val &lt; 2<sup>len</sup>.
* @param val the value to append * @param val the value to append
* @param len the number of low bits in the value to take * @param len the number of low-order bits in the value to take
* @throws IllegalArgumentException if the value or number of bits is out of range * @throws IllegalArgumentException if the value or number of bits is out of range
* @throws IllegalStateException if appending the data * @throws IllegalStateException if appending the data
* would make bitLength exceed Integer.MAX_VALUE * would make bitLength exceed Integer.MAX_VALUE
@ -112,7 +112,7 @@ public final class BitBuffer implements Cloneable {
/** /**
* Appends the specified bit buffer to this bit buffer. * Appends the content of the specified bit buffer to this buffer.
* @param bb the bit buffer whose data to append (not {@code null}) * @param bb the bit buffer whose data to append (not {@code null})
* @throws NullPointerException if the bit buffer is {@code null} * @throws NullPointerException if the bit buffer is {@code null}
* @throws IllegalStateException if appending the data * @throws IllegalStateException if appending the data
@ -128,8 +128,8 @@ public final class BitBuffer implements Cloneable {
/** /**
* Returns a copy of this bit buffer object. * Returns a new copy of this buffer.
* @return a copy of this bit buffer object * @return a new copy of this buffer (not {@code null})
*/ */
public BitBuffer clone() { public BitBuffer clone() {
try { try {

@ -962,13 +962,13 @@ var qrcodegen = new function() {
/* /*
* A private helper class that represents an appendable sequence of bits (0s and 1s). * A private helper class that represents an appendable sequence of bits (0s and 1s).
* This constructor creates an empty bit buffer (length 0). * Mainly used by QrSegment. This constructor creates an empty bit buffer (length 0).
*/ */
function BitBuffer() { function BitBuffer() {
Array.call(this); Array.call(this);
// Packs this buffer's bits into bytes in big endian, // Returns a new array representing this buffer's bits packed into bytes in big endian. If the
// padding with '0' bit values, and returns the new array. // bit length isn't a multiple of 8, then the remaining bits of the final byte are all '0'.
this.getBytes = function() { this.getBytes = function() {
var result = []; var result = [];
while (result.length * 8 < this.length) while (result.length * 8 < this.length)
@ -979,8 +979,8 @@ var qrcodegen = new function() {
return result; return result;
}; };
// Appends the given number of low bits of the given value to // Appends the given number of low-order bits of the given value
// this sequence. Requires 0 <= len <= 31 and 0 <= val < 2^len. // to this buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
this.appendBits = function(val, len) { this.appendBits = function(val, len) {
if (len < 0 || len > 31 || val >>> len != 0) if (len < 0 || len > 31 || val >>> len != 0)
throw "Value out of range"; throw "Value out of range";

@ -829,19 +829,19 @@ class _ReedSolomonGenerator(object):
class _BitBuffer(list): class _BitBuffer(list):
"""An appendable sequence of bits (0s and 1s).""" """An appendable sequence of bits (0s and 1s). Mainly used by QrSegment."""
def get_bytes(self): def get_bytes(self):
"""Packs this buffer's bits into bytes in big endian, """Returns a new list representing this buffer's bits packed into bytes in big endian. If the
padding with '0' bit values, and returns the new list.""" bit length isn't a multiple of 8, then the remaining bits of the final byte are all '0'."""
result = [0] * ((len(self) + 7) // 8) result = [0] * ((len(self) + 7) // 8)
for (i, bit) in enumerate(self): for (i, bit) in enumerate(self):
result[i >> 3] |= bit << (7 - (i & 7)) result[i >> 3] |= bit << (7 - (i & 7))
return result return result
def append_bits(self, val, n): def append_bits(self, val, n):
"""Appends the given number of low bits of the given value """Appends the given number of low-order bits of the given
to this sequence. Requires n >= 0 and 0 <= val < 2^n.""" value to this buffer. Requires n >= 0 and 0 <= val < 2^n."""
if n < 0 or val >> n != 0: if n < 0 or val >> n != 0:
raise ValueError("Value out of range") raise ValueError("Value out of range")
self.extend(((val >> i) & 1) for i in reversed(range(n))) self.extend(((val >> i) & 1) for i in reversed(range(n)))

@ -1070,13 +1070,13 @@ impl QrSegmentMode {
/*---- Bit buffer functionality ----*/ /*---- Bit buffer functionality ----*/
// An appendable sequence of bits (0s and 1s). // An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
pub struct BitBuffer(pub Vec<bool>); pub struct BitBuffer(pub Vec<bool>);
impl BitBuffer { impl BitBuffer {
// Appends the given number of low bits of the given value // Appends the given number of low-order bits of the given value
// to this sequence. Requires len <= 31 and 0 <= val < 2^len. // to this buffer. Requires len <= 31 and val < 2^len.
pub fn append_bits(&mut self, val: u32, len: u8) { pub fn append_bits(&mut self, val: u32, len: u8) {
assert!(len <= 31 && (val >> len) == 0, "Value out of range"); assert!(len <= 31 && (val >> len) == 0, "Value out of range");
self.0.extend((0 .. len as i32).rev().map(|i| get_bit(val, i))); // Append bit by bit self.0.extend((0 .. len as i32).rev().map(|i| get_bit(val, i))); // Append bit by bit

@ -890,12 +890,13 @@ namespace qrcodegen {
/* /*
* An appendable sequence of bits (0s and 1s). The implicit constructor creates an empty bit buffer (length 0). * An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
* The implicit constructor creates an empty bit buffer (length 0).
*/ */
class BitBuffer extends Array<bit> { class BitBuffer extends Array<bit> {
// Packs this buffer's bits into bytes in big endian, // Returns a new array representing this buffer's bits packed into bytes in big endian. If the
// padding with '0' bit values, and returns the new array. // bit length isn't a multiple of 8, then the remaining bits of the final byte are all '0'.
public getBytes(): Array<byte> { public getBytes(): Array<byte> {
let result: Array<byte> = []; let result: Array<byte> = [];
while (result.length * 8 < this.length) while (result.length * 8 < this.length)
@ -906,8 +907,8 @@ namespace qrcodegen {
} }
// Appends the given number of low bits of the given value to // Appends the given number of low-order bits of the given value
// this sequence. Requires 0 <= len <= 31 and 0 <= val < 2^len. // to this buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
public appendBits(val: int, len: int): void { public appendBits(val: int, len: int): void {
if (len < 0 || len > 31 || val >>> len != 0) if (len < 0 || len > 31 || val >>> len != 0)
throw "Value out of range"; throw "Value out of range";

Loading…
Cancel
Save