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.
22 #include "irrlichttypes_bloated.h"
23 #include "irrString.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,
90 unsigned char default_alpha = 0xff);
94 * Returns a copy of \p str with spaces inserted at the right hand side to ensure
95 * that the string is \p len characters in length. If \p str is <= \p len then the
96 * returned string will be identical to str.
98 inline std::string padStringRight(std::string str, size_t len)
100 if (len > str.size())
101 str.insert(str.end(), len - str.size(), ' ');
107 * Returns a version of \p str with the first occurrence of a string
108 * contained within ends[] removed from the end of the string.
111 * @param ends A NULL- or ""- terminated array of strings to remove from s in
112 * the copy produced. Note that once one of these strings is removed
113 * that no further postfixes contained within this array are removed.
115 * @return If no end could be removed then "" is returned.
117 inline std::string removeStringEnd(const std::string &str,
120 const char **p = ends;
122 for (; *p && (*p)[0] != '\0'; p++) {
123 std::string end = *p;
124 if (str.size() < end.size())
126 if (str.compare(str.size() - end.size(), end.size(), end) == 0)
127 return str.substr(0, str.size() - end.size());
135 * Check two strings for equivalence. If \p case_insensitive is true
136 * then the case of the strings is ignored (default is false).
140 * @param case_insensitive
141 * @return true if the strings match
143 template <typename T>
144 inline bool str_equal(const std::basic_string<T> &s1,
145 const std::basic_string<T> &s2,
146 bool case_insensitive = false)
148 if (!case_insensitive)
151 if (s1.size() != s2.size())
154 for (size_t i = 0; i < s1.size(); ++i)
155 if(tolower(s1[i]) != tolower(s2[i]))
163 * Check whether \p str begins with the string prefix. If \p case_insensitive
164 * is true then the check is case insensitve (default is false; i.e. case is
169 * @param case_insensitive
170 * @return true if the str begins with prefix
172 template <typename T>
173 inline bool str_starts_with(const std::basic_string<T> &str,
174 const std::basic_string<T> &prefix,
175 bool case_insensitive = false)
177 if (str.size() < prefix.size())
180 if (!case_insensitive)
181 return str.compare(0, prefix.size(), prefix) == 0;
183 for (size_t i = 0; i < prefix.size(); ++i)
184 if (tolower(str[i]) != tolower(prefix[i]))
190 * Check whether \p str begins with the string prefix. If \p case_insensitive
191 * is true then the check is case insensitve (default is false; i.e. case is
196 * @param case_insensitive
197 * @return true if the str begins with prefix
199 template <typename T>
200 inline bool str_starts_with(const std::basic_string<T> &str,
202 bool case_insensitive = false)
204 return str_starts_with(str, std::basic_string<T>(prefix),
210 * Check whether \p str ends with the string suffix. If \p case_insensitive
211 * is true then the check is case insensitve (default is false; i.e. case is
216 * @param case_insensitive
217 * @return true if the str begins with suffix
219 template <typename T>
220 inline bool str_ends_with(const std::basic_string<T> &str,
221 const std::basic_string<T> &suffix,
222 bool case_insensitive = false)
224 if (str.size() < suffix.size())
227 size_t start = str.size() - suffix.size();
228 if (!case_insensitive)
229 return str.compare(start, suffix.size(), suffix) == 0;
231 for (size_t i = 0; i < suffix.size(); ++i)
232 if (tolower(str[start + i]) != tolower(suffix[i]))
239 * Check whether \p str ends with the string suffix. If \p case_insensitive
240 * is true then the check is case insensitve (default is false; i.e. case is
245 * @param case_insensitive
246 * @return true if the str begins with suffix
248 template <typename T>
249 inline bool str_ends_with(const std::basic_string<T> &str,
251 bool case_insensitive = false)
253 return str_ends_with(str, std::basic_string<T>(suffix),
259 * Splits a string into its component parts separated by the character
262 * @return An std::vector<std::basic_string<T> > of the component parts
264 template <typename T>
265 inline std::vector<std::basic_string<T> > str_split(
266 const std::basic_string<T> &str,
269 std::vector<std::basic_string<T> > parts;
270 std::basic_stringstream<T> sstr(str);
271 std::basic_string<T> part;
273 while (std::getline(sstr, part, delimiter))
274 parts.push_back(part);
282 * @return A copy of \p str converted to all lowercase characters.
284 inline std::string lowercase(const std::string &str)
288 s2.reserve(str.size());
299 * @return A copy of \p str with leading and trailing whitespace removed.
301 inline std::string trim(const std::string &str)
305 while (std::isspace(str[front]))
308 size_t back = str.size();
309 while (back > front && std::isspace(str[back - 1]))
312 return str.substr(front, back - front);
317 * Returns whether \p str should be regarded as (bool) true. Case and leading
318 * and trailing whitespace are ignored. Values that will return
319 * true are "y", "yes", "true" and any number that is not 0.
322 inline bool is_yes(const std::string &str)
324 std::string s2 = lowercase(trim(str));
326 return s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0;
331 * Converts the string \p str to a signed 32-bit integer. The converted value
332 * is constrained so that min <= value <= max.
334 * @see atoi(3) for limitations
337 * @param min Range minimum
338 * @param max Range maximum
339 * @return The value converted to a signed 32-bit integer and constrained
340 * within the range defined by min and max (inclusive)
342 inline s32 mystoi(const std::string &str, s32 min, s32 max)
344 s32 i = atoi(str.c_str());
355 // MSVC2010 includes it's own versions of these
356 //#if !defined(_MSC_VER) || _MSC_VER < 1600
360 * Returns a 32-bit value reprensented by the string \p str (decimal).
361 * @see atoi(3) for further limitations
363 inline s32 mystoi(const std::string &str)
365 return atoi(str.c_str());
370 * Returns s 32-bit value represented by the wide string \p str (decimal).
371 * @see atoi(3) for further limitations
373 inline s32 mystoi(const std::wstring &str)
375 return mystoi(wide_to_narrow(str));
380 * Returns a float reprensented by the string \p str (decimal).
383 inline float mystof(const std::string &str)
385 return atof(str.c_str());
393 /// Returns a value represented by the string \p val.
394 template <typename T>
395 inline T from_string(const std::string &str)
397 std::stringstream tmp(str);
403 /// Returns a 64-bit signed value represented by the string \p str (decimal).
404 inline s64 stoi64(const std::string &str) { return from_string<s64>(str); }
406 #if __cplusplus < 201103L
409 /// Returns a string representing the value \p val.
410 template <typename T>
411 inline string to_string(T val)
417 #define DEFINE_STD_TOSTRING_FLOATINGPOINT(T) \
419 inline string to_string<T>(T val) \
423 << std::setprecision(6) \
427 DEFINE_STD_TOSTRING_FLOATINGPOINT(float)
428 DEFINE_STD_TOSTRING_FLOATINGPOINT(double)
429 DEFINE_STD_TOSTRING_FLOATINGPOINT(long double)
431 #undef DEFINE_STD_TOSTRING_FLOATINGPOINT
433 /// Returns a wide string representing the value \p val
434 template <typename T>
435 inline wstring to_wstring(T val)
437 return utf8_to_wide(to_string(val));
442 /// Returns a string representing the decimal value of the 32-bit value \p i.
443 inline std::string itos(s32 i) { return std::to_string(i); }
444 /// Returns a string representing the decimal value of the 64-bit value \p i.
445 inline std::string i64tos(s64 i) { return std::to_string(i); }
447 // std::to_string uses the '%.6f' conversion, which is inconsistent with
448 // std::ostream::operator<<() and impractical too. ftos() uses the
449 // more generic and std::ostream::operator<<()-compatible '%G' format.
450 /// Returns a string representing the decimal value of the float value \p f.
451 inline std::string ftos(float f)
453 std::ostringstream oss;
460 * Replace all occurrences of \p pattern in \p str with \p replacement.
462 * @param str String to replace pattern with replacement within.
463 * @param pattern The pattern to replace.
464 * @param replacement What to replace the pattern with.
466 inline void str_replace(std::string &str, const std::string &pattern,
467 const std::string &replacement)
469 std::string::size_type start = str.find(pattern, 0);
470 while (start != str.npos) {
471 str.replace(start, pattern.size(), replacement);
472 start = str.find(pattern, start + replacement.size());
477 * Escapes characters [ ] \ , ; that can not be used in formspecs
479 inline void str_formspec_escape(std::string &str)
481 str_replace(str, "\\", "\\\\");
482 str_replace(str, "]", "\\]");
483 str_replace(str, "[", "\\[");
484 str_replace(str, ";", "\\;");
485 str_replace(str, ",", "\\,");
489 * Replace all occurrences of the character \p from in \p str with \p to.
491 * @param str The string to (potentially) modify.
492 * @param from The character in str to replace.
493 * @param to The replacement character.
495 void str_replace(std::string &str, char from, char to);
499 * Check that a string only contains whitelisted characters. This is the
500 * opposite of string_allowed_blacklist().
502 * @param str The string to be checked.
503 * @param allowed_chars A string containing permitted characters.
504 * @return true if the string is allowed, otherwise false.
506 * @see string_allowed_blacklist()
508 inline bool string_allowed(const std::string &str, const std::string &allowed_chars)
510 return str.find_first_not_of(allowed_chars) == str.npos;
515 * Check that a string contains no blacklisted characters. This is the
516 * opposite of string_allowed().
518 * @param str The string to be checked.
519 * @param blacklisted_chars A string containing prohibited characters.
520 * @return true if the string is allowed, otherwise false.
522 * @see string_allowed()
524 inline bool string_allowed_blacklist(const std::string &str,
525 const std::string &blacklisted_chars)
527 return str.find_first_of(blacklisted_chars) == str.npos;
532 * Create a string based on \p from where a newline is forcefully inserted
533 * every \p row_len characters.
535 * @note This function does not honour word wraps and blindy inserts a newline
536 * every \p row_len characters whether it breaks a word or not. It is
537 * intended to be used for, for example, showing paths in the GUI.
539 * @note This function doesn't wrap inside utf-8 multibyte sequences and also
540 * counts multibyte sequences correcly as single characters.
542 * @param from The (utf-8) string to be wrapped into rows.
543 * @param row_len The row length (in characters).
544 * @return A new string with the wrapping applied.
546 inline std::string wrap_rows(const std::string &from,
551 size_t character_idx = 0;
552 for (size_t i = 0; i < from.size(); i++) {
553 if (!IS_UTF8_MULTB_INNER(from[i])) {
554 // Wrap string after last inner byte of char
555 if (character_idx > 0 && character_idx % row_len == 0)
567 * Removes backslashes from an escaped string (FormSpec strings)
569 template <typename T>
570 inline std::basic_string<T> unescape_string(const std::basic_string<T> &s)
572 std::basic_string<T> res;
574 for (size_t i = 0; i < s.length(); i++) {
587 * Remove all escape sequences in \p s.
589 * @param s The string in which to remove escape sequences.
590 * @return \p s, with escape sequences removed.
592 template <typename T>
593 std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
595 std::basic_string<T> output;
597 while (i < s.length()) {
598 if (s[i] == '\x1b') {
600 if (i == s.length()) continue;
603 while (i < s.length() && s[i] != ')') {
621 template <typename T>
622 std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
624 std::vector<std::basic_string<T> > tokens;
626 std::basic_string<T> current;
627 bool last_was_escape = false;
628 for (size_t i = 0; i < s.length(); i++) {
630 if (last_was_escape) {
633 last_was_escape = false;
636 tokens.push_back(current);
637 current = std::basic_string<T>();
638 last_was_escape = false;
639 } else if (si == '\\') {
640 last_was_escape = true;
643 last_was_escape = false;
648 tokens.push_back(current);
653 std::wstring translate_string(const std::wstring &s);
655 inline std::wstring unescape_translate(const std::wstring &s) {
656 return unescape_enriched(translate_string(s));
660 * Checks that all characters in \p to_check are a decimal digits.
663 * @return true if to_check is not empty and all characters in to_check are
664 * decimal digits, otherwise false
666 inline bool is_number(const std::string &to_check)
668 for (char i : to_check)
669 if (!std::isdigit(i))
672 return !to_check.empty();
677 * Returns a C-string, either "true" or "false", corresponding to \p val.
679 * @return If \p val is true, then "true" is returned, otherwise "false".
681 inline const char *bool_to_cstr(bool val)
683 return val ? "true" : "false";
686 inline const std::string duration_to_string(int sec)
693 std::stringstream ss;
710 * Joins a vector of strings by the string \p delimiter.
712 * @return A std::string
714 inline std::string str_join(const std::vector<std::string> &list,
715 const std::string &delimiter)
717 std::ostringstream oss;
719 for (const auto &part : list) {
729 * Create a UTF8 std::string from a irr::core::stringw.
731 inline std::string stringw_to_utf8(const irr::core::stringw &input)
733 std::wstring str(input.c_str());
734 return wide_to_utf8(str);
738 * Create a irr::core:stringw from a UTF8 std::string.
740 inline irr::core::stringw utf8_to_stringw(const std::string &input)
742 std::wstring str = utf8_to_wide(input);
743 return irr::core::stringw(str.c_str());