convert method

  1. @override
Uint8List convert(
  1. covariant List<int> input
)
override

Converts input array of numbers with bit-length of source to an array of numbers with bit-length of target. The input array will be treated as a sequence of bits to convert.

After consuming all of input sequence, if there are some non-zero partial word remains, 0 will be padded on the right to make the final word.

Implementation

@override
Uint8List convert(List<int> input) {
  final table = alphabet;
  final pad = padding;
  int n = input.length;
  int full = n ~/ 3;
  int rem = n - full * 3;

  int outLen;
  if (pad != null) {
    outLen = (full + (rem == 0 ? 0 : 1)) << 2;
  } else {
    outLen = (full << 2) + (rem == 0 ? 0 : rem + 1);
  }
  var out = Uint8List(outLen);

  int i = 0, j = 0, b0, b1, b2;
  for (int g = 0; g < full; ++g) {
    b0 = input[i++] & 0xFF;
    b1 = input[i++] & 0xFF;
    b2 = input[i++] & 0xFF;
    out[j++] = table[b0 >> 2];
    out[j++] = table[((b0 & 0x3) << 4) | (b1 >> 4)];
    out[j++] = table[((b1 & 0xF) << 2) | (b2 >> 6)];
    out[j++] = table[b2 & 0x3F];
  }

  if (rem == 1) {
    b0 = input[i] & 0xFF;
    out[j++] = table[b0 >> 2];
    out[j++] = table[(b0 & 0x3) << 4];
    if (pad != null) {
      out[j++] = pad;
      out[j++] = pad;
    }
  } else if (rem == 2) {
    b0 = input[i] & 0xFF;
    b1 = input[i + 1] & 0xFF;
    out[j++] = table[b0 >> 2];
    out[j++] = table[((b0 & 0x3) << 4) | (b1 >> 4)];
    out[j++] = table[(b1 & 0xF) << 2];
    if (pad != null) {
      out[j++] = pad;
    }
  }

  return out;
}