strtol method
Converts the string nptr to an int.
Trims leading whitespace, handles optional signs, stops at the first invalid
character, and autodetects the radix (base) if radix is 0 (e.g. 0x for 16).
If endptr is provided, endptr[0] is set to the unparsed remainder of the string.
Implementation
int strtol(String nptr, {List<String>? endptr, int radix = 10}) {
if (nptr.isEmpty) {
if (endptr != null && endptr.isNotEmpty) endptr[0] = nptr;
return 0;
}
int i = 0;
// Skip whitespace
while (i < nptr.length) {
int c = nptr.codeUnitAt(i);
if (c == 32 || (c >= 9 && c <= 13)) {
i++;
} else {
break;
}
}
if (i >= nptr.length) {
if (endptr != null && endptr.isNotEmpty) endptr[0] = nptr;
return 0;
}
int start = i;
bool negative = false;
int c = nptr.codeUnitAt(i);
if (c == 45) { // '-'
negative = true;
i++;
} else if (c == 43) { // '+'
i++;
}
if (i >= nptr.length) {
if (endptr != null && endptr.isNotEmpty) endptr[0] = nptr;
return 0;
}
int actualRadix = radix;
if (actualRadix == 0) {
if (nptr.codeUnitAt(i) == 48) { // '0'
if (i + 1 < nptr.length && (nptr.codeUnitAt(i + 1) == 120 || nptr.codeUnitAt(i + 1) == 88)) {
actualRadix = 16;
i += 2;
} else {
actualRadix = 8;
}
} else {
actualRadix = 10;
}
} else if (actualRadix == 16) {
if (nptr.codeUnitAt(i) == 48 && i + 1 < nptr.length && (nptr.codeUnitAt(i + 1) == 120 || nptr.codeUnitAt(i + 1) == 88)) {
i += 2;
}
}
int numStart = i;
// Scan digits
while (i < nptr.length) {
c = nptr.codeUnitAt(i);
int digit = -1;
if (c >= 48 && c <= 57) { digit = c - 48; }
else if (c >= 65 && c <= 90) { digit = c - 65 + 10; }
else if (c >= 97 && c <= 122) { digit = c - 97 + 10; }
if (digit == -1 || digit >= actualRadix) break;
i++;
}
if (i == numStart) {
if (numStart > start && (nptr.codeUnitAt(numStart - 1) == 120 || nptr.codeUnitAt(numStart - 1) == 88)) {
i = numStart - 1;
if (endptr != null && endptr.isNotEmpty) {
endptr[0] = nptr.substring(i);
}
return 0;
}
if (endptr != null && endptr.isNotEmpty) endptr[0] = nptr;
return 0;
}
if (endptr != null && endptr.isNotEmpty) {
endptr[0] = nptr.substring(i);
}
String valStr = nptr.substring(numStart, i);
String fullStr = (negative ? "-" : "") + valStr;
return int.tryParse(fullStr, radix: actualRadix) ?? 0;
}