DAS  3.0
Das Analysis System
strnatcmp.c File Reference
#include <stddef.h>
#include <ctype.h>
#include "strnatcmp.h"
+ Include dependency graph for strnatcmp.c:

Functions

static int nat_isdigit (nat_char a)
 
static int nat_isspace (nat_char a)
 
static nat_char nat_toupper (nat_char a)
 
static int compare_right (nat_char const *a, nat_char const *b)
 
static int compare_left (nat_char const *a, nat_char const *b)
 
static int strnatcmp0 (nat_char const *a, nat_char const *b, int fold_case)
 
int strnatcmp (nat_char const *a, nat_char const *b)
 
int strnatcasecmp (nat_char const *a, nat_char const *b)
 

Function Documentation

◆ compare_left()

static int compare_left ( nat_char const *  a,
nat_char const *  b 
)
static
94 {
95  /* Compare two left-aligned numbers: the first to have a
96  different value wins. */
97  for (;; a++, b++) {
98  if (!nat_isdigit(*a) && !nat_isdigit(*b))
99  return 0;
100  if (!nat_isdigit(*a))
101  return -1;
102  if (!nat_isdigit(*b))
103  return +1;
104  if (*a < *b)
105  return -1;
106  if (*a > *b)
107  return +1;
108  }
109 
110  return 0;
111 }

◆ compare_right()

static int compare_right ( nat_char const *  a,
nat_char const *  b 
)
static
64 {
65  int bias = 0;
66 
67  /* The longest run of digits wins. That aside, the greatest
68  value wins, but we can't know that it will until we've scanned
69  both numbers to know that they have the same magnitude, so we
70  remember it in BIAS. */
71  for (;; a++, b++) {
72  if (!nat_isdigit(*a) && !nat_isdigit(*b))
73  return bias;
74  if (!nat_isdigit(*a))
75  return -1;
76  if (!nat_isdigit(*b))
77  return +1;
78  if (*a < *b) {
79  if (!bias)
80  bias = -1;
81  } else if (*a > *b) {
82  if (!bias)
83  bias = +1;
84  } else if (!*a && !*b)
85  return bias;
86  }
87 
88  return 0;
89 }

◆ nat_isdigit()

static int nat_isdigit ( nat_char  a)
inlinestatic
43 {
44  return isdigit((unsigned char) a);
45 }

◆ nat_isspace()

static int nat_isspace ( nat_char  a)
inlinestatic
50 {
51  return isspace((unsigned char) a);
52 }

◆ nat_toupper()

static nat_char nat_toupper ( nat_char  a)
inlinestatic
57 {
58  return toupper((unsigned char) a);
59 }

◆ strnatcasecmp()

int strnatcasecmp ( nat_char const *  a,
nat_char const *  b 
)
175  {
176  return strnatcmp0(a, b, 1);
177 }

◆ strnatcmp()

int strnatcmp ( nat_char const *  a,
nat_char const *  b 
)
168  {
169  return strnatcmp0(a, b, 0);
170 }

◆ strnatcmp0()

static int strnatcmp0 ( nat_char const *  a,
nat_char const *  b,
int  fold_case 
)
static
116 {
117  int ai, bi;
118  nat_char ca, cb;
119  int fractional, result;
120 
121  ai = bi = 0;
122  while (1) {
123  ca = a[ai]; cb = b[bi];
124 
125  /* skip over leading spaces or zeros */
126  while (nat_isspace(ca))
127  ca = a[++ai];
128 
129  while (nat_isspace(cb))
130  cb = b[++bi];
131 
132  /* process run of digits */
133  if (nat_isdigit(ca) && nat_isdigit(cb)) {
134  fractional = (ca == '0' || cb == '0');
135 
136  if (fractional) {
137  if ((result = compare_left(a+ai, b+bi)) != 0)
138  return result;
139  } else {
140  if ((result = compare_right(a+ai, b+bi)) != 0)
141  return result;
142  }
143  }
144 
145  if (!ca && !cb) {
146  /* The strings compare the same. Perhaps the caller
147  will want to call strcmp to break the tie. */
148  return 0;
149  }
150 
151  if (fold_case) {
152  ca = nat_toupper(ca);
153  cb = nat_toupper(cb);
154  }
155 
156  if (ca < cb)
157  return -1;
158 
159  if (ca > cb)
160  return +1;
161 
162  ++ai; ++bi;
163  }
164 }
nat_isspace
static int nat_isspace(nat_char a)
Definition: strnatcmp.c:49
nat_char
char nat_char
Definition: strnatcmp.h:31
compare_left
static int compare_left(nat_char const *a, nat_char const *b)
Definition: strnatcmp.c:93
nat_toupper
static nat_char nat_toupper(nat_char a)
Definition: strnatcmp.c:56
nat_isdigit
static int nat_isdigit(nat_char a)
Definition: strnatcmp.c:42
compare_right
static int compare_right(nat_char const *a, nat_char const *b)
Definition: strnatcmp.c:63
strnatcmp0
static int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
Definition: strnatcmp.c:115