strnatcmp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  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 <stdio.h>
  22. #include "php.h"
  23. #include "php_string.h"
  24. /* {{{ compare_right */
  25. static int
  26. compare_right(char const **a, char const *aend, char const **b, char const *bend)
  27. {
  28. int bias = 0;
  29. /* The longest run of digits wins. That aside, the greatest
  30. value wins, but we can't know that it will until we've scanned
  31. both numbers to know that they have the same magnitude, so we
  32. remember it in BIAS. */
  33. for(;; (*a)++, (*b)++) {
  34. if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
  35. (*b == bend || !isdigit((int)(unsigned char)**b)))
  36. return bias;
  37. else if (*a == aend || !isdigit((int)(unsigned char)**a))
  38. return -1;
  39. else if (*b == bend || !isdigit((int)(unsigned char)**b))
  40. return +1;
  41. else if (**a < **b) {
  42. if (!bias)
  43. bias = -1;
  44. } else if (**a > **b) {
  45. if (!bias)
  46. bias = +1;
  47. }
  48. }
  49. return 0;
  50. }
  51. /* }}} */
  52. /* {{{ compare_left */
  53. static int
  54. compare_left(char const **a, char const *aend, char const **b, char const *bend)
  55. {
  56. /* Compare two left-aligned numbers: the first to have a
  57. different value wins. */
  58. for(;; (*a)++, (*b)++) {
  59. if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
  60. (*b == bend || !isdigit((int)(unsigned char)**b)))
  61. return 0;
  62. else if (*a == aend || !isdigit((int)(unsigned char)**a))
  63. return -1;
  64. else if (*b == bend || !isdigit((int)(unsigned char)**b))
  65. return +1;
  66. else if (**a < **b)
  67. return -1;
  68. else if (**a > **b)
  69. return +1;
  70. }
  71. return 0;
  72. }
  73. /* }}} */
  74. /* {{{ strnatcmp_ex */
  75. PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case)
  76. {
  77. unsigned char ca, cb;
  78. char const *ap, *bp;
  79. char const *aend = a + a_len,
  80. *bend = b + b_len;
  81. int fractional, result;
  82. if (a_len == 0 || b_len == 0) {
  83. return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1));
  84. }
  85. ap = a;
  86. bp = b;
  87. ca = *ap; cb = *bp;
  88. /* skip over leading zeros */
  89. while (ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) {
  90. ca = *++ap;
  91. }
  92. while (cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) {
  93. cb = *++bp;
  94. }
  95. while (1) {
  96. /* Skip consecutive whitespace */
  97. while (isspace((int)(unsigned char)ca)) {
  98. ca = *++ap;
  99. }
  100. while (isspace((int)(unsigned char)cb)) {
  101. cb = *++bp;
  102. }
  103. /* process run of digits */
  104. if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) {
  105. fractional = (ca == '0' || cb == '0');
  106. if (fractional)
  107. result = compare_left(&ap, aend, &bp, bend);
  108. else
  109. result = compare_right(&ap, aend, &bp, bend);
  110. if (result != 0)
  111. return result;
  112. else if (ap == aend && bp == bend)
  113. /* End of the strings. Let caller sort them out. */
  114. return 0;
  115. else if (ap == aend)
  116. return -1;
  117. else if (bp == bend)
  118. return 1;
  119. else {
  120. /* Keep on comparing from the current point. */
  121. ca = *ap; cb = *bp;
  122. }
  123. }
  124. if (fold_case) {
  125. ca = toupper((int)(unsigned char)ca);
  126. cb = toupper((int)(unsigned char)cb);
  127. }
  128. if (ca < cb)
  129. return -1;
  130. else if (ca > cb)
  131. return +1;
  132. ++ap; ++bp;
  133. if (ap >= aend && bp >= bend)
  134. /* The strings compare the same. Perhaps the caller
  135. will want to call strcmp to break the tie. */
  136. return 0;
  137. else if (ap >= aend)
  138. return -1;
  139. else if (bp >= bend)
  140. return 1;
  141. ca = *ap; cb = *bp;
  142. }
  143. }
  144. /* }}} */