decode static method

String decode(
  1. List<int> bytes, {
  2. bool allowMalformed = false,
})

Implementation

static String decode(List<int> bytes, {bool allowMalformed = false}) {
  int outLen = 0;
  for (int b in bytes) {
    if (b <= 0x7F) {
      outLen++;
    } else {
      if (!allowMalformed) {
        throw ArgumentException.invalidOperationArguments(
          "Invalid ASCII bytes.",
          name: "bytes",
          reason: "Invalid ASCII byte: $b",
        );
      }
      outLen++; // will insert U+FFFD
    }
  }

  final chars = List<int>.filled(outLen, 0, growable: false);

  int i = 0;
  for (int b in bytes) {
    if (b <= 0x7F) {
      chars[i++] = b;
    } else {
      chars[i++] =
          allowMalformed
              ? 0xFFFD
              : throw ArgumentException.invalidOperationArguments(
                "Invalid ASCII bytes.",
                name: "bytes",
                reason: "Invalid ASCII byte: $b",
              );
    }
  }

  return String.fromCharCodes(chars);
}