strverscmp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Compare strings while treating digits characters numerically.
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA. */
  17. #include "libiberty.h"
  18. #include "safe-ctype.h"
  19. /*
  20. @deftypefun int strverscmp (const char *@var{s1}, const char *@var{s2})
  21. The @code{strverscmp} function compares the string @var{s1} against
  22. @var{s2}, considering them as holding indices/version numbers. Return
  23. value follows the same conventions as found in the @code{strverscmp}
  24. function. In fact, if @var{s1} and @var{s2} contain no digits,
  25. @code{strverscmp} behaves like @code{strcmp}.
  26. Basically, we compare strings normally (character by character), until
  27. we find a digit in each string - then we enter a special comparison
  28. mode, where each sequence of digits is taken as a whole. If we reach the
  29. end of these two parts without noticing a difference, we return to the
  30. standard comparison mode. There are two types of numeric parts:
  31. "integral" and "fractional" (those begin with a '0'). The types
  32. of the numeric parts affect the way we sort them:
  33. @itemize @bullet
  34. @item
  35. integral/integral: we compare values as you would expect.
  36. @item
  37. fractional/integral: the fractional part is less than the integral one.
  38. Again, no surprise.
  39. @item
  40. fractional/fractional: the things become a bit more complex.
  41. If the common prefix contains only leading zeroes, the longest part is less
  42. than the other one; else the comparison behaves normally.
  43. @end itemize
  44. @smallexample
  45. strverscmp ("no digit", "no digit")
  46. @result{} 0 // @r{same behavior as strcmp.}
  47. strverscmp ("item#99", "item#100")
  48. @result{} <0 // @r{same prefix, but 99 < 100.}
  49. strverscmp ("alpha1", "alpha001")
  50. @result{} >0 // @r{fractional part inferior to integral one.}
  51. strverscmp ("part1_f012", "part1_f01")
  52. @result{} >0 // @r{two fractional parts.}
  53. strverscmp ("foo.009", "foo.0")
  54. @result{} <0 // @r{idem, but with leading zeroes only.}
  55. @end smallexample
  56. This function is especially useful when dealing with filename sorting,
  57. because filenames frequently hold indices/version numbers.
  58. @end deftypefun
  59. */
  60. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  61. fractional parts, S_Z: idem but with leading Zeroes only */
  62. #define S_N 0x0
  63. #define S_I 0x4
  64. #define S_F 0x8
  65. #define S_Z 0xC
  66. /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  67. #define CMP 2
  68. #define LEN 3
  69. /* Compare S1 and S2 as strings holding indices/version numbers,
  70. returning less than, equal to or greater than zero if S1 is less than,
  71. equal to or greater than S2 (for more info, see the Glibc texinfo doc). */
  72. int
  73. strverscmp (const char *s1, const char *s2)
  74. {
  75. const unsigned char *p1 = (const unsigned char *) s1;
  76. const unsigned char *p2 = (const unsigned char *) s2;
  77. unsigned char c1, c2;
  78. int state;
  79. int diff;
  80. /* Symbol(s) 0 [1-9] others (padding)
  81. Transition (10) 0 (01) d (00) x (11) - */
  82. static const unsigned int next_state[] =
  83. {
  84. /* state x d 0 - */
  85. /* S_N */ S_N, S_I, S_Z, S_N,
  86. /* S_I */ S_N, S_I, S_I, S_I,
  87. /* S_F */ S_N, S_F, S_F, S_F,
  88. /* S_Z */ S_N, S_F, S_Z, S_Z
  89. };
  90. static const int result_type[] =
  91. {
  92. /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
  93. 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
  94. /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
  95. CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  96. /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
  97. +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
  98. /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
  99. CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  100. /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
  101. -1, CMP, CMP, CMP
  102. };
  103. if (p1 == p2)
  104. return 0;
  105. c1 = *p1++;
  106. c2 = *p2++;
  107. /* Hint: '0' is a digit too. */
  108. state = S_N | ((c1 == '0') + (ISDIGIT (c1) != 0));
  109. while ((diff = c1 - c2) == 0 && c1 != '\0')
  110. {
  111. state = next_state[state];
  112. c1 = *p1++;
  113. c2 = *p2++;
  114. state |= (c1 == '0') + (ISDIGIT (c1) != 0);
  115. }
  116. state = result_type[state << 2 | (((c2 == '0') + (ISDIGIT (c2) != 0)))];
  117. switch (state)
  118. {
  119. case CMP:
  120. return diff;
  121. case LEN:
  122. while (ISDIGIT (*p1++))
  123. if (!ISDIGIT (*p2++))
  124. return 1;
  125. return ISDIGIT (*p2) ? -1 : diff;
  126. default:
  127. return state;
  128. }
  129. }