Skip to content

Commit

Permalink
Support negative numbers in q_strnaturalcmp
Browse files Browse the repository at this point in the history
Note: we only support negative numbers at the beginning of strings so that
"-2" is sorted before "-1", but "file-2345.ext" *after* "file-1234.ext".
  • Loading branch information
andrei-drexler committed Mar 3, 2024
1 parent 7d4bb80 commit e1a9f0e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,20 @@ void MultiString_Append (char **pvec, const char *str)

int q_strnaturalcmp (const char *s1, const char *s2)
{
qboolean neg1, neg2, sign1, sign2;

if (s1 == s2)
return 0;

neg1 = *s1 == '-';
neg2 = *s2 == '-';
sign1 = neg1 || *s1 == '+';
sign2 = neg2 || *s2 == '+';

// early out if strings start with different signs followed by digits
if (neg1 != neg2 && q_isdigit (s1[sign1]) && q_isdigit (s1[sign2]))
return neg2 - neg1;

skip_prefix:
while (*s1 && !q_isdigit (*s1) && q_toupper (*s1) == q_toupper (*s2))
{
Expand All @@ -240,7 +251,7 @@ int q_strnaturalcmp (const char *s1, const char *s2)
{
const char *begin1 = s1++;
const char *begin2 = s2++;
int diff;
int diff, sign;

while (*begin1 == '0')
begin1++;
Expand All @@ -252,17 +263,23 @@ int q_strnaturalcmp (const char *s1, const char *s2)
while (q_isdigit (*s2))
s2++;

sign = neg1 ? -1 : 1;

diff = (s1 - begin1) - (s2 - begin2);
if (diff)
return diff;
return diff * sign;

while (begin1 != s1)
{
diff = *begin1++ - *begin2++;
if (diff)
return diff;
return diff * sign;
}

// We only support negative numbers at the beginning of strings so that
// "-2" is sorted before "-1", but "file-2345.ext" *after* "file-1234.ext".
neg1 = neg2 = false;

goto skip_prefix;
}

Expand Down

0 comments on commit e1a9f0e

Please sign in to comment.