Lines 1-3
Link Here
|
1 |
// vim:set et cinoptions=g0,t0,^-2,(0 sw=4 ts=4: |
1 |
// vim:set noet cinoptions=g0,t0,(0 sw=4 ts=4: |
2 |
// This file is part of the eix project and distributed under the |
2 |
// This file is part of the eix project and distributed under the |
3 |
// terms of the GNU General Public License v2. |
3 |
// terms of the GNU General Public License v2. |
Lines 5-8
Link Here
|
5 |
// Copyright (c) |
5 |
// Copyright (c) |
6 |
// Emil Beinroth <emilbeinroth@gmx.net> |
6 |
// Emil Beinroth <emilbeinroth@gmx.net> |
|
|
7 |
// Martin VÀth <vaeth@mathematik.uni-wuerzburg.de> |
7 |
|
8 |
|
8 |
#ifndef __GUARD__COMPARE_H__ |
9 |
#ifndef __GUARD__COMPARE_H__ |
Lines 14-47
Link Here
|
14 |
namespace eix |
15 |
namespace eix |
15 |
{ |
16 |
{ |
16 |
/// compare two objects. |
17 |
/// compare two objects. |
17 |
/// @return 0 if equal, 1 if left > right or -1 if left < right. |
18 |
/// @return 0 if equal, 1 if left > right or -1 if left < right. |
18 |
template<typename T> |
19 |
template<typename T> |
19 |
int default_compare(const T& left, const T& right) |
20 |
int default_compare(const T& left, const T& right) |
20 |
{ |
21 |
{ |
21 |
if (left == right) |
22 |
if (left == right) |
22 |
return 0; |
23 |
return 0; |
23 |
else if(left < right) |
24 |
if(left < right) |
24 |
return -1; |
25 |
return -1; |
25 |
else |
26 |
return 1; |
26 |
return 1; |
27 |
} |
27 |
} |
|
|
28 |
|
28 |
|
29 |
/// numeric comparison. |
29 |
/// numeric comparison. |
30 |
/// @note empty strings count a "0" |
30 |
/// @note empty strings count a "0" |
31 |
static inline int |
31 |
static inline int |
32 |
numeric_compare(const std::string& left, const std::string& right) |
32 |
numeric_compare(const std::string& left, const std::string& right) |
33 |
{ |
33 |
{ |
34 |
// strip leading 0's |
34 |
// strip leading 0's |
35 |
const std::string::size_type lstart = left.find_first_not_of('0'); |
35 |
std::string::size_type lstart = left.find_first_not_of('0'); |
36 |
const std::string::size_type rstart = right.find_first_not_of('0'); |
36 |
std::string::size_type rstart = right.find_first_not_of('0'); |
|
|
37 |
// Special cases: number is 0 or string is empty |
38 |
if (lstart == std::string::npos) { |
39 |
if(rstart == std::string::npos) |
40 |
return 0; |
41 |
return -1; |
42 |
} |
43 |
if (rstart == std::string::npos) |
44 |
return 1; |
37 |
|
45 |
|
38 |
// check if one is longer, that one would be bigger |
46 |
// check if one is longer, that one would be bigger |
39 |
const int size_result = default_compare(left.size() - (lstart == std::string::npos ? 0 : lstart), |
47 |
int size_result = default_compare(left.size() - lstart, right.size() - rstart); |
40 |
right.size() - (rstart == std::string::npos ? 0 : rstart)); |
48 |
if (size_result) |
41 |
if (size_result) |
49 |
return size_result; |
42 |
return size_result; |
50 |
// both strings have the same length, do string comparison |
43 |
// both strings have the same length, do string comparison |
51 |
return left.compare(lstart, std::string::npos, right, rstart, std::string::npos); |
44 |
return left.compare(lstart, std::string::npos, right, rstart, std::string::npos); |
52 |
} |
45 |
} |
|
|
46 |
} |
53 |
} |
47 |
|
54 |
|