From af6a247121d3729a445f790ac2aa08ce7279f11f Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Thu, 4 Oct 2018 19:26:21 +0000 Subject: [PATCH] Tweaked logic in QrSegment.makeEci() to reduce redundancy, in all languages except Rust. --- c/qrcodegen.c | 8 +++++--- cpp/QrSegment.cpp | 8 +++++--- java/io/nayuki/qrcodegen/QrSegment.java | 8 +++++--- javascript/qrcodegen.js | 8 +++++--- python/qrcodegen.py | 8 +++++--- typescript/qrcodegen.ts | 8 +++++--- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index ffdd9bd..77a0bda 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -829,14 +829,16 @@ struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) { result.mode = qrcodegen_Mode_ECI; result.numChars = 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])); 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])); appendBitsToBuffer(2, 2, 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])); appendBitsToBuffer(6, 3, buf, &result.bitLength); appendBitsToBuffer(assignVal >> 10, 11, buf, &result.bitLength); diff --git a/cpp/QrSegment.cpp b/cpp/QrSegment.cpp index 8d1bde3..64d77f7 100644 --- a/cpp/QrSegment.cpp +++ b/cpp/QrSegment.cpp @@ -135,12 +135,14 @@ vector QrSegment::makeSegments(const char *text) { QrSegment QrSegment::makeEci(long assignVal) { 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); - else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { + else if (assignVal < (1 << 14)) { bb.appendBits(2, 2); bb.appendBits(assignVal, 14); - } else if ((1 << 14) <= assignVal && assignVal < 1000000L) { + } else if (assignVal < 1000000L) { bb.appendBits(6, 3); bb.appendBits(assignVal, 21); } else diff --git a/java/io/nayuki/qrcodegen/QrSegment.java b/java/io/nayuki/qrcodegen/QrSegment.java index ceee566..264c735 100644 --- a/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/io/nayuki/qrcodegen/QrSegment.java @@ -137,12 +137,14 @@ public final class QrSegment { */ public static QrSegment makeEci(int assignVal) { 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); - else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { + else if (assignVal < (1 << 14)) { bb.appendBits(2, 2); bb.appendBits(assignVal, 14); - } else if ((1 << 14) <= assignVal && assignVal < 1000000) { + } else if (assignVal < 1000000) { bb.appendBits(6, 3); bb.appendBits(assignVal, 21); } else diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 9c9e507..f1d2323 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -792,12 +792,14 @@ var qrcodegen = new function() { */ this.QrSegment.makeEci = function(assignVal) { 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); - else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { + else if (assignVal < (1 << 14)) { bb.appendBits(2, 2); bb.appendBits(assignVal, 14); - } else if ((1 << 14) <= assignVal && assignVal < 1000000) { + } else if (assignVal < 1000000) { bb.appendBits(6, 3); bb.appendBits(assignVal, 21); } else diff --git a/python/qrcodegen.py b/python/qrcodegen.py index a971d7a..8edd562 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -661,12 +661,14 @@ class QrSegment(object): """Returns a segment representing an Extended Channel Interpretation (ECI) designator with the given assignment value.""" 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) - elif (1 << 7) <= assignval < (1 << 14): + elif assignval < (1 << 14): bb.append_bits(2, 2) bb.append_bits(assignval, 14) - elif (1 << 14) <= assignval < 1000000: + elif assignval < 1000000: bb.append_bits(6, 3) bb.append_bits(assignval, 21) else: diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index 58bd14b..ab6d1ef 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -720,12 +720,14 @@ namespace qrcodegen { // (ECI) designator with the given assignment value. public static makeEci(assignVal: int): QrSegment { 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); - else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { + else if (assignVal < (1 << 14)) { bb.appendBits(2, 2); bb.appendBits(assignVal, 14); - } else if ((1 << 14) <= assignVal && assignVal < 1000000) { + } else if (assignVal < 1000000) { bb.appendBits(6, 3); bb.appendBits(assignVal, 21); } else