]> git.lizzy.rs Git - minetest.git/blob - src/util/string.h
e46fbf4e9b2a48e3682eee3a317927290079cc13
[minetest.git] / src / util / string.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef UTIL_STRING_HEADER
21 #define UTIL_STRING_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include <stdlib.h>
25 #include <string>
26 #include <cstring>
27 #include <vector>
28 #include <sstream>
29 #include <cctype>
30
31 #define STRINGIFY(x) #x
32 #define TOSTRING(x) STRINGIFY(x)
33
34 struct FlagDesc {
35         const char *name;
36         u32 flag;
37 };
38
39 std::wstring narrow_to_wide(const std::string& mbs);
40 std::string wide_to_narrow(const std::wstring& wcs);
41
42 static inline std::string padStringRight(std::string s, size_t len)
43 {
44         if(len > s.size())
45                 s.insert(s.end(), len - s.size(), ' ');
46         return s;
47 }
48
49 // ends: NULL- or ""-terminated array of strings
50 // Returns "" if no end could be removed.
51 static inline std::string removeStringEnd(const std::string &s, const char *ends[])
52 {
53         const char **p = ends;
54         for(; (*p) && (*p)[0] != '\0'; p++){
55                 std::string end = *p;
56                 if(s.size() < end.size())
57                         continue;
58                 if(s.substr(s.size()-end.size(), end.size()) == end)
59                         return s.substr(0, s.size() - end.size());
60         }
61         return "";
62 }
63
64 // Tests if two strings are equal, optionally case insensitive
65 inline bool str_equal(const std::wstring& s1, const std::wstring& s2,
66                 bool case_insensitive = false)
67 {
68         if(case_insensitive)
69         {
70                 if(s1.size() != s2.size())
71                         return false;
72                 for(size_t i = 0; i < s1.size(); ++i)
73                         if(tolower(s1[i]) != tolower(s2[i]))
74                                 return false;
75                 return true;
76         }
77         else
78         {
79                 return s1 == s2;
80         }
81 }
82
83 // Tests if the second string is a prefix of the first, optionally case insensitive
84 inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix,
85                 bool case_insensitive = false)
86 {
87         if(str.size() < prefix.size())
88                 return false;
89         if(case_insensitive)
90         {
91                 for(size_t i = 0; i < prefix.size(); ++i)
92                         if(tolower(str[i]) != tolower(prefix[i]))
93                                 return false;
94         }
95         else
96         {
97                 for(size_t i = 0; i < prefix.size(); ++i)
98                         if(str[i] != prefix[i])
99                                 return false;
100         }
101         return true;
102 }
103
104 // Split a string using the given delimiter. Returns a vector containing
105 // the component parts.
106 inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
107 {
108         std::vector<std::wstring> parts;
109         std::wstringstream sstr(str);
110         std::wstring part;
111         while(std::getline(sstr, part, delimiter))
112                 parts.push_back(part);
113         return parts;
114 }
115
116 inline std::vector<std::string> str_split(const std::string &str, char delimiter) {
117
118         std::vector<std::string> parts;
119         std::stringstream sstr(str);
120         std::string part;
121         while(std::getline(sstr, part, delimiter))
122                 parts.push_back(part);
123         return parts;
124 }
125
126 inline std::string lowercase(const std::string &s)
127 {
128         std::string s2;
129         for(size_t i=0; i<s.size(); i++)
130         {
131                 char c = s[i];
132                 if(c >= 'A' && c <= 'Z')
133                         c -= 'A' - 'a';
134                 s2 += c;
135         }
136         return s2;
137 }
138
139 inline std::string trim(const std::string &s)
140 {
141         size_t front = 0;
142         while(s[front] == ' '    ||
143               s[front] == '\t'   ||
144               s[front] == '\r'   ||
145               s[front] == '\n'
146              )
147                 ++front;
148
149         size_t back = s.size();
150         while(back > front &&
151               (s[back-1] == ' '  ||
152                s[back-1] == '\t' ||
153                s[back-1] == '\r' ||
154                s[back-1] == '\n'
155               )
156              )
157                 --back;
158
159         return s.substr(front, back - front);
160 }
161
162 inline bool is_yes(const std::string &s)
163 {
164         std::string s2 = lowercase(trim(s));
165         if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
166                 return true;
167         return false;
168 }
169
170 inline s32 mystoi(const std::string &s, s32 min, s32 max)
171 {
172         s32 i = atoi(s.c_str());
173         if(i < min)
174                 i = min;
175         if(i > max)
176                 i = max;
177         return i;
178 }
179
180 inline s64 stoi64(const std::string &s) {
181         std::stringstream tmp(s);
182         s64 t;
183         tmp >> t;
184         return t;
185 }
186
187 // MSVC2010 includes it's own versions of these
188 //#if !defined(_MSC_VER) || _MSC_VER < 1600
189
190 inline s32 mystoi(const std::string &s)
191 {
192         return atoi(s.c_str());
193 }
194
195 inline s32 mystoi(const std::wstring &s)
196 {
197         return atoi(wide_to_narrow(s).c_str());
198 }
199
200 inline float mystof(const std::string &s)
201 {
202         // This crap causes a segfault in certain cases on MinGW
203         /*float f;
204         std::istringstream ss(s);
205         ss>>f;
206         return f;*/
207         // This works in that case
208         return atof(s.c_str());
209 }
210
211 //#endif
212
213 #define stoi mystoi
214 #define stof mystof
215
216 inline std::string itos(s32 i)
217 {
218         std::ostringstream o;
219         o<<i;
220         return o.str();
221 }
222
223 inline std::string i64tos(s64 i) {
224         std::ostringstream o;
225         o<<i;
226         return o.str();
227 }
228
229 inline std::string ftos(float f)
230 {
231         std::ostringstream o;
232         o<<f;
233         return o.str();
234 }
235
236 inline void str_replace(std::string & str, std::string const & pattern,
237                 std::string const & replacement)
238 {
239         std::string::size_type start = str.find(pattern, 0);
240         while(start != str.npos)
241         {
242                 str.replace(start, pattern.size(), replacement);
243                 start = str.find(pattern, start+replacement.size());
244         }
245 }
246
247 inline void str_replace_char(std::string & str, char from, char to)
248 {
249         for(unsigned int i=0; i<str.size(); i++)
250         {
251                 if(str[i] == from)
252                         str[i] = to;
253         }
254 }
255
256 /*
257         Checks if a string contains only supplied characters
258 */
259 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
260 {
261         for(u32 i=0; i<s.size(); i++)
262         {
263                 bool confirmed = false;
264                 for(u32 j=0; j<allowed_chars.size(); j++)
265                 {
266                         if(s[i] == allowed_chars[j])
267                         {
268                                 confirmed = true;
269                                 break;
270                         }
271                 }
272                 if(confirmed == false)
273                         return false;
274         }
275         return true;
276 }
277
278 /*
279         Checks if a string contains no blacklisted characters (opposite
280         function of string_allowed())
281 */
282 inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars)
283 {
284         for(unsigned int i = 0; i < s.length(); i++)
285         {
286                 bool invalid = false;
287                 for(unsigned int j = 0; j < blacklisted_chars.length(); j++)
288                 {
289                         if(s[i] == blacklisted_chars[j])
290                         {
291                                 invalid = true;
292                                 break;
293                         }
294                 }
295                 if(invalid)
296                         return false;
297         }
298         return true;
299 }
300
301 /*
302         Forcefully wraps string into rows using \n
303         (no word wrap, used for showing paths in gui)
304 */
305 inline std::string wrap_rows(const std::string &from, u32 rowlen)
306 {
307         std::string to;
308         for(u32 i=0; i<from.size(); i++)
309         {
310                 if(i != 0 && i%rowlen == 0)
311                         to += '\n';
312                 to += from[i];
313         }
314         return to;
315 }
316
317 /*
318         Removes all \\ from a string that had been escaped (FormSpec strings)
319 */
320 inline std::string unescape_string(std::string &s)
321 {
322         std::string res;
323         
324         for (size_t i = 0; i < s.length(); i++) {
325                 if (s[i] == '\\')
326                         i++;
327                 res += s[i];
328         }
329         
330         return res;
331 }
332
333 inline bool is_number(const std::string& tocheck)
334 {
335         std::string::const_iterator iter = tocheck.begin();
336
337         while (iter != tocheck.end() && std::isdigit(*iter)) {
338                 ++iter;
339         }
340
341         return ((!tocheck.empty()) && (iter == tocheck.end()));
342 }
343
344 std::string translatePassword(std::string playername, std::wstring password);
345 std::string urlencode(std::string str);
346 std::string urldecode(std::string str);
347 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
348 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
349 size_t mystrlcpy(char *dst, const char *src, size_t size);
350 char *mystrtok_r(char *s, const char *sep, char **lasts);
351 u64 read_seed(const char *str);
352 bool parseColorString(const std::string &value, video::SColor &color, bool quiet);
353
354 #endif
355