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)
39 std::wstring narrow_to_wide(const std::string& mbs);
40 std::string wide_to_narrow(const std::wstring& wcs);
42 static inline std::string padStringRight(std::string s, size_t len)
45 s.insert(s.end(), len - s.size(), ' ');
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[])
53 const char **p = ends;
54 for(; (*p) && (*p)[0] != '\0'; p++){
56 if(s.size() < end.size())
58 if(s.substr(s.size()-end.size(), end.size()) == end)
59 return s.substr(0, s.size() - end.size());
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)
70 if(s1.size() != s2.size())
72 for(size_t i = 0; i < s1.size(); ++i)
73 if(tolower(s1[i]) != tolower(s2[i]))
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)
87 if(str.size() < prefix.size())
91 for(size_t i = 0; i < prefix.size(); ++i)
92 if(tolower(str[i]) != tolower(prefix[i]))
97 for(size_t i = 0; i < prefix.size(); ++i)
98 if(str[i] != prefix[i])
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)
108 std::vector<std::wstring> parts;
109 std::wstringstream sstr(str);
111 while(std::getline(sstr, part, delimiter))
112 parts.push_back(part);
116 inline std::vector<std::string> str_split(const std::string &str, char delimiter) {
118 std::vector<std::string> parts;
119 std::stringstream sstr(str);
121 while(std::getline(sstr, part, delimiter))
122 parts.push_back(part);
126 inline std::string lowercase(const std::string &s)
129 for(size_t i = 0; i < s.size(); i++)
130 s2[i] = tolower(s.at(i));
134 inline std::string trim(const std::string &s)
137 while(s[front] == ' ' ||
144 size_t back = s.size();
145 while(back > front &&
154 return s.substr(front, back - front);
157 inline bool is_yes(const std::string &s)
159 std::string s2 = lowercase(trim(s));
160 if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
165 inline s32 mystoi(const std::string &s, s32 min, s32 max)
167 s32 i = atoi(s.c_str());
175 inline s64 stoi64(const std::string &s) {
176 std::stringstream tmp(s);
182 // MSVC2010 includes it's own versions of these
183 //#if !defined(_MSC_VER) || _MSC_VER < 1600
185 inline s32 mystoi(const std::string &s)
187 return atoi(s.c_str());
190 inline s32 mystoi(const std::wstring &s)
192 return atoi(wide_to_narrow(s).c_str());
195 inline float mystof(const std::string &s)
197 // This crap causes a segfault in certain cases on MinGW
199 std::istringstream ss(s);
202 // This works in that case
203 return atof(s.c_str());
211 inline std::string itos(s32 i)
213 std::ostringstream o;
218 inline std::string i64tos(s64 i) {
219 std::ostringstream o;
224 inline std::string ftos(float f)
226 std::ostringstream o;
231 inline void str_replace(std::string & str, std::string const & pattern,
232 std::string const & replacement)
234 std::string::size_type start = str.find(pattern, 0);
235 while(start != str.npos)
237 str.replace(start, pattern.size(), replacement);
238 start = str.find(pattern, start+replacement.size());
242 inline void str_replace_char(std::string & str, char from, char to)
244 for(unsigned int i=0; i<str.size(); i++)
252 Checks if a string contains only supplied characters
254 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
256 for(u32 i=0; i<s.size(); i++)
258 bool confirmed = false;
259 for(u32 j=0; j<allowed_chars.size(); j++)
261 if(s[i] == allowed_chars[j])
267 if(confirmed == false)
274 Checks if a string contains no blacklisted characters (opposite
275 function of string_allowed())
277 inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars)
279 for(unsigned int i = 0; i < s.length(); i++)
281 bool invalid = false;
282 for(unsigned int j = 0; j < blacklisted_chars.length(); j++)
284 if(s[i] == blacklisted_chars[j])
297 Forcefully wraps string into rows using \n
298 (no word wrap, used for showing paths in gui)
300 inline std::string wrap_rows(const std::string &from, u32 rowlen)
303 for(u32 i=0; i<from.size(); i++)
305 if(i != 0 && i%rowlen == 0)
313 Removes all \\ from a string that had been escaped (FormSpec strings)
315 inline std::string unescape_string(std::string &s)
319 for (size_t i = 0; i < s.length(); i++) {
328 inline bool is_number(const std::string& tocheck)
330 std::string::const_iterator iter = tocheck.begin();
332 while (iter != tocheck.end() && std::isdigit(*iter)) {
336 return ((!tocheck.empty()) && (iter == tocheck.end()));
339 std::string translatePassword(std::string playername, std::wstring password);
340 std::string urlencode(std::string str);
341 std::string urldecode(std::string str);
342 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
343 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
344 size_t mystrlcpy(char *dst, const char *src, size_t size);
345 char *mystrtok_r(char *s, const char *sep, char **lasts);
346 u64 read_seed(const char *str);
347 bool parseColorString(const std::string &value, video::SColor &color, bool quiet);