Tweaked logic in QrSegment.makeEci() to reduce redundancy, in all languages except Rust.

pull/39/merge
Project Nayuki 6 years ago
parent 55f410cc66
commit af6a247121

@ -829,14 +829,16 @@ struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
result.mode = qrcodegen_Mode_ECI; result.mode = qrcodegen_Mode_ECI;
result.numChars = 0; result.numChars = 0;
result.bitLength = 0; result.bitLength = 0;
if (0 <= assignVal && assignVal < (1 << 7)) { if (assignVal < 0)
assert(false);
else if (assignVal < (1 << 7)) {
memset(buf, 0, 1 * sizeof(buf[0])); memset(buf, 0, 1 * sizeof(buf[0]));
appendBitsToBuffer(assignVal, 8, buf, &result.bitLength); appendBitsToBuffer(assignVal, 8, buf, &result.bitLength);
} else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { } else if (assignVal < (1 << 14)) {
memset(buf, 0, 2 * sizeof(buf[0])); memset(buf, 0, 2 * sizeof(buf[0]));
appendBitsToBuffer(2, 2, buf, &result.bitLength); appendBitsToBuffer(2, 2, buf, &result.bitLength);
appendBitsToBuffer(assignVal, 14, buf, &result.bitLength); appendBitsToBuffer(assignVal, 14, buf, &result.bitLength);
} else if ((1 << 14) <= assignVal && assignVal < 1000000L) { } else if (assignVal < 1000000L) {
memset(buf, 0, 3 * sizeof(buf[0])); memset(buf, 0, 3 * sizeof(buf[0]));
appendBitsToBuffer(6, 3, buf, &result.bitLength); appendBitsToBuffer(6, 3, buf, &result.bitLength);
appendBitsToBuffer(assignVal >> 10, 11, buf, &result.bitLength); appendBitsToBuffer(assignVal >> 10, 11, buf, &result.bitLength);

@ -135,12 +135,14 @@ vector<QrSegment> QrSegment::makeSegments(const char *text) {
QrSegment QrSegment::makeEci(long assignVal) { QrSegment QrSegment::makeEci(long assignVal) {
BitBuffer bb; BitBuffer bb;
if (0 <= assignVal && assignVal < (1 << 7)) if (assignVal < 0)
throw std::domain_error("ECI assignment value out of range");
else if (assignVal < (1 << 7))
bb.appendBits(assignVal, 8); bb.appendBits(assignVal, 8);
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { else if (assignVal < (1 << 14)) {
bb.appendBits(2, 2); bb.appendBits(2, 2);
bb.appendBits(assignVal, 14); bb.appendBits(assignVal, 14);
} else if ((1 << 14) <= assignVal && assignVal < 1000000L) { } else if (assignVal < 1000000L) {
bb.appendBits(6, 3); bb.appendBits(6, 3);
bb.appendBits(assignVal, 21); bb.appendBits(assignVal, 21);
} else } else

@ -137,12 +137,14 @@ public final class QrSegment {
*/ */
public static QrSegment makeEci(int assignVal) { public static QrSegment makeEci(int assignVal) {
BitBuffer bb = new BitBuffer(); BitBuffer bb = new BitBuffer();
if (0 <= assignVal && assignVal < (1 << 7)) if (assignVal < 0)
throw new IllegalArgumentException("ECI assignment value out of range");
else if (assignVal < (1 << 7))
bb.appendBits(assignVal, 8); bb.appendBits(assignVal, 8);
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { else if (assignVal < (1 << 14)) {
bb.appendBits(2, 2); bb.appendBits(2, 2);
bb.appendBits(assignVal, 14); bb.appendBits(assignVal, 14);
} else if ((1 << 14) <= assignVal && assignVal < 1000000) { } else if (assignVal < 1000000) {
bb.appendBits(6, 3); bb.appendBits(6, 3);
bb.appendBits(assignVal, 21); bb.appendBits(assignVal, 21);
} else } else

@ -792,12 +792,14 @@ var qrcodegen = new function() {
*/ */
this.QrSegment.makeEci = function(assignVal) { this.QrSegment.makeEci = function(assignVal) {
var bb = new BitBuffer(); var bb = new BitBuffer();
if (0 <= assignVal && assignVal < (1 << 7)) if (assignVal < 0)
throw "ECI assignment value out of range";
else if (assignVal < (1 << 7))
bb.appendBits(assignVal, 8); bb.appendBits(assignVal, 8);
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { else if (assignVal < (1 << 14)) {
bb.appendBits(2, 2); bb.appendBits(2, 2);
bb.appendBits(assignVal, 14); bb.appendBits(assignVal, 14);
} else if ((1 << 14) <= assignVal && assignVal < 1000000) { } else if (assignVal < 1000000) {
bb.appendBits(6, 3); bb.appendBits(6, 3);
bb.appendBits(assignVal, 21); bb.appendBits(assignVal, 21);
} else } else

@ -661,12 +661,14 @@ class QrSegment(object):
"""Returns a segment representing an Extended Channel Interpretation """Returns a segment representing an Extended Channel Interpretation
(ECI) designator with the given assignment value.""" (ECI) designator with the given assignment value."""
bb = _BitBuffer() bb = _BitBuffer()
if 0 <= assignval < (1 << 7): if assignval < 0:
raise ValueError("ECI assignment value out of range")
elif assignval < (1 << 7):
bb.append_bits(assignval, 8) bb.append_bits(assignval, 8)
elif (1 << 7) <= assignval < (1 << 14): elif assignval < (1 << 14):
bb.append_bits(2, 2) bb.append_bits(2, 2)
bb.append_bits(assignval, 14) bb.append_bits(assignval, 14)
elif (1 << 14) <= assignval < 1000000: elif assignval < 1000000:
bb.append_bits(6, 3) bb.append_bits(6, 3)
bb.append_bits(assignval, 21) bb.append_bits(assignval, 21)
else: else:

@ -720,12 +720,14 @@ namespace qrcodegen {
// (ECI) designator with the given assignment value. // (ECI) designator with the given assignment value.
public static makeEci(assignVal: int): QrSegment { public static makeEci(assignVal: int): QrSegment {
let bb = new BitBuffer(); let bb = new BitBuffer();
if (0 <= assignVal && assignVal < (1 << 7)) if (assignVal < 0)
throw "ECI assignment value out of range";
else if (assignVal < (1 << 7))
bb.appendBits(assignVal, 8); bb.appendBits(assignVal, 8);
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { else if (assignVal < (1 << 14)) {
bb.appendBits(2, 2); bb.appendBits(2, 2);
bb.appendBits(assignVal, 14); bb.appendBits(assignVal, 14);
} else if ((1 << 14) <= assignVal && assignVal < 1000000) { } else if (assignVal < 1000000) {
bb.appendBits(6, 3); bb.appendBits(6, 3);
bb.appendBits(assignVal, 21); bb.appendBits(assignVal, 21);
} else } else

Loading…
Cancel
Save