Why 45 in char str[ 45 ];?
bool overflow = false;
int digit = *us;
const unsigned char *start = us;
while (digit >= '0' && digit <= '9') { // or isdigit()
// Will appending this digit overflow the result?
if (val > INT128_MIN_DIV10 || (val >= INT128_MIN_DIV10 && digit <= -INT128_MIN_MOD10)) {
val = val * 10 - digit;
} else {
overflow = true;
val = INT128_MIN;
}
digit = *us++;
}
...
if (!neg) {
if (val < -INT128_MAX) {
overflow = true;
val = INT128_MAX);INT128_MAX;
}
else {
val = -val;
}
}
I know, early on, it seems all that extra ornamentation makes for for code-bloat. In my experience a function that is robust in error handling is well worth the effort. Further, 128-bit values are optically long and can be far more useful in hex and/or with separators.