int _compareNumerically()

in lib/src/comparators.dart [283:354]


int _compareNumerically(String a, String b, int aChar, int bChar, int index) {
  // Both are digits. Find the first significant different digit, then find
  // the length of the numbers.
  if (_isNonZeroNumberSuffix(a, index)) {
    // Part of a longer number, differs at this index, just count the length.
    var result = _compareDigitCount(a, b, index, index);
    if (result != 0) return result;
    // If same length, the current character is the most significant differing
    // digit.
    return (aChar - bChar).sign;
  }
  // Not part of larger (non-zero) number, so skip leading zeros before
  // comparing numbers.
  var aIndex = index;
  var bIndex = index;
  if (aChar == _zero) {
    do {
      aIndex++;
      if (aIndex == a.length) return -1; // number in a is zero, b is not.
      aChar = a.codeUnitAt(aIndex);
    } while (aChar == _zero);
    if (!_isDigit(aChar)) return -1;
  } else if (bChar == _zero) {
    do {
      bIndex++;
      if (bIndex == b.length) return 1; // number in b is zero, a is not.
      bChar = b.codeUnitAt(bIndex);
    } while (bChar == _zero);
    if (!_isDigit(bChar)) return 1;
  }
  if (aChar != bChar) {
    var result = _compareDigitCount(a, b, aIndex, bIndex);
    if (result != 0) return result;
    return (aChar - bChar).sign;
  }
  // Same leading digit, one had more leading zeros.
  // Compare digits until reaching a difference.
  while (true) {
    var aIsDigit = false;
    var bIsDigit = false;
    aChar = 0;
    bChar = 0;
    if (++aIndex < a.length) {
      aChar = a.codeUnitAt(aIndex);
      aIsDigit = _isDigit(aChar);
    }
    if (++bIndex < b.length) {
      bChar = b.codeUnitAt(bIndex);
      bIsDigit = _isDigit(bChar);
    }
    if (aIsDigit) {
      if (bIsDigit) {
        if (aChar == bChar) continue;
        // First different digit found.
        break;
      }
      // bChar is non-digit, so a has longer number.
      return 1;
    } else if (bIsDigit) {
      return -1; // b has longer number.
    } else {
      // Neither is digit, so numbers had same numerical value.
      // Fall back on number of leading zeros
      // (reflected by difference in indices).
      return (aIndex - bIndex).sign;
    }
  }
  // At first differing digits.
  var result = _compareDigitCount(a, b, aIndex, bIndex);
  if (result != 0) return result;
  return (aChar - bChar).sign;
}