equalsIgnoreAsciiCase function

bool equalsIgnoreAsciiCase(
  1. String a,
  2. String b
)

Checks if strings a and b differ only on the case of ASCII letters.

Strings are equal if they have the same length, and the characters at each index are the same, or they are ASCII letters where one is upper-case and the other is the lower-case version of the same letter.

The comparison does not ignore the case of non-ASCII letters, so an upper-case ae-ligature (Æ) is different from a lower case ae-ligature (æ).

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.

Implementation

bool equalsIgnoreAsciiCase(String a, String b) {
  if (a.length != b.length) return false;
  for (var i = 0; i < a.length; i++) {
    var aChar = a.codeUnitAt(i);
    var bChar = b.codeUnitAt(i);
    if (aChar == bChar) continue;
    // Quick-check for whether this may be different cases of the same letter.
    if (aChar ^ bChar != _asciiCaseBit) return false;
    // If it's possible, then check if either character is actually an ASCII
    // letter.
    var aCharLowerCase = aChar | _asciiCaseBit;
    if (_lowerCaseA <= aCharLowerCase && aCharLowerCase <= _lowerCaseZ) {
      continue;
    }
    return false;
  }
  return true;
}