compareAsciiUpperCase function

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

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

Comparison treats all lower-case ASCII letters as upper-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 and is compatible with equality.

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 compareAsciiUpperCase(String a, String b) {
  var defaultResult = 0; // Returned if no difference found.
  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;
    // Upper-case if letters.
    var aUpperCase = aChar;
    var bUpperCase = bChar;
    if (_lowerCaseA <= aChar && aChar <= _lowerCaseZ) {
      aUpperCase -= _asciiCaseBit;
    if (_lowerCaseA <= bChar && bChar <= _lowerCaseZ) {
      bUpperCase -= _asciiCaseBit;
    if (aUpperCase != bUpperCase) return (aUpperCase - bUpperCase).sign;
    if (defaultResult == 0) defaultResult = (aChar - bChar);
  if (b.length > a.length) return -1;
  return defaultResult.sign;