strverscmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Compare strings while treating digits characters numerically.
  3. Copyright (C) 1997, 2002, 2011
  4. The 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 <ctype.h>
  20. #ifdef HAVE_STRVERSCMP
  21. #include <string.h>
  22. #endif /* HAVE_STRVERSCMP */
  23. #include "lib/strutil.h"
  24. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  25. fractionnal parts, S_Z: idem but with leading Zeroes only */
  26. #define S_N 0x0
  27. #define S_I 0x4
  28. #define S_F 0x8
  29. #define S_Z 0xC
  30. /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  31. #define CMP 2
  32. #define LEN 3
  33. /* Compare S1 and S2 as strings holding indices/version numbers,
  34. returning less than, equal to or greater than zero if S1 is less than,
  35. equal to or greater than S2 (for more info, see the texinfo doc).
  36. */
  37. int
  38. str_verscmp (const char *s1, const char *s2)
  39. {
  40. #ifdef HAVE_STRVERSCMP
  41. return strverscmp (s1, s2);
  42. #else /* HAVE_STRVERSCMP */
  43. unsigned char *p1 = (unsigned char *) s1;
  44. unsigned char *p2 = (unsigned char *) s2;
  45. unsigned char c1, c2;
  46. int state;
  47. int diff;
  48. /* Symbol(s) 0 [1-9] others (padding)
  49. Transition (10) 0 (01) d (00) x (11) - */
  50. static const unsigned int next_state[] = {
  51. /* state x d 0 - */
  52. /* S_N */ S_N, S_I, S_Z, S_N,
  53. /* S_I */ S_N, S_I, S_I, S_I,
  54. /* S_F */ S_N, S_F, S_F, S_F,
  55. /* S_Z */ S_N, S_F, S_Z, S_Z
  56. };
  57. static const int result_type[] = {
  58. /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
  59. 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
  60. /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
  61. CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  62. /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
  63. +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
  64. /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
  65. CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  66. /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
  67. -1, CMP, CMP, CMP
  68. };
  69. if (p1 == p2)
  70. return 0;
  71. c1 = *p1++;
  72. c2 = *p2++;
  73. /* Hint: '0' is a digit too. */
  74. state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
  75. while ((diff = c1 - c2) == 0 && c1 != '\0')
  76. {
  77. state = next_state[state];
  78. c1 = *p1++;
  79. c2 = *p2++;
  80. state |= (c1 == '0') + (isdigit (c1) != 0);
  81. }
  82. state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
  83. switch (state)
  84. {
  85. case CMP:
  86. return diff;
  87. case LEN:
  88. while (isdigit (*p1++))
  89. if (!isdigit (*p2++))
  90. return 1;
  91. return isdigit (*p2) ? -1 : diff;
  92. default:
  93. return state;
  94. }
  95. #endif /* HAVE_STRVERSCMP */
  96. }