1 /////////////////////////////////////////////////////////////////////////////
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 /////////////////////////////////////////////////////////////////////////////
19 * @file UnicodeString.cpp
21 * @brief String utilities.
24 // String formatting code originally from Paul Senzee:
25 // http://www.senzee5.com/2006/05/c-formatting-stdstring.html
27 #include "UnicodeString.h"
39 * @brief Convert a string to lower case string.
40 * @param [in] str String to convert to lower case.
41 * @return Lower case string.
43 String makelower(const String &str)
46 String::size_type i = 0;
47 for (i = 0; i < ret.length(); i++)
48 ret[i] = _totlower(ret[i]);
53 * @brief Convert a string to upper case string.
54 * @param [in] str String to convert to upper case.
55 * @return upper case string.
57 String makeupper(const String &str)
60 String::size_type i = 0;
61 for (i = 0; i < ret.length(); i++)
62 ret[i] = _totupper(ret[i]);
67 * @brief Replace a string inside a string with another string.
68 * This function searches for a string inside another string an if found,
69 * replaces it with another string. Function can replace several instances
70 * of the string inside one string.
71 * @param [in] target A string containing another string to replace.
72 * @param [in] find A string to search and replace with another (@p replace).
73 * @param [in] replace A string used to replace original (@p find).
75 void replace(String &target, const String &find, const String &replace)
77 const String::size_type find_len = find.length();
78 const String::size_type replace_len = replace.length();
79 String::size_type pos = 0;
80 while ((pos = target.find(find, pos)) != String::npos)
82 target.replace(pos, find_len, replace);
88 * @brief Compare two strings ignoring the character casing.
89 * @param [in] str1 First string to compare.
90 * @param [in] str2 Second string to compare.
91 * @return As strcmp(), 0 if strings match.
93 int compare_nocase(const String &str1, const String &str2)
95 return _tcsicoll(str1.c_str(), str2.c_str());
99 * @brief Trims whitespace chars from begin and end of the string.
100 * @param [in] str the original string.
101 * @return Trimmed string.
103 String trim_ws(const String & str)
109 String::iterator it = result.begin();
110 while (it != result.end() && _istspace(*it))
113 if (it != result.begin())
114 result.erase(result.begin(), it);
119 it = result.end() - 1;
120 while (it != result.begin() && _istspace(*it))
123 if (it != result.end() - 1)
124 result.erase(it + 1, result.end());
129 * @brief Trims whitespace chars from begin of the string.
130 * @param [in] str the original string.
131 * @return Trimmed string.
133 String trim_ws_begin(const String & str)
139 String::iterator it = result.begin();
140 while (it != result.end() && _istspace(*it))
143 if (it != result.begin())
144 result.erase(result.begin(), it);
149 * @brief Trims whitespace chars from end of the string.
150 * @param [in] str the original string.
151 * @return Trimmed string.
153 String trim_ws_end(const String & str)
159 String::reverse_iterator it = result.rbegin();
160 while (it != result.rend() && _istspace(*it))
163 if (it != result.rbegin())
164 result.erase(it.base(), result.end());
168 String format_arg_list(const TCHAR *fmt, va_list args)
174 std::vector<TCHAR> buffer(length, 0);
177 result = _vsntprintf_s(&buffer[0], length, _TRUNCATE, fmt, args);
179 buffer.resize(length, 0);
181 String s(&buffer[0]);
186 * @brief printf()-style formatting for STL string.
187 * Use this function to format String:s in printf() style.
189 String format(const TCHAR *fmt, ...)
193 String s = format_arg_list(fmt, args);
198 String format_strings(const String& fmt, const String *args[], size_t nargs)
201 str.reserve(fmt.length() * 2);
202 String::const_iterator it;
203 for (it = fmt.begin(); it != fmt.end(); ++it)
211 if (n > 0 && static_cast<unsigned int>(n) <= nargs)
224 String format_string1(const String& fmt, const String& arg1)
226 const String* args[] = {&arg1};
227 return format_strings(fmt, args, 1);
230 String format_string2(const String& fmt, const String& arg1, const String& arg2)
232 const String* args[] = {&arg1, &arg2};
233 return format_strings(fmt, args, 2);