libcfe  0.12.1
some useful C-functions
xstrcoll.c
Go to the documentation of this file.
1 #include "config.h"
2 #include "xstrcoll.h"
3 
4 #include <string.h>
5 
6 #include "len.h"
7 
8 #ifndef MIN
9 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
10 #endif
11 
12 #ifndef NONZERO
13 # define NONZERO(x) ((x) != 0)
14 #endif
15 
20 static char sortorder[] = "/\\~=-+!@#$%^&*()_{}[]|:;\"ยจ'?,.`0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz ";
21 
22 int str_compare(void *a, void *b)
23 {
24  return xstrcoll((char *)a, str_len(a), (char *)b, str_len(b));
25 }
26 
27 int xstrcoll(const char *a, size_t alen, const char *b, size_t blen)
28 {
29  if (alen == 0)
30  return - NONZERO(blen);
31  else if (blen == 0)
32  return 1;
33 
34  if(alen == blen && memcmp(a, b, alen) == 0)
35  return 0;
36 
37  char *s = (char *)sortorder;
38  int slen = str_len(s);
39  int j, aval, bval;
40  size_t i;
41  i = j = aval = bval = 0;
42 
43  while(i < MIN(alen, blen))
44  {
45  if(memcmp(a + i, b + i, 1) != 0)
46  {
47  j = aval = bval = 0;
48  while(j < slen && (aval == 0 || bval == 0))
49  {
50  if(s[j] == a[i])
51  aval = j;
52  if(s[j] == b[i])
53  bval = j;
54  j++;
55  }
56  if(aval != bval)
57  return aval - bval;
58  }
59  i++;
60  }
61 
62  if(i == MIN(alen, blen) && alen < blen)
63  return -1;
64  else if(i == MIN(alen, blen) && alen > blen)
65  return 1;
66  else
67  return 0; /* or error? */
68 }
int str_compare(void *a, void *b)
Definition: xstrcoll.c:22
#define str_len(s)
Shorthand for counting '\0' terminating strings. See _len for more info.
Definition: len.h:17
#define NONZERO(x)
Definition: xstrcoll.c:13
int xstrcoll(const char *a, size_t alen, const char *b, size_t blen)
Definition: xstrcoll.c:27
#define MIN(a, b)
Definition: xstrcoll.c:9
#define a