strverscmp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifdef HAVE_STRVERSCMP
  32. #include <string.h>
  33. #endif /* HAVE_STRVERSCMP */
  34. #include "lib/strutil.h"
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. #ifndef HAVE_STRVERSCMP
  38. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  39. fractional parts, S_Z: idem but with leading Zeroes only */
  40. #define S_N 0x0
  41. #define S_I 0x3
  42. #define S_F 0x6
  43. #define S_Z 0x9
  44. /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  45. #define CMP 2
  46. #define LEN 3
  47. #endif /* HAVE_STRVERSCMP */
  48. /*** file scope type declarations ****************************************************************/
  49. /*** file scope variables ************************************************************************/
  50. /* --------------------------------------------------------------------------------------------- */
  51. /*** file scope functions ************************************************************************/
  52. /* --------------------------------------------------------------------------------------------- */
  53. /* --------------------------------------------------------------------------------------------- */
  54. /*** public functions ****************************************************************************/
  55. /* --------------------------------------------------------------------------------------------- */
  56. /* Compare S1 and S2 as strings holding indices/version numbers,
  57. returning less than, equal to or greater than zero if S1 is less than,
  58. equal to or greater than S2 (for more info, see the texinfo doc).
  59. */
  60. int
  61. str_verscmp (const char *s1, const char *s2)
  62. {
  63. #ifdef HAVE_STRVERSCMP
  64. return strverscmp (s1, s2);
  65. #else /* HAVE_STRVERSCMP */
  66. const unsigned char *p1 = (const unsigned char *) s1;
  67. const unsigned char *p2 = (const unsigned char *) s2;
  68. unsigned char c1, c2;
  69. int state;
  70. int diff;
  71. /* *INDENT-OFF* */
  72. /* Symbol(s) 0 [1-9] others
  73. Transition (10) 0 (01) d (00) x */
  74. static const unsigned char next_state[] =
  75. {
  76. /* state x d 0 */
  77. /* S_N */ S_N, S_I, S_Z,
  78. /* S_I */ S_N, S_I, S_I,
  79. /* S_F */ S_N, S_F, S_F,
  80. /* S_Z */ S_N, S_F, S_Z
  81. };
  82. static const signed char result_type[] =
  83. {
  84. /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
  85. /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
  86. /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
  87. /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  88. /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
  89. };
  90. /* *INDENT-ON* */
  91. if (p1 == p2)
  92. return 0;
  93. c1 = *p1++;
  94. c2 = *p2++;
  95. /* Hint: '0' is a digit too. */
  96. state = S_N + ((c1 == '0') + (g_ascii_isdigit (c1) ? 1 : 0));
  97. while ((diff = c1 - c2) == 0)
  98. {
  99. if (c1 == '\0')
  100. return diff;
  101. state = next_state[state];
  102. c1 = *p1++;
  103. c2 = *p2++;
  104. state += (c1 == '0') + (g_ascii_isdigit (c1) ? 1 : 0);
  105. }
  106. state = result_type[state * 3 + (((c2 == '0') + (g_ascii_isdigit (c2) ? 1 : 0)))];
  107. switch (state)
  108. {
  109. case CMP:
  110. return diff;
  111. case LEN:
  112. while (g_ascii_isdigit (*p1++))
  113. if (!g_ascii_isdigit (*p2++))
  114. return 1;
  115. return g_ascii_isdigit (*p2) ? -1 : diff;
  116. default:
  117. return state;
  118. }
  119. #endif /* HAVE_STRVERSCMP */
  120. }
  121. /* --------------------------------------------------------------------------------------------- */