decode function
Implementation
List<int> decode(String input) {
if (input.isEmpty) {
return <int>[];
}
var encodedInput = utf8.encode(input);
var uintAlphabet = utf8.encode(ALPHABET);
List<int?> INDEXES = List<int>.filled(128, -1);
for (int i = 0; i < ALPHABET.length; i++) {
INDEXES[uintAlphabet[i]] = i;
}
// Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).
List<int> input58 = List<int>.filled(encodedInput.length, 0);
for (int i = 0; i < encodedInput.length; ++i) {
var c = encodedInput[i];
var digit = c < 128 ? INDEXES[c]! : -1;
if (digit < 0) {
var buff = <int>[c];
var invalidChar = utf8.decode(buff);
throw new AddressFormatException(
"Illegal character " + invalidChar + " at position " + i.toString());
}
input58[i] = digit;
}
// Count leading zeros.
int zeros = 0;
while (zeros < input58.length && input58[zeros] == 0) {
++zeros;
}
// Convert base-58 digits to base-256 digits.
var decoded = List<int>.filled(encodedInput.length, 0);
int outputStart = decoded.length;
for (int inputStart = zeros; inputStart < input58.length;) {
decoded[--outputStart] = divmod(input58, inputStart, 58, 256);
if (input58[inputStart] == 0) {
++inputStart; // optimization - skip leading zeros
}
}
// Ignore extra leading zeroes that were added during the calculation.
while (outputStart < decoded.length && decoded[outputStart] == 0) {
++outputStart;
}
// Return decoded data (including original number of leading zeros).
return decoded.sublist(outputStart - zeros, decoded.length);
}