encodeToCharCode static method

List<int?> encodeToCharCode(
  1. List<int> bytes
)

Implementation

static List<int?> encodeToCharCode(List<int> bytes) {
  int bn = 15; // bit needed
  int bv = 0; // bit value
  int outLen = (bytes.length * 8 + 14) ~/ 15;
  List<int> out = new List<int>.filled(outLen, -1, growable: false);
  int pos = 0;
  for (int byte in bytes) {
    if (bn > 8) {
      bv = (bv << 8) | byte;
      bn -= 8;
    } else {
      bv = ((bv << bn) | (byte >> (8 - bn))) & 0x7FFF;
      if (bv < 0x1936) {
        out[pos++] = bv + 0x3480;
      } else if (bv < 0x545C) {
        out[pos++] = bv + 0x34CA;
      } else {
        out[pos++] = bv + 0x57A4;
      }
      bv = byte;
      bn += 7;
    }
  }
  if (bn != 15) {
    if (bn > 7) {
      // need 8 bits or more, so has 7 bits or less
      out[pos++] = ((bv << (bn - 8)) & 0x7F) + 0x3400;
    } else {
      bv = (bv << bn) & 0x7FFF;
      if (bv < 0x1936) {
        out[pos++] = bv + 0x3480;
      } else if (bv < 0x545C) {
        out[pos++] = bv + 0x34CA;
      } else {
        out[pos++] = bv + 0x57A4;
      }
    }
  }
  return out;
}