forked from hove-io/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphanum.hpp
228 lines (192 loc) · 7.17 KB
/
alphanum.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
// from http://www.davekoelle.com/files/alphanum.hpp
/*
Released under the MIT License - https://opensource.org/licenses/MIT
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* $Header: /code/doj/alphanum.hpp,v 1.3 2008/01/28 23:06:47 doj Exp $ */
#include <cassert>
#include <functional>
#include <string>
#include <sstream>
namespace doj {
// anonymous namespace for functions we use internally. But if you
// are coding in C, you can use alphanum_impl() directly, since it
// uses not C++ features.
namespace {
/** this function does not consider the current locale and only
works with ASCII digits.
@return true if c is a digit character
*/
bool alphanum_isdigit(const char c) {
return c >= '0' && c <= '9';
}
/**
compare l and r with strcmp() semantics, but using
the "Alphanum Algorithm". This function is designed to read
through the l and r strings only one time, for
maximum performance. It does not allocate memory for
substrings. It can either use the C-library functions isdigit()
and atoi() to honour your locale settings, when recognizing
digit characters when you "#define ALPHANUM_LOCALE=1" or use
it's own digit character handling which only works with ASCII
digit characters, but provides better performance.
@param l NULL-terminated C-style string
@param r NULL-terminated C-style string
@return negative if l<r, 0 if l equals r, positive if l>r
*/
int alphanum_impl(const char* l, const char* r) {
enum mode_t { STRING, NUMBER } mode = STRING;
while (*l && *r) {
if (mode == STRING) {
char l_char, r_char;
while ((l_char = *l) && (r_char = *r)) {
// check if this are digit characters
const bool l_digit = alphanum_isdigit(l_char), r_digit = alphanum_isdigit(r_char);
// if both characters are digits, we continue in NUMBER mode
if (l_digit && r_digit) {
mode = NUMBER;
break;
}
// if only the left character is a digit, we have a result
if (l_digit)
return -1;
// if only the right character is a digit, we have a result
if (r_digit)
return +1;
// compute the difference of both characters
const int diff = l_char - r_char;
// if they differ we have a result
if (diff != 0)
return diff;
// otherwise process the next characters
++l;
++r;
}
} else // mode==NUMBER
{
// get the left number
unsigned long l_int = 0;
while (*l && alphanum_isdigit(*l)) {
// TODO: this can overflow
l_int = l_int * 10 + *l - '0';
++l;
}
// get the right number
unsigned long r_int = 0;
while (*r && alphanum_isdigit(*r)) {
// TODO: this can overflow
r_int = r_int * 10 + *r - '0';
++r;
}
// if the difference is not equal to zero, we have a comparison result
const long diff = l_int - r_int;
if (diff != 0)
return diff;
// otherwise we process the next substring in STRING mode
mode = STRING;
}
}
if (*r)
return -1;
if (*l)
return +1;
return 0;
}
} // namespace
/**
Compare left and right with the same semantics as strcmp(), but with the
"Alphanum Algorithm" which produces more human-friendly
results. The classes lT and rT must implement "std::ostream
operator<< (std::ostream&, const Ty&)".
@return negative if left<right, 0 if left==right, positive if left>right.
*/
template <typename lT, typename rT>
int alphanum_comp(const lT& left, const rT& right) {
std::ostringstream l;
l << left;
std::ostringstream r;
r << right;
return alphanum_impl(l.str().c_str(), r.str().c_str());
}
/**
Compare l and r with the same semantics as strcmp(), but with
the "Alphanum Algorithm" which produces more human-friendly
results.
@return negative if l<r, 0 if l==r, positive if l>r.
*/
template <>
int alphanum_comp<std::string>(const std::string& l, const std::string& r) {
return alphanum_impl(l.c_str(), r.c_str());
}
////////////////////////////////////////////////////////////////////////////
// now follow a lot of overloaded alphanum_comp() functions to get a
// direct call to alphanum_impl() upon the various combinations of c
// and c++ strings.
/**
Compare l and r with the same semantics as strcmp(), but with
the "Alphanum Algorithm" which produces more human-friendly
results.
@return negative if l<r, 0 if l==r, positive if l>r.
*/
inline int alphanum_comp(char* l, char* r) {
assert(l);
assert(r);
return alphanum_impl(l, r);
}
inline int alphanum_comp(const char* l, const char* r) {
assert(l);
assert(r);
return alphanum_impl(l, r);
}
inline int alphanum_comp(char* l, const char* r) {
assert(l);
assert(r);
return alphanum_impl(l, r);
}
inline int alphanum_comp(const char* l, char* r) {
assert(l);
assert(r);
return alphanum_impl(l, r);
}
inline int alphanum_comp(const std::string& l, char* r) {
assert(r);
return alphanum_impl(l.c_str(), r);
}
inline int alphanum_comp(char* l, const std::string& r) {
assert(l);
return alphanum_impl(l, r.c_str());
}
inline int alphanum_comp(const std::string& l, const char* r) {
assert(r);
return alphanum_impl(l.c_str(), r);
}
inline int alphanum_comp(const char* l, const std::string& r) {
assert(l);
return alphanum_impl(l, r.c_str());
}
////////////////////////////////////////////////////////////////////////////
/**
Functor class to compare two objects with the "Alphanum
Algorithm". If the objects are no std::string, they must
implement "std::ostream operator<< (std::ostream&, const Ty&)".
*/
template <class Ty>
struct alphanum_less : public std::binary_function<Ty, Ty, bool> {
bool operator()(const Ty& left, const Ty& right) const { return alphanum_comp(left, right) < 0; }
};
} // namespace doj