2
0

apr_strnatcmp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- mode: c; c-file-style: "k&r" -*-
  2. strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
  3. Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include "apr_strings.h"
  21. #include "apr_lib.h" /* for apr_is*() */
  22. #if defined(__GNUC__)
  23. # define UNUSED __attribute__((__unused__))
  24. #else
  25. # define UNUSED
  26. #endif
  27. /* based on "strnatcmp.c,v 1.6 2000/04/20 07:30:11 mbp Exp $" */
  28. static int
  29. compare_right(char const *a, char const *b)
  30. {
  31. int bias = 0;
  32. /* The longest run of digits wins. That aside, the greatest
  33. value wins, but we can't know that it will until we've scanned
  34. both numbers to know that they have the same magnitude, so we
  35. remember it in BIAS. */
  36. for (;; a++, b++) {
  37. if (!apr_isdigit(*a) && !apr_isdigit(*b))
  38. break;
  39. else if (!apr_isdigit(*a))
  40. return -1;
  41. else if (!apr_isdigit(*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. } else if (!*a && !*b)
  50. break;
  51. }
  52. return bias;
  53. }
  54. static int
  55. compare_left(char const *a, char const *b)
  56. {
  57. /* Compare two left-aligned numbers: the first to have a
  58. different value wins. */
  59. for (;; a++, b++) {
  60. if (!apr_isdigit(*a) && !apr_isdigit(*b))
  61. break;
  62. else if (!apr_isdigit(*a))
  63. return -1;
  64. else if (!apr_isdigit(*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. static int strnatcmp0(char const *a, char const *b, int fold_case)
  74. {
  75. int ai, bi;
  76. char ca, cb;
  77. int fractional, result;
  78. ai = bi = 0;
  79. while (1) {
  80. ca = a[ai]; cb = b[bi];
  81. /* skip over leading spaces or zeros */
  82. while (apr_isspace(ca))
  83. ca = a[++ai];
  84. while (apr_isspace(cb))
  85. cb = b[++bi];
  86. /* process run of digits */
  87. if (apr_isdigit(ca) && apr_isdigit(cb)) {
  88. fractional = (ca == '0' || cb == '0');
  89. if (fractional) {
  90. if ((result = compare_left(a+ai, b+bi)) != 0)
  91. return result;
  92. } else {
  93. if ((result = compare_right(a+ai, b+bi)) != 0)
  94. return result;
  95. }
  96. }
  97. if (!ca && !cb) {
  98. /* The strings compare the same. Perhaps the caller
  99. will want to call strcmp to break the tie. */
  100. return 0;
  101. }
  102. if (fold_case) {
  103. ca = apr_toupper(ca);
  104. cb = apr_toupper(cb);
  105. }
  106. if (ca < cb)
  107. return -1;
  108. else if (ca > cb)
  109. return +1;
  110. ++ai; ++bi;
  111. }
  112. }
  113. APR_DECLARE(int) apr_strnatcmp(char const *a, char const *b)
  114. {
  115. return strnatcmp0(a, b, 0);
  116. }
  117. /* Compare, recognizing numeric string and ignoring case. */
  118. APR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b)
  119. {
  120. return strnatcmp0(a, b, 1);
  121. }