strverscmp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Compare strings while treating digits characters numerically.
  3. Copyright (C) 1997-2024
  4. Free Software Foundation, Inc.
  5. This file is part of the GNU C Library.
  6. Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, see
  17. <https://www.gnu.org/licenses/>.
  18. This file is part of the Midnight Commander.
  19. The Midnight Commander is free software: you can redistribute it
  20. and/or modify it under the terms of the GNU General Public License as
  21. published by the Free Software Foundation, either version 3 of the License,
  22. or (at your option) any later version.
  23. The Midnight Commander is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. GNU General Public License for more details.
  27. You should have received a copy of the GNU General Public License
  28. along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. */
  30. #include <config.h>
  31. #include <ctype.h>
  32. #ifdef HAVE_STRVERSCMP
  33. #include <string.h>
  34. #endif /* HAVE_STRVERSCMP */
  35. #include "lib/strutil.h"
  36. /*** global variables ****************************************************************************/
  37. /*** file scope macro definitions ****************************************************************/
  38. #ifndef HAVE_STRVERSCMP
  39. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  40. fractional parts, S_Z: idem but with leading Zeroes only */
  41. #define S_N 0x0
  42. #define S_I 0x3
  43. #define S_F 0x6
  44. #define S_Z 0x9
  45. /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  46. #define CMP 2
  47. #define LEN 3
  48. #endif /* HAVE_STRVERSCMP */
  49. /*** file scope type declarations ****************************************************************/
  50. /*** file scope variables ************************************************************************/
  51. /* --------------------------------------------------------------------------------------------- */
  52. /*** file scope functions ************************************************************************/
  53. /* --------------------------------------------------------------------------------------------- */
  54. /* --------------------------------------------------------------------------------------------- */
  55. /*** public functions ****************************************************************************/
  56. /* --------------------------------------------------------------------------------------------- */
  57. /* Compare S1 and S2 as strings holding indices/version numbers,
  58. returning less than, equal to or greater than zero if S1 is less than,
  59. equal to or greater than S2 (for more info, see the texinfo doc).
  60. */
  61. int
  62. str_verscmp (const char *s1, const char *s2)
  63. {
  64. #ifdef HAVE_STRVERSCMP
  65. return strverscmp (s1, s2);
  66. #else /* HAVE_STRVERSCMP */
  67. const unsigned char *p1 = (const unsigned char *) s1;
  68. const unsigned char *p2 = (const unsigned char *) s2;
  69. unsigned char c1, c2;
  70. int state;
  71. int diff;
  72. /* *INDENT-OFF* */
  73. /* Symbol(s) 0 [1-9] others
  74. Transition (10) 0 (01) d (00) x */
  75. static const unsigned char next_state[] = { /* state x d 0 */
  76. /* S_N */ S_N, S_I, S_Z,
  77. /* S_I */ S_N, S_I, S_I,
  78. /* S_F */ S_N, S_F, S_F,
  79. /* S_Z */ S_N, S_F, S_Z
  80. };
  81. static const signed char result_type[] = {
  82. /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
  83. /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
  84. /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
  85. /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  86. /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
  87. };
  88. /* *INDENT-ON* */
  89. if (p1 == p2)
  90. return 0;
  91. c1 = *p1++;
  92. c2 = *p2++;
  93. /* Hint: '0' is a digit too. */
  94. state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
  95. while ((diff = c1 - c2) == 0)
  96. {
  97. if (c1 == '\0')
  98. return diff;
  99. state = next_state[state];
  100. c1 = *p1++;
  101. c2 = *p2++;
  102. state += (c1 == '0') + (isdigit (c1) != 0);
  103. }
  104. state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
  105. switch (state)
  106. {
  107. case CMP:
  108. return diff;
  109. case LEN:
  110. while (isdigit (*p1++))
  111. if (!isdigit (*p2++))
  112. return 1;
  113. return isdigit (*p2) ? -1 : diff;
  114. default:
  115. return state;
  116. }
  117. #endif /* HAVE_STRVERSCMP */
  118. }
  119. /* --------------------------------------------------------------------------------------------- */