strverscmp.c 4.6 KB

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