createEncodingMap static method

Map<int, int> createEncodingMap(
  1. String symbols,
  2. int startIndex
)

Static helper function to generate a conversion map from a symbols string.

Implementation

static Map<int, int> createEncodingMap(String symbols, int startIndex) {
  final runes = symbols.runes;
  final map = <int, int>{};
  var index = 0;
  if (runes.length != 255 - startIndex) {
    print('WARNING: there are not ${255 - startIndex} symbols '
        'but ${runes.length} runes in the specified map - '
        'is the given startIndex $startIndex correct?');
  }
  for (final rune in runes) {
    if (rune != 0x3F) {
      // "?" denote an empty slot in the map
      final value = index + startIndex + 1;
      if (map.containsValue(value)) {
        final symbol = symbols.substring(index, index + 1);
        final firstIndex = symbols.indexOf(symbol);
        final lastIndex = symbols.lastIndexOf(symbol);
        throw FormatException(
            'Duplicate value $value for symbols "$symbol" at index $index '
            '- in symbols to found at $firstIndex and $lastIndex');
      }
      if (value <= startIndex) {
        final symbol = symbols.substring(index, index + 1);
        throw FormatException(
            'Invalid value $value for "$symbol" at index $index');
      }
      map[rune] = value;
      print('\t$rune: $value,');
    }
    index++;
  }
  return map;
}