TAKEN FROM Qt, because it doesn't expose it intelligently
| 51 | |
| 52 | /// TAKEN FROM Qt, because it doesn't expose it intelligently |
| 53 | int StringUtils::naturalCompare(const QString& s1, const QString& s2, Qt::CaseSensitivity cs) |
| 54 | { |
| 55 | int l1 = 0, l2 = 0; |
| 56 | while (l1 <= s1.count() && l2 <= s2.count()) { |
| 57 | // skip spaces, tabs and 0's |
| 58 | QChar c1 = getNextChar(s1, l1); |
| 59 | while (c1.isSpace()) |
| 60 | c1 = getNextChar(s1, ++l1); |
| 61 | |
| 62 | QChar c2 = getNextChar(s2, l2); |
| 63 | while (c2.isSpace()) |
| 64 | c2 = getNextChar(s2, ++l2); |
| 65 | |
| 66 | if (c1.isDigit() && c2.isDigit()) { |
| 67 | while (c1.digitValue() == 0) |
| 68 | c1 = getNextChar(s1, ++l1); |
| 69 | while (c2.digitValue() == 0) |
| 70 | c2 = getNextChar(s2, ++l2); |
| 71 | |
| 72 | int lookAheadLocation1 = l1; |
| 73 | int lookAheadLocation2 = l2; |
| 74 | int currentReturnValue = 0; |
| 75 | // find the last digit, setting currentReturnValue as we go if it isn't equal |
| 76 | for (QChar lookAhead1 = c1, lookAhead2 = c2; (lookAheadLocation1 <= s1.length() && lookAheadLocation2 <= s2.length()); |
| 77 | lookAhead1 = getNextChar(s1, ++lookAheadLocation1), lookAhead2 = getNextChar(s2, ++lookAheadLocation2)) { |
| 78 | bool is1ADigit = !lookAhead1.isNull() && lookAhead1.isDigit(); |
| 79 | bool is2ADigit = !lookAhead2.isNull() && lookAhead2.isDigit(); |
| 80 | if (!is1ADigit && !is2ADigit) |
| 81 | break; |
| 82 | if (!is1ADigit) |
| 83 | return -1; |
| 84 | if (!is2ADigit) |
| 85 | return 1; |
| 86 | if (currentReturnValue == 0) { |
| 87 | if (lookAhead1 < lookAhead2) { |
| 88 | currentReturnValue = -1; |
| 89 | } else if (lookAhead1 > lookAhead2) { |
| 90 | currentReturnValue = 1; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | if (currentReturnValue != 0) |
| 95 | return currentReturnValue; |
| 96 | } |
| 97 | |
| 98 | if (cs == Qt::CaseInsensitive) { |
| 99 | if (!c1.isLower()) |
| 100 | c1 = c1.toLower(); |
| 101 | if (!c2.isLower()) |
| 102 | c2 = c2.toLower(); |
| 103 | } |
| 104 | |
| 105 | int r = QString::localeAwareCompare(c1, c2); |
| 106 | if (r < 0) |
| 107 | return -1; |
| 108 | if (r > 0) |
| 109 | return 1; |
| 110 |
nothing calls this directly
no test coverage detected