chooseMode static method

Mode chooseMode(
  1. String content, {
  2. CharacterSetECI? encoding,
})

Choose the best mode by examining the content. Note that 'encoding' is used as a hint; if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}.

Implementation

static Mode chooseMode(String content, {CharacterSetECI? encoding}) {
  if (encoding == CharacterSetECI.SJIS && _isOnlyDoubleByteKanji(content)) {
    // Choose Kanji mode if all input are double-byte characters
    return Mode.kanji;
  }
  var hasNumeric = false;
  var hasAlphanumeric = false;
  for (var i = 0; i < content.length; ++i) {
    var c = content.codeUnitAt(i);
    if (c >= $0 && c <= $9) {
      hasNumeric = true;
    } else if (getAlphanumericCode(c) != -1) {
      hasAlphanumeric = true;
    } else {
      return Mode.byte;
    }
  }
  if (hasAlphanumeric) {
    return Mode.alphanumeric;
  }
  if (hasNumeric) {
    return Mode.numeric;
  }
  return Mode.byte;
}