Reordered QrSegment.{isNumeric(),isAlphanumeric()} in {C, C++, Rust} versions.

pull/118/head
Project Nayuki 4 years ago
parent 6fb34fddc1
commit 0e80f23c04

@ -805,10 +805,10 @@ static bool getBit(int x, int i) {
/*---- Segment handling ----*/ /*---- Segment handling ----*/
// Public function - see documentation comment in header file. // Public function - see documentation comment in header file.
bool qrcodegen_isAlphanumeric(const char *text) { bool qrcodegen_isNumeric(const char *text) {
assert(text != NULL); assert(text != NULL);
for (; *text != '\0'; text++) { for (; *text != '\0'; text++) {
if (strchr(ALPHANUMERIC_CHARSET, *text) == NULL) if (*text < '0' || *text > '9')
return false; return false;
} }
return true; return true;
@ -816,10 +816,10 @@ bool qrcodegen_isAlphanumeric(const char *text) {
// Public function - see documentation comment in header file. // Public function - see documentation comment in header file.
bool qrcodegen_isNumeric(const char *text) { bool qrcodegen_isAlphanumeric(const char *text) {
assert(text != NULL); assert(text != NULL);
for (; *text != '\0'; text++) { for (; *text != '\0'; text++) {
if (*text < '0' || *text > '9') if (strchr(ALPHANUMERIC_CHARSET, *text) == NULL)
return false; return false;
} }
return true; return true;

@ -230,18 +230,18 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
/* /*
* Tests whether the given string can be encoded as a segment in alphanumeric mode. * Tests whether the given string can be encoded as a segment in numeric mode.
* A string is encodable iff each character is in the following set: 0 to 9, A to Z * A string is encodable iff each character is in the range 0 to 9.
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/ */
bool qrcodegen_isAlphanumeric(const char *text); bool qrcodegen_isNumeric(const char *text);
/* /*
* Tests whether the given string can be encoded as a segment in numeric mode. * Tests whether the given string can be encoded as a segment in alphanumeric mode.
* A string is encodable iff each character is in the range 0 to 9. * A string is encodable iff each character is in the following set: 0 to 9, A to Z
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/ */
bool qrcodegen_isNumeric(const char *text); bool qrcodegen_isAlphanumeric(const char *text);
/* /*

@ -191,19 +191,19 @@ int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
} }
bool QrSegment::isAlphanumeric(const char *text) { bool QrSegment::isNumeric(const char *text) {
for (; *text != '\0'; text++) { for (; *text != '\0'; text++) {
if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr) char c = *text;
if (c < '0' || c > '9')
return false; return false;
} }
return true; return true;
} }
bool QrSegment::isNumeric(const char *text) { bool QrSegment::isAlphanumeric(const char *text) {
for (; *text != '\0'; text++) { for (; *text != '\0'; text++) {
char c = *text; if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)
if (c < '0' || c > '9')
return false; return false;
} }
return true; return true;

@ -133,18 +133,18 @@ class QrSegment final {
/*---- Public static helper functions ----*/ /*---- Public static helper functions ----*/
/* /*
* Tests whether the given string can be encoded as a segment in alphanumeric mode. * Tests whether the given string can be encoded as a segment in numeric mode.
* A string is encodable iff each character is in the following set: 0 to 9, A to Z * A string is encodable iff each character is in the range 0 to 9.
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/ */
public: static bool isAlphanumeric(const char *text); public: static bool isNumeric(const char *text);
/* /*
* Tests whether the given string can be encoded as a segment in numeric mode. * Tests whether the given string can be encoded as a segment in alphanumeric mode.
* A string is encodable iff each character is in the range 0 to 9. * A string is encodable iff each character is in the following set: 0 to 9, A to Z
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/ */
public: static bool isNumeric(const char *text); public: static bool isAlphanumeric(const char *text);

@ -1116,6 +1116,13 @@ impl QrSegment {
} }
// Tests whether the given string can be encoded as a segment in numeric mode.
// A string is encodable iff each character is in the range 0 to 9.
fn is_numeric(text: &[char]) -> bool {
text.iter().all(|&c| '0' <= c && c <= '9')
}
// Tests whether the given string can be encoded as a segment in alphanumeric mode. // Tests whether the given string can be encoded as a segment in alphanumeric mode.
// A string is encodable iff each character is in the following set: 0 to 9, A to Z // A string is encodable iff each character is in the following set: 0 to 9, A to Z
// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon. // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
@ -1123,13 +1130,6 @@ impl QrSegment {
text.iter().all(|c| ALPHANUMERIC_CHARSET.contains(c)) text.iter().all(|c| ALPHANUMERIC_CHARSET.contains(c))
} }
// Tests whether the given string can be encoded as a segment in numeric mode.
// A string is encodable iff each character is in the range 0 to 9.
fn is_numeric(text: &[char]) -> bool {
text.iter().all(|&c| '0' <= c && c <= '9')
}
} }

Loading…
Cancel
Save