Browse Source

Slight improvement of Natural::compare, for instance: t1.txt now comes after t.txt

merge-requests/413/head
eidheim 3 years ago
parent
commit
d28dcf6f7a
  1. 4
      src/utility.cpp
  2. 6
      tests/utility_test.cpp

4
src/utility.cpp

@ -316,6 +316,10 @@ int Natural::compare(const std::string &s1, const std::string &s2) {
size_t i1 = 0; size_t i1 = 0;
size_t i2 = 0; size_t i2 = 0;
while(i1 < s1.size() && i2 < s2.size()) { while(i1 < s1.size() && i2 < s2.size()) {
if(s1[i1] == '.' && s2[i2] != '.')
return -1;
if(s1[i1] != '.' && s2[i2] == '.')
return 1;
if(is_digit(s1[i1]) && !is_digit(s2[i2])) if(is_digit(s1[i1]) && !is_digit(s2[i2]))
return -1; return -1;
if(!is_digit(s1[i1]) && is_digit(s2[i2])) if(!is_digit(s1[i1]) && is_digit(s2[i2]))

6
tests/utility_test.cpp

@ -198,4 +198,10 @@ int main() {
g_assert(Natural::compare("z2", "z11") == -1); g_assert(Natural::compare("z2", "z11") == -1);
g_assert(Natural::compare("z11", "z2") == 1); g_assert(Natural::compare("z11", "z2") == 1);
} }
{
g_assert(Natural::compare("t", "t1") == -1);
g_assert(Natural::compare("t.txt", "t1.txt") == -1);
g_assert(Natural::compare("t1", "t") == 1);
g_assert(Natural::compare("t1.txt", "t.txt") == 1);
}
} }

Loading…
Cancel
Save