Bump to 4.6.7
[qBittorrent.git] / src / base / utils / compare.cpp
blob536dd147ce43dd6e44023b819a73405b0a53c95b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "compare.h"
31 #include <QChar>
32 #include <QString>
34 #ifndef QBT_USE_QCOLLATOR
35 int Utils::Compare::naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity)
37 // Return value <0: `left` is smaller than `right`
38 // Return value >0: `left` is greater than `right`
39 // Return value =0: both strings are equal
41 int posL = 0;
42 int posR = 0;
43 while (true)
45 if ((posL == left.size()) || (posR == right.size()))
46 return (left.size() - right.size()); // when a shorter string is another string's prefix, shorter string place before longer string
48 const QChar leftChar = (caseSensitivity == Qt::CaseSensitive) ? left[posL] : left[posL].toLower();
49 const QChar rightChar = (caseSensitivity == Qt::CaseSensitive) ? right[posR] : right[posR].toLower();
50 // Compare only non-digits.
51 // Numbers should be compared as a whole
52 // otherwise the string->int conversion can yield a wrong value
53 if ((leftChar == rightChar) && !leftChar.isDigit())
55 // compare next character
56 ++posL;
57 ++posR;
59 else if (leftChar.isDigit() && rightChar.isDigit())
61 // Both are digits, compare the numbers
63 const auto numberView = [](const QStringView str, int &pos) -> QStringView
65 const int start = pos;
66 while ((pos < str.size()) && str[pos].isDigit())
67 ++pos;
68 return str.mid(start, (pos - start));
71 const QStringView numViewL = numberView(left, posL);
72 const QStringView numViewR = numberView(right, posR);
74 if (numViewL.length() != numViewR.length())
75 return (numViewL.length() - numViewR.length());
77 // both string/view has the same length
78 for (int i = 0; i < numViewL.length(); ++i)
80 const QChar numL = numViewL[i];
81 const QChar numR = numViewR[i];
83 if (numL != numR)
84 return (numL.unicode() - numR.unicode());
87 // String + digits do match and we haven't hit the end of both strings
88 // then continue to consume the remainings
90 else
92 return (leftChar.unicode() - rightChar.unicode());
96 #endif