strverscmp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Compare strings while treating digits characters numerically.
  3. Copyright (C) 1997-2021
  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[] =
  76. {
  77. /* state x d 0 */
  78. /* S_N */ S_N, S_I, S_Z,
  79. /* S_I */ S_N, S_I, S_I,
  80. /* S_F */ S_N, S_F, S_F,
  81. /* S_Z */ S_N, S_F, S_Z
  82. };
  83. static const signed char result_type[] =
  84. {
  85. /* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
  86. /* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
  87. /* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
  88. /* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  89. /* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP
  90. };
  91. /* *INDENT-ON* */
  92. if (p1 == p2)
  93. return 0;
  94. c1 = *p1++;
  95. c2 = *p2++;
  96. /* Hint: '0' is a digit too. */
  97. state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
  98. while ((diff = c1 - c2) == 0)
  99. {
  100. if (c1 == '\0')
  101. return diff;
  102. state = next_state[state];
  103. c1 = *p1++;
  104. c2 = *p2++;
  105. state += (c1 == '0') + (isdigit (c1) != 0);
  106. }
  107. state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
  108. switch (state)
  109. {
  110. case CMP:
  111. return diff;
  112. case LEN:
  113. while (isdigit (*p1++))
  114. if (!isdigit (*p2++))
  115. return 1;
  116. return isdigit (*p2) ? -1 : diff;
  117. default:
  118. return state;
  119. }
  120. #endif /* HAVE_STRVERSCMP */
  121. }
  122. /* --------------------------------------------------------------------------------------------- */