strnatcmp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* -*- mode: c; c-file-style: "k&r" -*-
  2. Modified for PHP by Andrei Zmievski <andrei@ispi.net>
  3. strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
  4. Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include "php.h"
  24. #include "php_string.h"
  25. /* {{{ compare_right
  26. */
  27. static int
  28. compare_right(char const **a, char const *aend, char const **b, char const *bend)
  29. {
  30. int bias = 0;
  31. /* The longest run of digits wins. That aside, the greatest
  32. value wins, but we can't know that it will until we've scanned
  33. both numbers to know that they have the same magnitude, so we
  34. remember it in BIAS. */
  35. for(;; (*a)++, (*b)++) {
  36. if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
  37. (*b == bend || !isdigit((int)(unsigned char)**b)))
  38. return bias;
  39. else if (*a == aend || !isdigit((int)(unsigned char)**a))
  40. return -1;
  41. else if (*b == bend || !isdigit((int)(unsigned char)**b))
  42. return +1;
  43. else if (**a < **b) {
  44. if (!bias)
  45. bias = -1;
  46. } else if (**a > **b) {
  47. if (!bias)
  48. bias = +1;
  49. }
  50. }
  51. return 0;
  52. }
  53. /* }}} */
  54. /* {{{ compare_left
  55. */
  56. static int
  57. compare_left(char const **a, char const *aend, char const **b, char const *bend)
  58. {
  59. /* Compare two left-aligned numbers: the first to have a
  60. different value wins. */
  61. for(;; (*a)++, (*b)++) {
  62. if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
  63. (*b == bend || !isdigit((int)(unsigned char)**b)))
  64. return 0;
  65. else if (*a == aend || !isdigit((int)(unsigned char)**a))
  66. return -1;
  67. else if (*b == bend || !isdigit((int)(unsigned char)**b))
  68. return +1;
  69. else if (**a < **b)
  70. return -1;
  71. else if (**a > **b)
  72. return +1;
  73. }
  74. return 0;
  75. }
  76. /* }}} */
  77. /* {{{ strnatcmp_ex
  78. */
  79. PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case)
  80. {
  81. unsigned char ca, cb;
  82. char const *ap, *bp;
  83. char const *aend = a + a_len,
  84. *bend = b + b_len;
  85. int fractional, result;
  86. short leading = 1;
  87. if (a_len == 0 || b_len == 0) {
  88. return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1));
  89. }
  90. ap = a;
  91. bp = b;
  92. while (1) {
  93. ca = *ap; cb = *bp;
  94. /* skip over leading zeros */
  95. while (leading && ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) {
  96. ca = *++ap;
  97. }
  98. while (leading && cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) {
  99. cb = *++bp;
  100. }
  101. leading = 0;
  102. /* Skip consecutive whitespace */
  103. while (isspace((int)(unsigned char)ca)) {
  104. ca = *++ap;
  105. }
  106. while (isspace((int)(unsigned char)cb)) {
  107. cb = *++bp;
  108. }
  109. /* process run of digits */
  110. if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) {
  111. fractional = (ca == '0' || cb == '0');
  112. if (fractional)
  113. result = compare_left(&ap, aend, &bp, bend);
  114. else
  115. result = compare_right(&ap, aend, &bp, bend);
  116. if (result != 0)
  117. return result;
  118. else if (ap == aend && bp == bend)
  119. /* End of the strings. Let caller sort them out. */
  120. return 0;
  121. else if (ap == aend)
  122. return -1;
  123. else if (bp == bend)
  124. return 1;
  125. else {
  126. /* Keep on comparing from the current point. */
  127. ca = *ap; cb = *bp;
  128. }
  129. }
  130. if (fold_case) {
  131. ca = toupper((int)(unsigned char)ca);
  132. cb = toupper((int)(unsigned char)cb);
  133. }
  134. if (ca < cb)
  135. return -1;
  136. else if (ca > cb)
  137. return +1;
  138. ++ap; ++bp;
  139. if (ap >= aend && bp >= bend)
  140. /* The strings compare the same. Perhaps the caller
  141. will want to call strcmp to break the tie. */
  142. return 0;
  143. else if (ap >= aend)
  144. return -1;
  145. else if (bp >= bend)
  146. return 1;
  147. }
  148. }
  149. /* }}} */
  150. /*
  151. * Local variables:
  152. * tab-width: 4
  153. * c-basic-offset: 4
  154. * End:
  155. * vim600: sw=4 ts=4 fdm=marker
  156. * vim<600: sw=4 ts=4
  157. */