strverscmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* GNU's strverscmp() function, taken from glibc 2.3.2 sources
  2. */
  3. /* Compare strings while treating digits characters numerically.
  4. Copyright (C) 1997, 2002 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. Derived work for uClibc by Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. The GNU C Library 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 GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with the GNU C Library; if not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdint.h>
  22. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  23. fractional 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. /* using more efficient isdigit() */
  32. #undef isdigit
  33. #define isdigit(a) ((unsigned)((a) - '0') <= 9)
  34. /* Compare S1 and S2 as strings holding indices/version numbers,
  35. returning less than, equal to or greater than zero if S1 is less than,
  36. equal to or greater than S2 (for more info, see the texinfo doc).
  37. */
  38. int strverscmp (const char *s1, const char *s2)
  39. {
  40. const unsigned char *p1 = (const unsigned char *) s1;
  41. const unsigned char *p2 = (const 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 uint8_t 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 int8_t 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. }
  95. libc_hidden_def(strverscmp)