convert method

  1. @override
String convert(
  1. Uint8List bytes, [
  2. int start = 0,
  3. int? end
])
override

Converts input and returns the result of the conversion.

Implementation

@override
String convert(Uint8List bytes, [int start = 0, int? end]) {
  final int bytesLength = bytes.length;
  end = RangeError.checkValidRange(start, end, bytesLength);

  if (Endian.host != Endian.little) {
    throw FormatException("host endian is not little");
  }

  BytesBuilder builder = BytesBuilder();

  int? unicode;

  for (int i = start; i < bytesLength;) {
    // When 1 byte becomes 1 cp949 code
    if (0x00 <= bytes[i] && bytes[i] <= 0x7F) {
      unicode = cp949ToUnicodeCodeMap[bytes[i]];

      if (unicode == null) {
        throw FormatException(
            'out of unicode table : ${bytes[i]} is Invalid code unit of CP949. It has to be (>=0x00 <=0x7F) || (>=0x8141 <=0xFDFE).');
      }

      // little endian
      builder.addByte(unicode);
      builder.addByte(0);
      i = i + 1;
    } else {
      // When 2 bytes become 1 cp949 code
      int cp949Code;
      if (i + 1 == bytesLength) {
        cp949Code = bytes[i] << 8;
      } else {
        cp949Code = (bytes[i] << 8) + bytes[i + 1];
      }

      // valid range
      if (0x8141 <= cp949Code && cp949Code <= 0xFDFE) {
        unicode = cp949ToUnicodeCodeMap[cp949Code];

        if (unicode == null) {
          throw FormatException(
              'out of unicode table : ${bytes[i]} is Invalid code unit of CP949. It has to be (>=0x00 <=0x7F) || (>=0x8141 <=0xFDFE).');
        }

        // little endian
        builder.addByte(unicode);
        builder.addByte(unicode >> 8);
      } else {
        throw FormatException(
            '${bytes[i]} is Invalid code unit of CP949. It has to be (>=0x00 <=0x7F) || (>=0x8141 <=0xFDFE).');
      }
      i = i + 2;
    }
  }
  // print(builder.toBytes());
  // print(builder.toBytes().buffer.asUint16List());
  // print("아름다운".codeUnits);

  // dart string use only 16bit charCodes
  return String.fromCharCodes(builder.toBytes().buffer.asUint16List());
}