strverscmp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, write to the Free
  18. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. 02111-1307 USA. */
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdint.h>
  23. /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
  24. fractional parts, S_Z: idem but with leading Zeroes only */
  25. #define S_N 0x0
  26. #define S_I 0x4
  27. #define S_F 0x8
  28. #define S_Z 0xC
  29. /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
  30. #define CMP 2
  31. #define LEN 3
  32. /* using more efficient isdigit() */
  33. #undef isdigit
  34. #define isdigit(a) ((unsigned)((a) - '0') <= 9)
  35. /* Compare S1 and S2 as strings holding indices/version numbers,
  36. returning less than, equal to or greater than zero if S1 is less than,
  37. equal to or greater than S2 (for more info, see the texinfo doc).
  38. */
  39. int strverscmp (const char *s1, const char *s2)
  40. {
  41. const unsigned char *p1 = (const unsigned char *) s1;
  42. const unsigned char *p2 = (const unsigned char *) s2;
  43. unsigned char c1, c2;
  44. int state;
  45. int diff;
  46. /* Symbol(s) 0 [1-9] others (padding)
  47. Transition (10) 0 (01) d (00) x (11) - */
  48. static const uint8_t next_state[] =
  49. {
  50. /* state x d 0 - */
  51. /* S_N */ S_N, S_I, S_Z, S_N,
  52. /* S_I */ S_N, S_I, S_I, S_I,
  53. /* S_F */ S_N, S_F, S_F, S_F,
  54. /* S_Z */ S_N, S_F, S_Z, S_Z
  55. };
  56. static const int8_t result_type[] =
  57. {
  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. }
  96. libc_hidden_def(strverscmp)