decodeHeader static method

String? decodeHeader(
  1. String? input
)

Implementation

static String? decodeHeader(String? input) {
  if (input == null || input.isEmpty) {
    return input;
  }
  // remove any spaces between 2 encoded words:
  final containsEncodedWordsWithSpace = input.contains('?= =?');
  final containsEncodedWordsWithoutSpace =
      !containsEncodedWordsWithSpace && input.contains('?==?');
  if (containsEncodedWordsWithSpace || containsEncodedWordsWithoutSpace) {
    final match = _encodingExpression.firstMatch(input);
    if (match != null) {
      final sequence = match.group(0)!;
      final separatorIndex = sequence.indexOf('?', 3);
      final endIndex = separatorIndex + 3;
      final startSequence = sequence.substring(0, endIndex);
      final searchText = containsEncodedWordsWithSpace
          ? '?= $startSequence'
          : '?=$startSequence';
      if (startSequence.endsWith('?B?')) {
        // in base64 encoding there are 2 cases:
        // 1. individual parts can end  with the padding character "=":
        //    - in that case we just remove the space between the encoded words
        // 2. individual words do not end with a padding character:
        //    - in that case we combine the words
        if (input.contains('=$searchText')) {
          if (containsEncodedWordsWithSpace) {
            input = input.replaceAll('?= =?', '?==?');
          }
        } else {
          input = input.replaceAll(searchText, '');
        }
      } else {
        // "standard case" - just fuse the sequences together
        input = input.replaceAll(searchText, '');
      }
    }
  }
  final buffer = StringBuffer();
  _decodeHeaderImpl(input, buffer);
  return buffer.toString();
}