strverscmp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Compare strings while treating digits characters numerically.
  2. Copyright (C) 1997, 2000, 2002, 2004 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. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program 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
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <string.h>
  20. #include <ctype.h>
  21. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  22. fractional parts, S_Z: idem but with leading Zeroes only */
  23. #define S_N 0x0
  24. #define S_I 0x4
  25. #define S_F 0x8
  26. #define S_Z 0xC
  27. /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  28. #define CMP 2
  29. #define LEN 3
  30. /* ISDIGIT differs from isdigit, as follows:
  31. - Its arg may be any int or unsigned int; it need not be an unsigned char.
  32. - It's guaranteed to evaluate its argument exactly once.
  33. - It's typically faster.
  34. POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
  35. ISDIGIT_LOCALE unless it's important to use the locale's definition
  36. of `digit' even when the host does not conform to POSIX. */
  37. #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
  38. #undef __strverscmp
  39. #undef strverscmp
  40. #ifndef weak_alias
  41. # define __strverscmp strverscmp
  42. #endif
  43. /* Compare S1 and S2 as strings holding indices/version numbers,
  44. returning less than, equal to or greater than zero if S1 is less than,
  45. equal to or greater than S2 (for more info, see the texinfo doc).
  46. */
  47. int
  48. __strverscmp (const char *s1, const char *s2)
  49. {
  50. const unsigned char *p1 = (const unsigned char *) s1;
  51. const unsigned char *p2 = (const unsigned char *) s2;
  52. unsigned char c1, c2;
  53. int state;
  54. int diff;
  55. /* Symbol(s) 0 [1-9] others (padding)
  56. Transition (10) 0 (01) d (00) x (11) - */
  57. static const unsigned int next_state[] =
  58. {
  59. /* state x d 0 - */
  60. /* S_N */ S_N, S_I, S_Z, S_N,
  61. /* S_I */ S_N, S_I, S_I, S_I,
  62. /* S_F */ S_N, S_F, S_F, S_F,
  63. /* S_Z */ S_N, S_F, S_Z, S_Z
  64. };
  65. static const int result_type[] =
  66. {
  67. /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
  68. 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
  69. /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
  70. CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  71. /* S_I */ CMP, -1, -1, CMP, 1, LEN, LEN, CMP,
  72. 1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
  73. /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
  74. CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
  75. /* S_Z */ CMP, 1, 1, CMP, -1, CMP, CMP, CMP,
  76. -1, CMP, CMP, CMP
  77. };
  78. if (p1 == p2)
  79. return 0;
  80. c1 = *p1++;
  81. c2 = *p2++;
  82. /* Hint: '0' is a digit too. */
  83. state = S_N | ((c1 == '0') + (ISDIGIT (c1) != 0));
  84. while ((diff = c1 - c2) == 0 && c1 != '\0')
  85. {
  86. state = next_state[state];
  87. c1 = *p1++;
  88. c2 = *p2++;
  89. state |= (c1 == '0') + (ISDIGIT (c1) != 0);
  90. }
  91. state = result_type[state << 2 | ((c2 == '0') + (ISDIGIT (c2) != 0))];
  92. switch (state)
  93. {
  94. case CMP:
  95. return diff;
  96. case LEN:
  97. while (ISDIGIT (*p1++))
  98. if (!ISDIGIT (*p2++))
  99. return 1;
  100. return ISDIGIT (*p2) ? -1 : diff;
  101. default:
  102. return state;
  103. }
  104. }
  105. #ifdef weak_alias
  106. weak_alias (__strverscmp, strverscmp)
  107. #endif