strverscmp.c 3.4 KB

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