1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * Benjamin Grauer |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "String.h" |
---|
29 | |
---|
30 | /** |
---|
31 | @brief Removes all whitespaces from a string. |
---|
32 | @param str The string to strip |
---|
33 | */ |
---|
34 | void strip(std::string* str) |
---|
35 | { |
---|
36 | unsigned int pos; |
---|
37 | while ((pos = (*str).find(" ")) < (*str).length()) |
---|
38 | (*str).erase(pos, 1); |
---|
39 | while ((pos = (*str).find("\t")) < (*str).length()) |
---|
40 | (*str).erase(pos, 1); |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | @brief Returns a copy of a string without whitespaces. |
---|
45 | @param str The string to strip |
---|
46 | @return The stripped line |
---|
47 | */ |
---|
48 | std::string getStripped(const std::string& str) |
---|
49 | { |
---|
50 | std::string output = std::string(str); |
---|
51 | strip(&output); |
---|
52 | return output; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | @brief Determines if a string in is a comment. |
---|
57 | @param str The string to check |
---|
58 | @return True = it's a comment |
---|
59 | |
---|
60 | A comment is defined by a leading '#', '%', ';' or '//'. |
---|
61 | */ |
---|
62 | bool isComment(const std::string& str) |
---|
63 | { |
---|
64 | // Strip the line, whitespaces are disturbing |
---|
65 | std::string teststring = getStripped(str); |
---|
66 | |
---|
67 | // There are four possible comment-symbols: |
---|
68 | // 1) #comment in script-language style |
---|
69 | // 2) %comment in matlab style |
---|
70 | // 3) ;comment in unreal tournament config-file style |
---|
71 | // 4) //comment in code style |
---|
72 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/')) |
---|
73 | return true; |
---|
74 | |
---|
75 | return false; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | @brief Determines if a string is empty (contains only whitespaces). |
---|
80 | @param str The string to check |
---|
81 | @return True = it's empty |
---|
82 | */ |
---|
83 | bool isEmpty(const std::string& str) |
---|
84 | { |
---|
85 | std::string temp = getStripped(str); |
---|
86 | return ((temp == "") || (temp.size() == 0)); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | @brief Determines if a string contains only numbers and maximal one '.'. |
---|
91 | @param str The string to check |
---|
92 | @return True = it's a number |
---|
93 | */ |
---|
94 | bool isNumeric(const std::string& str) |
---|
95 | { |
---|
96 | bool foundPoint = false; |
---|
97 | |
---|
98 | for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) |
---|
99 | { |
---|
100 | if (((*it) < '0' || (*it) > '9')) |
---|
101 | { |
---|
102 | if ((*it) != '.' && !foundPoint) |
---|
103 | foundPoint = true; |
---|
104 | else |
---|
105 | return false; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | return true; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | @brief Replaces each char between A and Z with its lowercase equivalent. |
---|
114 | @param str The string to convert |
---|
115 | */ |
---|
116 | void lowercase(std::string* str) |
---|
117 | { |
---|
118 | static unsigned const char difference_between_A_and_a = 'A' - 'a'; |
---|
119 | |
---|
120 | for (std::string::iterator it = (*str).begin(); it != (*str).end(); ++it) |
---|
121 | if ((*it) >= 'A' && (*it) <= 'Z') |
---|
122 | (*it) -= difference_between_A_and_a; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | @brief Returns a copy of the given string without uppercase chars. |
---|
127 | @param str The string |
---|
128 | @return The copy |
---|
129 | */ |
---|
130 | std::string getLowercase(const std::string& str) |
---|
131 | { |
---|
132 | std::string output = std::string(str); |
---|
133 | lowercase(&output); |
---|
134 | return output; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | @brief Replaces each char between a and z with its uppercase equivalent. |
---|
139 | @param str The string to convert |
---|
140 | */ |
---|
141 | void uppercase(std::string* str) |
---|
142 | { |
---|
143 | static unsigned const char difference_between_A_and_a = 'A' - 'a'; |
---|
144 | |
---|
145 | for (std::string::iterator it = (*str).begin(); it != (*str).end(); ++it) |
---|
146 | if ((*it) >= 'a' && (*it) <= 'z') |
---|
147 | (*it) += difference_between_A_and_a; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | @brief Returns a copy of the given string without lowercase chars. |
---|
152 | @param str The string |
---|
153 | @return The copy |
---|
154 | */ |
---|
155 | std::string getUppercase(const std::string& str) |
---|
156 | { |
---|
157 | std::string output = std::string(str); |
---|
158 | uppercase(&output); |
---|
159 | return output; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * @brief compares two strings without ignoring the case |
---|
164 | * @param s1 first string |
---|
165 | * @param s2 second string |
---|
166 | */ |
---|
167 | int nocaseCmp(const std::string& s1, const std::string& s2) |
---|
168 | { |
---|
169 | std::string::const_iterator it1=s1.begin(); |
---|
170 | std::string::const_iterator it2=s2.begin(); |
---|
171 | |
---|
172 | //stop when either string's end has been reached |
---|
173 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
---|
174 | { |
---|
175 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
176 | // return -1 to indicate smaller than, 1 otherwise |
---|
177 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
178 | //proceed to the next character in each string |
---|
179 | ++it1; |
---|
180 | ++it2; |
---|
181 | } |
---|
182 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
---|
183 | //return -1,0 or 1 according to strings' lengths |
---|
184 | if (size1==size2) |
---|
185 | return 0; |
---|
186 | return (size1<size2) ? -1 : 1; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /** |
---|
191 | * @brief compares two strings without ignoring the case |
---|
192 | * @param s1 first string |
---|
193 | * @param s2 second string |
---|
194 | * @param len how far from the beginning to start. |
---|
195 | */ |
---|
196 | int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len) |
---|
197 | { |
---|
198 | if (len == 0) |
---|
199 | return 0; |
---|
200 | std::string::const_iterator it1=s1.begin(); |
---|
201 | std::string::const_iterator it2=s2.begin(); |
---|
202 | |
---|
203 | //stop when either string's end has been reached |
---|
204 | while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0) |
---|
205 | { |
---|
206 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
207 | // return -1 to indicate smaller than, 1 otherwise |
---|
208 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
209 | //proceed to the next character in each string |
---|
210 | ++it1; |
---|
211 | ++it2; |
---|
212 | } |
---|
213 | return 0; |
---|
214 | } |
---|