3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef UTIL_STRING_HEADER
21 #define UTIL_STRING_HEADER
23 #include "irrlichttypes_bloated.h"
31 #define STRINGIFY(x) #x
32 #define TOSTRING(x) STRINGIFY(x)
40 // You must free the returned string!
41 // The returned string is allocated using new
42 wchar_t *narrow_to_wide_c(const char *str);
44 std::wstring narrow_to_wide(const std::string &mbs);
45 std::string wide_to_narrow(const std::wstring &wcs);
47 std::string urlencode(std::string str);
48 std::string urldecode(std::string str);
49 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
50 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
51 size_t mystrlcpy(char *dst, const char *src, size_t size);
52 char *mystrtok_r(char *s, const char *sep, char **lasts);
53 u64 read_seed(const char *str);
54 bool parseColorString(const std::string &value, video::SColor &color, bool quiet);
58 * Returns a copy of \p str with spaces inserted at the right hand side to ensure
59 * that the string is \p len characters in length. If \p str is <= \p len then the
60 * returned string will be identical to str.
62 inline std::string padStringRight(std::string str, size_t len)
65 str.insert(str.end(), len - str.size(), ' ');
71 * Returns a version of \p str with the first occurrence of a string
72 * contained within ends[] removed from the end of the string.
75 * @param ends A NULL- or ""- terminated array of strings to remove from s in
76 * the copy produced. Note that once one of these strings is removed
77 * that no further postfixes contained within this array are removed.
79 * @return If no end could be removed then "" is returned.
81 inline std::string removeStringEnd(const std::string &str,
84 const char **p = ends;
86 for (; *p && (*p)[0] != '\0'; p++) {
88 if (str.size() < end.size())
90 if (str.compare(str.size() - end.size(), end.size(), end) == 0)
91 return str.substr(0, str.size() - end.size());
99 * Check two strings for equivalence. If \p case_insensitive is true
100 * then the case of the strings is ignored (default is false).
104 * @param case_insensitive
105 * @return true if the strings match
107 template <typename T>
108 inline bool str_equal(const std::basic_string<T> &s1,
109 const std::basic_string<T> &s2,
110 bool case_insensitive = false)
112 if (!case_insensitive)
115 if (s1.size() != s2.size())
118 for (size_t i = 0; i < s1.size(); ++i)
119 if(tolower(s1[i]) != tolower(s2[i]))
127 * Check whether \p str begins with the string prefix. If \p case_insensitive
128 * is true then the check is case insensitve (default is false; i.e. case is
133 * @param case_insensitive
134 * @return true if the str begins with prefix
136 template <typename T>
137 inline bool str_starts_with(const std::basic_string<T> &str,
138 const std::basic_string<T> &prefix,
139 bool case_insensitive = false)
141 if (str.size() < prefix.size())
144 if (!case_insensitive)
145 return str.compare(0, prefix.size(), prefix) == 0;
147 for (size_t i = 0; i < prefix.size(); ++i)
148 if (tolower(str[i]) != tolower(prefix[i]))
154 * Check whether \p str begins with the string prefix. If \p case_insensitive
155 * is true then the check is case insensitve (default is false; i.e. case is
160 * @param case_insensitive
161 * @return true if the str begins with prefix
163 template <typename T>
164 inline bool str_starts_with(const std::basic_string<T> &str,
166 bool case_insensitive = false)
168 return str_starts_with(str, std::basic_string<T>(prefix),
173 * Splits a string into its component parts separated by the character
176 * @return An std::vector<std::basic_string<T> > of the component parts
178 template <typename T>
179 inline std::vector<std::basic_string<T> > str_split(
180 const std::basic_string<T> &str,
183 std::vector<std::basic_string<T> > parts;
184 std::basic_stringstream<T> sstr(str);
185 std::basic_string<T> part;
187 while (std::getline(sstr, part, delimiter))
188 parts.push_back(part);
196 * @return A copy of \p str converted to all lowercase characters.
198 inline std::string lowercase(const std::string &str)
202 s2.reserve(str.size());
204 for (size_t i = 0; i < str.size(); i++)
205 s2 += tolower(str[i]);
213 * @return A copy of \p str with leading and trailing whitespace removed.
215 inline std::string trim(const std::string &str)
219 while (std::isspace(str[front]))
222 size_t back = str.size();
223 while (back > front && std::isspace(str[back - 1]))
226 return str.substr(front, back - front);
231 * Returns whether \p str should be regarded as (bool) true. Case and leading
232 * and trailing whitespace are ignored. Values that will return
233 * true are "y", "yes", "true" and any number that is not 0.
236 inline bool is_yes(const std::string &str)
238 std::string s2 = lowercase(trim(str));
240 return s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0;
245 * Converts the string \p str to a signed 32-bit integer. The converted value
246 * is constrained so that min <= value <= max.
248 * @see atoi(3) for limitations
251 * @param min Range minimum
252 * @param max Range maximum
253 * @return The value converted to a signed 32-bit integer and constrained
254 * within the range defined by min and max (inclusive)
256 inline s32 mystoi(const std::string &str, s32 min, s32 max)
258 s32 i = atoi(str.c_str());
269 /// Returns a 64-bit value represented by the string \p str (decimal).
270 inline s64 stoi64(const std::string &str)
272 std::stringstream tmp(str);
278 // MSVC2010 includes it's own versions of these
279 //#if !defined(_MSC_VER) || _MSC_VER < 1600
283 * Returns a 32-bit value reprensented by the string \p str (decimal).
284 * @see atoi(3) for further limitations
286 inline s32 mystoi(const std::string &str)
288 return atoi(str.c_str());
293 * Returns s 32-bit value represented by the wide string \p str (decimal).
294 * @see atoi(3) for further limitations
296 inline s32 mystoi(const std::wstring &str)
298 return mystoi(wide_to_narrow(str));
303 * Returns a float reprensented by the string \p str (decimal).
306 inline float mystof(const std::string &str)
308 return atof(str.c_str());
316 // TODO: Replace with C++11 std::to_string.
318 /// Returns A string representing the value \p val.
319 template <typename T>
320 inline std::string to_string(T val)
322 std::ostringstream oss;
327 /// Returns a string representing the decimal value of the 32-bit value \p i.
328 inline std::string itos(s32 i) { return to_string(i); }
329 /// Returns a string representing the decimal value of the 64-bit value \p i.
330 inline std::string i64tos(s64 i) { return to_string(i); }
331 /// Returns a string representing the decimal value of the float value \p f.
332 inline std::string ftos(float f) { return to_string(f); }
336 * Replace all occurrences of \p pattern in \p str with \p replacement.
338 * @param str String to replace pattern with replacement within.
339 * @param pattern The pattern to replace.
340 * @param replacement What to replace the pattern with.
342 inline void str_replace(std::string &str, const std::string &pattern,
343 const std::string &replacement)
345 std::string::size_type start = str.find(pattern, 0);
346 while (start != str.npos) {
347 str.replace(start, pattern.size(), replacement);
348 start = str.find(pattern, start + replacement.size());
354 * Replace all occurrences of the character \p from in \p str with \p to.
356 * @param str The string to (potentially) modify.
357 * @param from The character in str to replace.
358 * @param to The replacement character.
360 void str_replace(std::string &str, char from, char to);
364 * Check that a string only contains whitelisted characters. This is the
365 * opposite of string_allowed_blacklist().
367 * @param str The string to be checked.
368 * @param allowed_chars A string containing permitted characters.
369 * @return true if the string is allowed, otherwise false.
371 * @see string_allowed_blacklist()
373 inline bool string_allowed(const std::string &str, const std::string &allowed_chars)
375 return str.find_first_not_of(allowed_chars) == str.npos;
380 * Check that a string contains no blacklisted characters. This is the
381 * opposite of string_allowed().
383 * @param str The string to be checked.
384 * @param blacklisted_chars A string containing prohibited characters.
385 * @return true if the string is allowed, otherwise false.
387 * @see string_allowed()
389 inline bool string_allowed_blacklist(const std::string &str,
390 const std::string &blacklisted_chars)
392 return str.find_first_of(blacklisted_chars) == str.npos;
397 * Create a string based on \p from where a newline is forcefully inserted
398 * every \p row_len characters.
400 * @note This function does not honour word wraps and blindy inserts a newline
401 * every \p row_len characters whether it breaks a word or not. It is
402 * intended to be used for, for example, showing paths in the GUI.
404 * @param from The string to be wrapped into rows.
405 * @param row_len The row length (in characters).
406 * @return A new string with the wrapping applied.
408 inline std::string wrap_rows(const std::string &from,
413 for (size_t i = 0; i < from.size(); i++) {
414 if (i != 0 && i % row_len == 0)
424 * Removes backslashes from an escaped string (FormSpec strings)
426 template <typename T>
427 inline std::basic_string<T> unescape_string(std::basic_string<T> &s)
429 std::basic_string<T> res;
431 for (size_t i = 0; i < s.length(); i++) {
445 * Checks that all characters in \p to_check are a decimal digits.
448 * @return true if to_check is not empty and all characters in to_check are
449 * decimal digits, otherwise false
451 inline bool is_number(const std::string &to_check)
453 for (size_t i = 0; i < to_check.size(); i++)
454 if (!std::isdigit(to_check[i]))
457 return !to_check.empty();
462 * Returns a C-string, either "true" or "false", corresponding to \p val.
464 * @return If \p val is true, then "true" is returned, otherwise "false".
466 inline const char *bool_to_cstr(bool val)
468 return val ? "true" : "false";