compareAsciiLowerCaseNatural function
Compares strings a
and b
according to lower-case
natural sort ordering.
ASCII letters are converted to lower case before being compared, like for compareAsciiLowerCase, then the result is compared like for compareNatural.
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.
Implementation
int compareAsciiLowerCaseNatural(String a, String b) {
int defaultResult = 0; // Returned if no difference found.
for (int 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;
int aLowerCase = aChar;
int bLowerCase = bChar;
if (_upperCaseA <= aChar && aChar <= _upperCaseZ) {
aLowerCase += _asciiCaseBit;
}
if (_upperCaseA <= bChar && bChar <= _upperCaseZ) {
bLowerCase += _asciiCaseBit;
}
if (aLowerCase != bLowerCase) {
return _compareNaturally(a, b, i, aLowerCase, bLowerCase);
}
if (defaultResult == 0) defaultResult = aChar - bChar;
}
if (b.length > a.length) return -1;
return defaultResult.sign;
}