3 Copyright (C) 2010-2012 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.h"
24 #include "../strfnd.h" // For trim()
31 static inline std::string padStringRight(std::string s, size_t len)
34 s.insert(s.end(), len - s.size(), ' ');
38 // ends: NULL- or ""-terminated array of strings
39 // Returns "" if no end could be removed.
40 static inline std::string removeStringEnd(const std::string &s, const char *ends[])
42 const char **p = ends;
43 for(; (*p) && (*p)[0] != '\0'; p++){
45 if(s.size() < end.size())
47 if(s.substr(s.size()-end.size(), end.size()) == end)
48 return s.substr(0, s.size() - end.size());
53 // Tests if two strings are equal, optionally case insensitive
54 inline bool str_equal(const std::wstring& s1, const std::wstring& s2,
55 bool case_insensitive = false)
59 if(s1.size() != s2.size())
61 for(size_t i = 0; i < s1.size(); ++i)
62 if(tolower(s1[i]) != tolower(s2[i]))
72 // Tests if the second string is a prefix of the first, optionally case insensitive
73 inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix,
74 bool case_insensitive = false)
76 if(str.size() < prefix.size())
80 for(size_t i = 0; i < prefix.size(); ++i)
81 if(tolower(str[i]) != tolower(prefix[i]))
86 for(size_t i = 0; i < prefix.size(); ++i)
87 if(str[i] != prefix[i])
93 inline std::wstring narrow_to_wide(const std::string& mbs)
95 size_t wcl = mbs.size();
96 Buffer<wchar_t> wcs(wcl+1);
97 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
99 return L"<invalid multibyte string>";
104 inline std::string wide_to_narrow(const std::wstring& wcs)
106 size_t mbl = wcs.size()*4;
107 SharedBuffer<char> mbs(mbl+1);
108 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
109 if(l == (size_t)(-1))
116 // Split a string using the given delimiter. Returns a vector containing
117 // the component parts.
118 inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
120 std::vector<std::wstring> parts;
121 std::wstringstream sstr(str);
123 while(std::getline(sstr, part, delimiter))
124 parts.push_back(part);
128 inline std::string lowercase(const std::string &s)
131 for(size_t i=0; i<s.size(); i++)
134 if(c >= 'A' && c <= 'Z')
141 inline bool is_yes(const std::string &s)
143 std::string s2 = lowercase(trim(s));
144 if(s2 == "y" || s2 == "yes" || s2 == "true" || s2 == "1")
149 inline s32 mystoi(const std::string &s, s32 min, s32 max)
151 s32 i = atoi(s.c_str());
160 // MSVC2010 includes it's own versions of these
161 //#if !defined(_MSC_VER) || _MSC_VER < 1600
163 inline s32 mystoi(const std::string &s)
165 return atoi(s.c_str());
168 inline s32 mystoi(const std::wstring &s)
170 return atoi(wide_to_narrow(s).c_str());
173 inline float mystof(const std::string &s)
175 // This crap causes a segfault in certain cases on MinGW
177 std::istringstream ss(s);
180 // This works in that case
181 return atof(s.c_str());
189 inline std::string itos(s32 i)
191 std::ostringstream o;
196 inline std::string ftos(float f)
198 std::ostringstream o;
203 inline void str_replace(std::string & str, std::string const & pattern,
204 std::string const & replacement)
206 std::string::size_type start = str.find(pattern, 0);
207 while(start != str.npos)
209 str.replace(start, pattern.size(), replacement);
210 start = str.find(pattern, start+replacement.size());
214 inline void str_replace_char(std::string & str, char from, char to)
216 for(unsigned int i=0; i<str.size(); i++)
224 Checks if a string contains only supplied characters
226 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
228 for(u32 i=0; i<s.size(); i++)
230 bool confirmed = false;
231 for(u32 j=0; j<allowed_chars.size(); j++)
233 if(s[i] == allowed_chars[j])
239 if(confirmed == false)
246 Checks if a string contains no blacklisted characters (opposite
247 function of string_allowed())
249 inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars)
251 for(unsigned int i = 0; i < s.length(); i++)
253 bool invalid = false;
254 for(unsigned int j = 0; j < blacklisted_chars.length(); j++)
256 if(s[i] == blacklisted_chars[j])
269 Forcefully wraps string into rows using \n
270 (no word wrap, used for showing paths in gui)
272 inline std::string wrap_rows(const std::string &from, u32 rowlen)
275 for(u32 i=0; i<from.size(); i++)
277 if(i != 0 && i%rowlen == 0)
284 std::string translatePassword(std::string playername, std::wstring password);
285 size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata);