1 | // SuperTux |
2 | // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com> |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | |
17 | #include "string_util.hpp" |
18 | |
19 | #include <algorithm> |
20 | #include <string> |
21 | #include <string.h> |
22 | |
23 | bool |
24 | StringUtil::has_suffix(const std::string& data, const std::string& suffix) |
25 | { |
26 | return data.length() >= suffix.length() |
27 | && data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0; |
28 | } |
29 | |
30 | bool |
31 | StringUtil::numeric_less(const std::string& lhs, const std::string& rhs) |
32 | { |
33 | std::string::size_type i = 0; |
34 | std::string::size_type min_len = std::min(lhs.size(), rhs.size()); |
35 | |
36 | while (i < min_len) |
37 | { |
38 | if (isdigit(lhs[i]) && isdigit(rhs[i])) |
39 | { |
40 | // have two digits, so check which number is smaller |
41 | std::string::size_type li = i+1; |
42 | std::string::size_type ri = i+1; |
43 | |
44 | // find the end of the number in both strings |
45 | while (li < lhs.size() && isdigit(lhs[li])) { li += 1; } |
46 | while (ri < rhs.size() && isdigit(rhs[ri])) { ri += 1; } |
47 | |
48 | if (li == ri) |
49 | { |
50 | // end is at the same point in both strings, so do a detaile |
51 | // comparism of the numbers |
52 | for (std::string::size_type j = i; j < li; ++j) |
53 | { |
54 | if (lhs[j] != rhs[j]) |
55 | { |
56 | return lhs[j] < rhs[j]; |
57 | } |
58 | } |
59 | |
60 | // numbers are the same, so jump to the end of the number and compare |
61 | i = li; |
62 | } |
63 | else |
64 | { |
65 | // numbers have different numbers of digits, so the number |
66 | // with the least digits wins |
67 | return li < ri; |
68 | } |
69 | } |
70 | else |
71 | { |
72 | // do normal character comparism |
73 | if (lhs[i] != rhs[i]) |
74 | { |
75 | return lhs[i] < rhs[i]; |
76 | } |
77 | else |
78 | { |
79 | // strings are the same so far, so continue |
80 | i += 1; |
81 | } |
82 | } |
83 | } |
84 | |
85 | return lhs.size() < rhs.size(); |
86 | } |
87 | |
88 | std::string |
89 | StringUtil::tolower(const std::string& text) |
90 | { |
91 | std::string result = text; |
92 | std::transform(result.begin(), result.end(), result.begin(), ::tolower); |
93 | return result; |
94 | } |
95 | |
96 | /* EOF */ |
97 | |