decodeText method

  1. @override
String decodeText(
  1. String part,
  2. Encoding codec, {
  3. bool isHeader = false,
})
override

Decodes the specified text

part the text part that should be decoded codec the character encoding (charset) Set isHeader to true to decode header text using the Q-Encoding scheme, compare https://tools.ietf.org/html/rfc2047#section-4.2

Implementation

@override
String decodeText(
  final String part,
  final Encoding codec, {
  bool isHeader = false,
}) {
  final buffer = StringBuffer();
  // remove all soft-breaks:
  final cleaned = part.replaceAll('=\r\n', '').replaceAll('=\n', '');
  var i = 0;
  while (i < cleaned.length) {
    final char = cleaned.codeUnitAt(i);
    if (char == AsciiRunes.runeEquals) {
      // collect consecutive =XX sequences so that multi-byte characters
      // are decoded together
      final bytes = <int>[];
      while (i < cleaned.length &&
          cleaned.codeUnitAt(i) == AsciiRunes.runeEquals) {
        final byte = _decodeHexByte(cleaned, i + 1);
        if (byte == null) {
          break;
        }
        bytes.add(byte);
        i += 3;
      }
      if (bytes.isNotEmpty) {
        try {
          buffer.write(codec.decode(bytes));
        } on FormatException catch (err) {
          print('unable to decode quotedPrintable buffer: ${err.message}');
          buffer.write(String.fromCharCodes(bytes));
        }
      } else {
        // RFC 2045 section 6.7: a "=" that is not followed by two hex digits
        // is invalid and is kept as is (robustness)
        buffer.writeCharCode(char);
        i++;
      }
    } else if (isHeader && char == AsciiRunes.runeUnderline) {
      buffer.write(' ');
      i++;
    } else {
      buffer.writeCharCode(char);
      i++;
    }
  }

  return buffer.toString();
}