summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Levin <al.le@rockbox.org>2009-03-02 22:02:13 +0000
committerAlexander Levin <al.le@rockbox.org>2009-03-02 22:02:13 +0000
commit7389928d89b79c7270253f3da17f08520d95a874 (patch)
treefdead40ac9054cc1f70c1a0c5c0f629d32330c7c
parent4a54fc0eeced2e6b340f40fc6a447ef7bea38d6e (diff)
downloadrockbox-7389928d89b79c7270253f3da17f08520d95a874.tar.gz
rockbox-7389928d89b79c7270253f3da17f08520d95a874.zip
Make sure that sorting works both for signed and unsigned char (it depends on the platform/compiler used)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20183 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/common/strnatcmp.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/firmware/common/strnatcmp.c b/firmware/common/strnatcmp.c
index d7b7d1d398..4cc3065978 100644
--- a/firmware/common/strnatcmp.c
+++ b/firmware/common/strnatcmp.c
@@ -41,6 +41,12 @@
41 41
42#define assert(x) /* nothing */ 42#define assert(x) /* nothing */
43 43
44/* Convert char to int regardless of whether char is signed or not */
45static inline int
46to_int(char c)
47{
48 return (int) ((unsigned char) c);
49}
44 50
45/* These are defined as macros to make it easier to adapt this code to 51/* These are defined as macros to make it easier to adapt this code to
46 * different characters types or comparison functions. */ 52 * different characters types or comparison functions. */
@@ -70,25 +76,28 @@ static int
70compare_right(char const *a, char const *b) 76compare_right(char const *a, char const *b)
71{ 77{
72 int bias = 0; 78 int bias = 0;
79 int ca, cb;
73 80
74 /* The longest run of digits wins. That aside, the greatest 81 /* The longest run of digits wins. That aside, the greatest
75 value wins, but we can't know that it will until we've scanned 82 value wins, but we can't know that it will until we've scanned
76 both numbers to know that they have the same magnitude, so we 83 both numbers to know that they have the same magnitude, so we
77 remember it in BIAS. */ 84 remember it in BIAS. */
78 for (;; a++, b++) { 85 for (;; a++, b++) {
79 if (!nat_isdigit(*a) && !nat_isdigit(*b)) 86 ca = to_int(*a);
87 cb = to_int(*b);
88 if (!nat_isdigit(ca) && !nat_isdigit(cb))
80 return bias; 89 return bias;
81 else if (!nat_isdigit(*a)) 90 else if (!nat_isdigit(ca))
82 return -1; 91 return -1;
83 else if (!nat_isdigit(*b)) 92 else if (!nat_isdigit(cb))
84 return +1; 93 return +1;
85 else if (*a < *b) { 94 else if (ca < cb) {
86 if (!bias) 95 if (!bias)
87 bias = -1; 96 bias = -1;
88 } else if (*a > *b) { 97 } else if (ca > cb) {
89 if (!bias) 98 if (!bias)
90 bias = +1; 99 bias = +1;
91 } else if (!*a && !*b) 100 } else if (!ca && !cb)
92 return bias; 101 return bias;
93 } 102 }
94 103
@@ -104,14 +113,15 @@ static int strnatcmp0(char const *a, char const *b, int fold_case)
104 assert(a && b); 113 assert(a && b);
105 ai = bi = 0; 114 ai = bi = 0;
106 while (1) { 115 while (1) {
107 ca = a[ai]; cb = b[bi]; 116 ca = to_int(a[ai]);
117 cb = to_int(b[bi]);
108 118
109 /* skip over leading spaces or zeros */ 119 /* skip over leading spaces or zeros */
110 while (nat_isspace(ca)) 120 while (nat_isspace(ca))
111 ca = a[++ai]; 121 ca = to_int(a[++ai]);
112 122
113 while (nat_isspace(cb)) 123 while (nat_isspace(cb))
114 cb = b[++bi]; 124 cb = to_int(b[++bi]);
115 125
116 /* process run of digits */ 126 /* process run of digits */
117 if (nat_isdigit(ca) && nat_isdigit(cb)) { 127 if (nat_isdigit(ca) && nat_isdigit(cb)) {