]> git.lizzy.rs Git - minetest.git/blob - src/util/string.h
Fix settings to honor numeric conversion errors
[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.h"
24 #include <stdlib.h>
25 #include <string>
26 #include <cstring>
27 #include <vector>
28 #include <sstream>
29 #include "exceptions.h"
30
31 struct FlagDesc {
32         const char *name;
33         u32 flag;
34 };
35
36 std::wstring narrow_to_wide(const std::string& mbs);
37 std::string wide_to_narrow(const std::wstring& wcs);
38
39 static inline std::string padStringRight(std::string s, size_t len)
40 {
41         if(len > s.size())
42                 s.insert(s.end(), len - s.size(), ' ');
43         return s;
44 }
45
46 // ends: NULL- or ""-terminated array of strings
47 // Returns "" if no end could be removed.
48 static inline std::string removeStringEnd(const std::string &s, const char *ends[])
49 {
50         const char **p = ends;
51         for(; (*p) && (*p)[0] != '\0'; p++){
52                 std::string end = *p;
53                 if(s.size() < end.size())
54                         continue;
55                 if(s.substr(s.size()-end.size(), end.size()) == end)
56                         return s.substr(0, s.size() - end.size());
57         }
58         return "";
59 }
60
61 // Tests if two strings are equal, optionally case insensitive
62 inline bool str_equal(const std::wstring& s1, const std::wstring& s2,
63                 bool case_insensitive = false)
64 {
65         if(case_insensitive)
66         {
67                 if(s1.size() != s2.size())
68                         return false;
69                 for(size_t i = 0; i < s1.size(); ++i)
70                         if(tolower(s1[i]) != tolower(s2[i]))
71                                 return false;
72                 return true;
73         }
74         else
75         {
76                 return s1 == s2;
77         }
78 }
79
80 // Tests if the second string is a prefix of the first, optionally case insensitive
81 inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix,
82                 bool case_insensitive = false)
83 {
84         if(str.size() < prefix.size())
85                 return false;
86         if(case_insensitive)
87         {
88                 for(size_t i = 0; i < prefix.size(); ++i)
89                         if(tolower(str[i]) != tolower(prefix[i]))
90                                 return false;
91         }
92         else
93         {
94                 for(size_t i = 0; i < prefix.size(); ++i)
95                         if(str[i] != prefix[i])
96                                 return false;
97         }
98         return true;
99 }
100
101 // Split a string using the given delimiter. Returns a vector containing
102 // the component parts.
103 inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
104 {
105         std::vector<std::wstring> parts;
106         std::wstringstream sstr(str);
107         std::wstring part;
108         while(std::getline(sstr, part, delimiter))
109                 parts.push_back(part);
110         return parts;
111 }
112
113 inline std::string lowercase(const std::string &s)
114 {
115         std::string s2;
116         for(size_t i=0; i<s.size(); i++)
117         {
118                 char c = s[i];
119                 if(c >= 'A' && c <= 'Z')
120                         c -= 'A' - 'a';
121                 s2 += c;
122         }
123         return s2;
124 }
125
126 inline std::string trim(const std::string &s)
127 {
128         size_t front = 0;
129         while(s[front] == ' '    ||
130               s[front] == '\t'   ||
131               s[front] == '\r'   ||
132               s[front] == '\n'
133              )
134                 ++front;
135
136         size_t back = s.size();
137         while(back > front &&
138               (s[back-1] == ' '  ||
139                s[back-1] == '\t' ||
140                s[back-1] == '\r' ||
141                s[back-1] == '\n'
142               )
143              )
144                 --back;
145
146         return s.substr(front, back - front);
147 }
148
149 inline s32 mystoi(const std::string &s)
150 {
151         char* endptr = NULL;
152         s32 retval = strtol(s.c_str(),&endptr,10);
153
154         if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str()))
155                 throw NumericException("string to convert");
156
157         return retval;
158 }
159
160 inline bool is_yes(const std::string &s)
161 {
162         std::string s2 = lowercase(trim(s));
163         try {
164                 if(s2 == "y" || s2 == "yes" || s2 == "true" || mystoi(s2) != 0)
165                         return true;
166         }
167         catch(NumericException&e) {}
168         return false;
169 }
170
171 inline s32 mystoi(const std::string &s, s32 min, s32 max)
172 {
173         s32 i = mystoi(s);
174         if(i < min)
175                 i = min;
176         if(i > max)
177                 i = max;
178         return i;
179 }
180
181 inline s64 stoi64(const std::string &s) {
182         std::stringstream tmp(s);
183         long long t;
184         tmp >> t;
185         return t;
186 }
187
188 // MSVC2010 includes it's own versions of these
189 //#if !defined(_MSC_VER) || _MSC_VER < 1600
190
191 inline s32 mystoi(const std::wstring &s)
192 {
193         return mystoi(wide_to_narrow(s).c_str());
194 }
195
196 inline float mystof(const std::string &s)
197 {
198         char* endptr = NULL;
199         float retval = strtof(s.c_str(),&endptr);
200
201         if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str()))
202                 throw NumericException("string to convert");
203
204         return retval;
205 }
206
207 //#endif
208
209 #define stoi mystoi
210 #define stof mystof
211
212 inline std::string itos(s32 i)
213 {
214         std::ostringstream o;
215         o<<i;
216         return o.str();
217 }
218
219 inline std::string i64tos(s64 i) {
220         std::ostringstream o;
221         o<<i;
222         return o.str();
223 }
224
225 inline std::string ftos(float f)
226 {
227         std::ostringstream o;
228         o<<f;
229         return o.str();
230 }
231
232 inline void str_replace(std::string & str, std::string const & pattern,
233                 std::string const & replacement)
234 {
235         std::string::size_type start = str.find(pattern, 0);
236         while(start != str.npos)
237         {
238                 str.replace(start, pattern.size(), replacement);
239                 start = str.find(pattern, start+replacement.size());
240         }
241 }
242
243 inline void str_replace_char(std::string & str, char from, char to)
244 {
245         for(unsigned int i=0; i<str.size(); i++)
246         {
247                 if(str[i] == from)
248                         str[i] = to;
249         }
250 }
251
252 /*
253         Checks if a string contains only supplied characters
254 */
255 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
256 {
257         for(u32 i=0; i<s.size(); i++)
258         {
259                 bool confirmed = false;
260                 for(u32 j=0; j<allowed_chars.size(); j++)
261                 {
262                         if(s[i] == allowed_chars[j])
263                         {
264                                 confirmed = true;
265                                 break;
266                         }
267                 }
268                 if(confirmed == false)
269                         return false;
270         }
271         return true;
272 }
273
274 /*
275         Checks if a string contains no blacklisted characters (opposite
276         function of string_allowed())
277 */
278 inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars)
279 {
280         for(unsigned int i = 0; i < s.length(); i++)
281         {
282                 bool invalid = false;
283                 for(unsigned int j = 0; j < blacklisted_chars.length(); j++)
284                 {
285                         if(s[i] == blacklisted_chars[j])
286                         {
287                                 invalid = true;
288                                 break;
289                         }
290                 }
291                 if(invalid)
292                         return false;
293         }
294         return true;
295 }
296
297 /*
298         Forcefully wraps string into rows using \n
299         (no word wrap, used for showing paths in gui)
300 */
301 inline std::string wrap_rows(const std::string &from, u32 rowlen)
302 {
303         std::string to;
304         for(u32 i=0; i<from.size(); i++)
305         {
306                 if(i != 0 && i%rowlen == 0)
307                         to += '\n';
308                 to += from[i];
309         }
310         return to;
311 }
312
313 /*
314         Removes all \\ from a string that had been escaped (FormSpec strings)
315 */
316 inline std::string unescape_string(std::string &s)
317 {
318         std::string res;
319         
320         for (size_t i = 0; i < s.length(); i++) {
321                 if (s[i] == '\\')
322                         i++;
323                 res += s[i];
324         }
325         
326         return res;
327 }
328
329 std::string translatePassword(std::string playername, std::wstring password);
330 std::string urlencode(std::string str);
331 std::string urldecode(std::string str);
332 u32 readFlagString(std::string str, FlagDesc *flagdesc);
333 std::string writeFlagString(u32 flags, FlagDesc *flagdesc);
334 char *mystrtok_r(char *s, const char *sep, char **lasts);
335 u64 read_seed(const char *str);
336
337 #endif
338