Skip to main content
deleted 4 characters in body
Source Link
Alnitak
  • 441
  • 2
  • 7

I'm surprised that no-one has pointed out the O(m * base) complexity caused by the inner loop that performs a linear scan over the (unnecessary) ASCIICodes array.

This version of the outer loop removes the inner loop, supports lower cased characters, and also breaks the loop as soon as any illegal character is seen.

while ((n = x[count++])) {
    int ch = toupper((int)n);
    if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
        int digit = ch - '0';
        if (digit > 9) {
            digit +== 10 -+ ('A'ch - '0');'A';  // convert 'A' to 10, etc
        }
        if (digit < base) {
            output = output * base + digit;
        } else {
            break;
        }
    } else {
        break;
    }
}

I'm surprised that no-one has pointed out the O(m * base) complexity caused by the inner loop that performs a linear scan over the (unnecessary) ASCIICodes array.

This version of the outer loop removes the inner loop, supports lower cased characters, and also breaks the loop as soon as any illegal character is seen.

while ((n = x[count++])) {
    int ch = toupper((int)n);
    if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
        int digit = ch - '0';
        if (digit > 9) {
            digit += 10 - ('A' - '0');  // convert 'A' to 10, etc
        }
        if (digit < base) {
            output = output * base + digit;
        } else {
            break;
        }
    } else {
        break;
    }
}

I'm surprised that no-one has pointed out the O(m * base) complexity caused by the inner loop that performs a linear scan over the (unnecessary) ASCIICodes array.

This version of the outer loop removes the inner loop, supports lower cased characters, and also breaks the loop as soon as any illegal character is seen.

while ((n = x[count++])) {
    int ch = toupper((int)n);
    if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
        int digit = ch - '0';
        if (digit > 9) {
            digit = 10 + ch - 'A';  // convert 'A' to 10, etc
        }
        if (digit < base) {
            output = output * base + digit;
        } else {
            break;
        }
    } else {
        break;
    }
}
Source Link
Alnitak
  • 441
  • 2
  • 7

I'm surprised that no-one has pointed out the O(m * base) complexity caused by the inner loop that performs a linear scan over the (unnecessary) ASCIICodes array.

This version of the outer loop removes the inner loop, supports lower cased characters, and also breaks the loop as soon as any illegal character is seen.

while ((n = x[count++])) {
    int ch = toupper((int)n);
    if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
        int digit = ch - '0';
        if (digit > 9) {
            digit += 10 - ('A' - '0');  // convert 'A' to 10, etc
        }
        if (digit < base) {
            output = output * base + digit;
        } else {
            break;
        }
    } else {
        break;
    }
}