compareAsciiLowerCase function

int compareAsciiLowerCase(
  1. String a,
  2. String b

Compares a and b lexically, converting ASCII letters to lower case.

Comparison treats all upper-case ASCII letters as lower-case letters, but does no case conversion for non-ASCII letters.

If two strings differ only on the case of ASCII letters, the one with the capital letter at the first difference will compare as less than the other string. This tie-breaking ensures that the comparison is a total ordering on strings.

Ignoring non-ASCII letters is not generally a good idea, but it makes sense for situations where the strings are known to be ASCII. Examples could be Dart identifiers, base-64 or hex encoded strings, GUIDs or similar strings with a known structure.


int compareAsciiLowerCase(String a, String b) {
  var defaultResult = 0;
  for (var i = 0; i < a.length; i++) {
    if (i >= b.length) return 1;
    var aChar = a.codeUnitAt(i);
    var bChar = b.codeUnitAt(i);
    if (aChar == bChar) continue;
    var aLowerCase = aChar;
    var bLowerCase = bChar;
    // Upper case if ASCII letters.
    if (_upperCaseA <= bChar && bChar <= _upperCaseZ) {
      bLowerCase += _asciiCaseBit;
    if (_upperCaseA <= aChar && aChar <= _upperCaseZ) {
      aLowerCase += _asciiCaseBit;
    if (aLowerCase != bLowerCase) return (aLowerCase - bLowerCase).sign;
    if (defaultResult == 0) defaultResult = aChar - bChar;
  if (b.length > a.length) return -1;
  return defaultResult.sign;