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"
32 #include <unordered_map>
34 #define STRINGIFY(x) #x
35 #define TOSTRING(x) STRINGIFY(x)
37 // Checks whether a value is an ASCII printable character
38 #define IS_ASCII_PRINTABLE_CHAR(x) \
39 (((unsigned int)(x) >= 0x20) && \
40 ( (unsigned int)(x) <= 0x7e))
42 // Checks whether a byte is an inner byte for an utf-8 multibyte sequence
43 #define IS_UTF8_MULTB_INNER(x) \
44 (((unsigned char)(x) >= 0x80) && \
45 ( (unsigned char)(x) <= 0xbf))
47 // Checks whether a byte is a start byte for an utf-8 multibyte sequence
48 #define IS_UTF8_MULTB_START(x) \
49 (((unsigned char)(x) >= 0xc2) && \
50 ( (unsigned char)(x) <= 0xf4))
52 // Given a start byte x for an utf-8 multibyte sequence
53 // it gives the length of the whole sequence in bytes.
54 #define UTF8_MULTB_START_LEN(x) \
55 (((unsigned char)(x) < 0xe0) ? 2 : \
56 (((unsigned char)(x) < 0xf0) ? 3 : 4))
58 typedef std::unordered_map<std::string, std::string> StringMap;
65 // try not to convert between wide/utf8 encodings; this can result in data loss
66 // try to only convert between them when you need to input/output stuff via Irrlicht
67 std::wstring utf8_to_wide(const std::string &input);
68 std::string wide_to_utf8(const std::wstring &input);
70 wchar_t *utf8_to_wide_c(const char *str);
72 // NEVER use those two functions unless you have a VERY GOOD reason to
73 // they just convert between wide and multibyte encoding
74 // multibyte encoding depends on current locale, this is no good, especially on Windows
76 // You must free the returned string!
77 // The returned string is allocated using new
78 wchar_t *narrow_to_wide_c(const char *str);
79 std::wstring narrow_to_wide(const std::string &mbs);
80 std::string wide_to_narrow(const std::wstring &wcs);
82 std::string urlencode(const std::string &str);
83 std::string urldecode(const std::string &str);
84 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
85 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
86 size_t mystrlcpy(char *dst, const char *src, size_t size);
87 char *mystrtok_r(char *s, const char *sep, char **lasts);
88 u64 read_seed(const char *str);
89 bool parseColorString(const std::string &value, video::SColor &color, bool quiet);
93 * Returns a copy of \p str with spaces inserted at the right hand side to ensure
94 * that the string is \p len characters in length. If \p str is <= \p len then the
95 * returned string will be identical to str.
97 inline std::string padStringRight(std::string str, size_t len)
100 str.insert(str.end(), len - str.size(), ' ');
106 * Returns a version of \p str with the first occurrence of a string
107 * contained within ends[] removed from the end of the string.
110 * @param ends A NULL- or ""- terminated array of strings to remove from s in
111 * the copy produced. Note that once one of these strings is removed
112 * that no further postfixes contained within this array are removed.
114 * @return If no end could be removed then "" is returned.
116 inline std::string removeStringEnd(const std::string &str,
119 const char **p = ends;
121 for (; *p && (*p)[0] != '\0'; p++) {
122 std::string end = *p;
123 if (str.size() < end.size())
125 if (str.compare(str.size() - end.size(), end.size(), end) == 0)
126 return str.substr(0, str.size() - end.size());
134 * Check two strings for equivalence. If \p case_insensitive is true
135 * then the case of the strings is ignored (default is false).
139 * @param case_insensitive
140 * @return true if the strings match
142 template <typename T>
143 inline bool str_equal(const std::basic_string<T> &s1,
144 const std::basic_string<T> &s2,
145 bool case_insensitive = false)
147 if (!case_insensitive)
150 if (s1.size() != s2.size())
153 for (size_t i = 0; i < s1.size(); ++i)
154 if(tolower(s1[i]) != tolower(s2[i]))
162 * Check whether \p str begins with the string prefix. If \p case_insensitive
163 * is true then the check is case insensitve (default is false; i.e. case is
168 * @param case_insensitive
169 * @return true if the str begins with prefix
171 template <typename T>
172 inline bool str_starts_with(const std::basic_string<T> &str,
173 const std::basic_string<T> &prefix,
174 bool case_insensitive = false)
176 if (str.size() < prefix.size())
179 if (!case_insensitive)
180 return str.compare(0, prefix.size(), prefix) == 0;
182 for (size_t i = 0; i < prefix.size(); ++i)
183 if (tolower(str[i]) != tolower(prefix[i]))
189 * Check whether \p str begins with the string prefix. If \p case_insensitive
190 * is true then the check is case insensitve (default is false; i.e. case is
195 * @param case_insensitive
196 * @return true if the str begins with prefix
198 template <typename T>
199 inline bool str_starts_with(const std::basic_string<T> &str,
201 bool case_insensitive = false)
203 return str_starts_with(str, std::basic_string<T>(prefix),
208 * Splits a string into its component parts separated by the character
211 * @return An std::vector<std::basic_string<T> > of the component parts
213 template <typename T>
214 inline std::vector<std::basic_string<T> > str_split(
215 const std::basic_string<T> &str,
218 std::vector<std::basic_string<T> > parts;
219 std::basic_stringstream<T> sstr(str);
220 std::basic_string<T> part;
222 while (std::getline(sstr, part, delimiter))
223 parts.push_back(part);
231 * @return A copy of \p str converted to all lowercase characters.
233 inline std::string lowercase(const std::string &str)
237 s2.reserve(str.size());
239 for (size_t i = 0; i < str.size(); i++)
240 s2 += tolower(str[i]);
248 * @return A copy of \p str with leading and trailing whitespace removed.
250 inline std::string trim(const std::string &str)
254 while (std::isspace(str[front]))
257 size_t back = str.size();
258 while (back > front && std::isspace(str[back - 1]))
261 return str.substr(front, back - front);
266 * Returns whether \p str should be regarded as (bool) true. Case and leading
267 * and trailing whitespace are ignored. Values that will return
268 * true are "y", "yes", "true" and any number that is not 0.
271 inline bool is_yes(const std::string &str)
273 std::string s2 = lowercase(trim(str));
275 return s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0;
280 * Converts the string \p str to a signed 32-bit integer. The converted value
281 * is constrained so that min <= value <= max.
283 * @see atoi(3) for limitations
286 * @param min Range minimum
287 * @param max Range maximum
288 * @return The value converted to a signed 32-bit integer and constrained
289 * within the range defined by min and max (inclusive)
291 inline s32 mystoi(const std::string &str, s32 min, s32 max)
293 s32 i = atoi(str.c_str());
304 // MSVC2010 includes it's own versions of these
305 //#if !defined(_MSC_VER) || _MSC_VER < 1600
309 * Returns a 32-bit value reprensented by the string \p str (decimal).
310 * @see atoi(3) for further limitations
312 inline s32 mystoi(const std::string &str)
314 return atoi(str.c_str());
319 * Returns s 32-bit value represented by the wide string \p str (decimal).
320 * @see atoi(3) for further limitations
322 inline s32 mystoi(const std::wstring &str)
324 return mystoi(wide_to_narrow(str));
329 * Returns a float reprensented by the string \p str (decimal).
332 inline float mystof(const std::string &str)
334 return atof(str.c_str());
342 /// Returns a value represented by the string \p val.
343 template <typename T>
344 inline T from_string(const std::string &str)
346 std::stringstream tmp(str);
352 /// Returns a 64-bit signed value represented by the string \p str (decimal).
353 inline s64 stoi64(const std::string &str) { return from_string<s64>(str); }
355 #if __cplusplus < 201103L
358 /// Returns a string representing the value \p val.
359 template <typename T>
360 inline string to_string(T val)
366 #define DEFINE_STD_TOSTRING_FLOATINGPOINT(T) \
368 inline string to_string<T>(T val) \
372 << std::setprecision(6) \
376 DEFINE_STD_TOSTRING_FLOATINGPOINT(float)
377 DEFINE_STD_TOSTRING_FLOATINGPOINT(double)
378 DEFINE_STD_TOSTRING_FLOATINGPOINT(long double)
380 #undef DEFINE_STD_TOSTRING_FLOATINGPOINT
382 /// Returns a wide string representing the value \p val
383 template <typename T>
384 inline wstring to_wstring(T val)
386 return utf8_to_wide(to_string(val));
391 /// Returns a string representing the decimal value of the 32-bit value \p i.
392 inline std::string itos(s32 i) { return std::to_string(i); }
393 /// Returns a string representing the decimal value of the 64-bit value \p i.
394 inline std::string i64tos(s64 i) { return std::to_string(i); }
396 // std::to_string uses the '%.6f' conversion, which is inconsistent with
397 // std::ostream::operator<<() and impractical too. ftos() uses the
398 // more generic and std::ostream::operator<<()-compatible '%G' format.
399 /// Returns a string representing the decimal value of the float value \p f.
400 inline std::string ftos(float f)
402 std::ostringstream oss;
409 * Replace all occurrences of \p pattern in \p str with \p replacement.
411 * @param str String to replace pattern with replacement within.
412 * @param pattern The pattern to replace.
413 * @param replacement What to replace the pattern with.
415 inline void str_replace(std::string &str, const std::string &pattern,
416 const std::string &replacement)
418 std::string::size_type start = str.find(pattern, 0);
419 while (start != str.npos) {
420 str.replace(start, pattern.size(), replacement);
421 start = str.find(pattern, start + replacement.size());
426 * Escapes characters [ ] \ , ; that can not be used in formspecs
428 inline void str_formspec_escape(std::string &str)
430 str_replace(str, "\\", "\\\\");
431 str_replace(str, "]", "\\]");
432 str_replace(str, "[", "\\[");
433 str_replace(str, ";", "\\;");
434 str_replace(str, ",", "\\,");
438 * Replace all occurrences of the character \p from in \p str with \p to.
440 * @param str The string to (potentially) modify.
441 * @param from The character in str to replace.
442 * @param to The replacement character.
444 void str_replace(std::string &str, char from, char to);
448 * Check that a string only contains whitelisted characters. This is the
449 * opposite of string_allowed_blacklist().
451 * @param str The string to be checked.
452 * @param allowed_chars A string containing permitted characters.
453 * @return true if the string is allowed, otherwise false.
455 * @see string_allowed_blacklist()
457 inline bool string_allowed(const std::string &str, const std::string &allowed_chars)
459 return str.find_first_not_of(allowed_chars) == str.npos;
464 * Check that a string contains no blacklisted characters. This is the
465 * opposite of string_allowed().
467 * @param str The string to be checked.
468 * @param blacklisted_chars A string containing prohibited characters.
469 * @return true if the string is allowed, otherwise false.
471 * @see string_allowed()
473 inline bool string_allowed_blacklist(const std::string &str,
474 const std::string &blacklisted_chars)
476 return str.find_first_of(blacklisted_chars) == str.npos;
481 * Create a string based on \p from where a newline is forcefully inserted
482 * every \p row_len characters.
484 * @note This function does not honour word wraps and blindy inserts a newline
485 * every \p row_len characters whether it breaks a word or not. It is
486 * intended to be used for, for example, showing paths in the GUI.
488 * @note This function doesn't wrap inside utf-8 multibyte sequences and also
489 * counts multibyte sequences correcly as single characters.
491 * @param from The (utf-8) string to be wrapped into rows.
492 * @param row_len The row length (in characters).
493 * @return A new string with the wrapping applied.
495 inline std::string wrap_rows(const std::string &from,
500 size_t character_idx = 0;
501 for (size_t i = 0; i < from.size(); i++) {
502 if (!IS_UTF8_MULTB_INNER(from[i])) {
503 // Wrap string after last inner byte of char
504 if (character_idx > 0 && character_idx % row_len == 0)
516 * Removes backslashes from an escaped string (FormSpec strings)
518 template <typename T>
519 inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
521 std::basic_string<T> res;
523 for (size_t i = 0; i < s.length(); i++) {
536 * Remove all escape sequences in \p s.
538 * @param s The string in which to remove escape sequences.
539 * @return \p s, with escape sequences removed.
541 template <typename T>
542 std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
544 std::basic_string<T> output;
546 while (i < s.length()) {
547 if (s[i] == '\x1b') {
549 if (i == s.length()) continue;
552 while (i < s.length() && s[i] != ')') {
570 template <typename T>
571 std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
573 std::vector<std::basic_string<T> > tokens;
575 std::basic_string<T> current;
576 bool last_was_escape = false;
577 for (size_t i = 0; i < s.length(); i++) {
579 if (last_was_escape) {
582 last_was_escape = false;
585 tokens.push_back(current);
586 current = std::basic_string<T>();
587 last_was_escape = false;
588 } else if (si == '\\') {
589 last_was_escape = true;
592 last_was_escape = false;
597 tokens.push_back(current);
603 * Checks that all characters in \p to_check are a decimal digits.
606 * @return true if to_check is not empty and all characters in to_check are
607 * decimal digits, otherwise false
609 inline bool is_number(const std::string &to_check)
611 for (size_t i = 0; i < to_check.size(); i++)
612 if (!std::isdigit(to_check[i]))
615 return !to_check.empty();
620 * Returns a C-string, either "true" or "false", corresponding to \p val.
622 * @return If \p val is true, then "true" is returned, otherwise "false".
624 inline const char *bool_to_cstr(bool val)
626 return val ? "true" : "false";
629 inline const std::string duration_to_string(int sec)
636 std::stringstream ss;