utils_string.h
Go to the documentation of this file.
1/*!
2 * \file utils_string.h
3 * \brief Handling string related issues in CCGL.
4 *
5 * \remarks
6 * - 1. 2018-05-02 - lj - Make part of CCGL.
7 * - 2. 2018-11-12 - lj - Add check and conversion between string and number (int, float, double)
8 *
9 * \author Liangjun Zhu, zlj(at)lreis.ac.cn
10 * \version 1.1
11 */
12#ifndef CCGL_UTILS_STRING_H
13#define CCGL_UTILS_STRING_H
14
15#include <sstream>
16#include <vector>
17
18#include "basic.h"
19
20using std::string;
21using std::wstring;
22using std::vector;
23
24namespace ccgl {
25/*!
26 * \namespace ccgl::utils_string
27 * \brief String related functions
28 */
29namespace utils_string {
30/*!
31 * \brief Get Uppercase of given string
32 * \param[in] str
33 * \return Uppercase string
34 */
35string GetUpper(const string& str);
36
37/*!
38 * \brief Match \a char ignore cases
39 * \param[in] a, b \a char*
40 * \return true or false
41 * \sa StringMatch()
42 */
43bool StringMatch(const char* a, const char* b);
44
45/*!
46 * \brief Match Strings in UPPERCASE manner
47 * \param[in] text1, text2
48 * \return true or false
49 */
50bool StringMatch(const string& text1, const string& text2);
51
52/*!
53 * \brief Trim Both leading and trailing spaces
54 * \sa Trim
55 * \param[in] str \a string
56 */
57void TrimSpaces(string& str);
58
59/*!
60 * \brief Trim given string's heading and tailing by "<space>,\n,\t,\r"
61 * \sa TrimSpaces
62 * \param[in] s \a string information
63 * \return Trimmed string
64 */
65string& Trim(string& s);
66
67/*!
68 * \brief Splits the given string by spaces
69 * \param[in] item \a string information
70 * \return The split strings vector
71 */
72vector<string> SplitString(const string& item);
73
74/*!
75 * \brief Splits the given string based on the given delimiter
76 * \param[in] item \a string information
77 * \param[in] delimiter \a char
78 * \return The split strings vector
79 */
80vector<string> SplitString(const string& item, char delimiter);
81
82/*!
83 * \brief Convert value to string
84 * \param[in] val value, e.g., a int, or float
85 * \return converted string
86 */
87template <typename T>
88string ValueToString(const T& val) {
89 std::ostringstream oss;
90 oss << val;
91 return oss.str();
92}
93
94/*!
95 * \brief Copy string map
96 */
97void CopyStringMap(const STRING_MAP& in_opts, STRING_MAP& out_opts);
98
99/*!
100 * \brief Add or modify element in a string map
101 */
102void UpdateStringMap(STRING_MAP& opts, const string& key, const string& value);
103
104#if defined(CPP_GCC) || defined(CPP_ICC)
105extern void _itoa_s(vint32_t value, char* buffer, size_t size, vint radix);
106extern void _itow_s(vint32_t value, wchar_t* buffer, size_t size, vint radix);
107extern void _i64toa_s(vint64_t value, char* buffer, size_t size, vint radix);
108extern void _i64tow_s(vint64_t value, wchar_t* buffer, size_t size, vint radix);
109extern void _uitoa_s(vuint32_t value, char* buffer, size_t size, vint radix);
110extern void _uitow_s(vuint32_t value, wchar_t* buffer, size_t size, vint radix);
111extern void _ui64toa_s(vuint64_t value, char* buffer, size_t size, vint radix);
112extern void _ui64tow_s(vuint64_t value, wchar_t* buffer, size_t size, vint radix);
113extern void _gcvt_s(char* buffer, size_t size, double value, vint numberOfDigits);
114#endif
115
116/*!
117 * \brief Convert a signed integer to a string
118 * \param[in] number The number to convert
119 * \return The converted string
120 */
121string itoa(vint number);
122
123/*!
124 * \brief Convert a signed integer to an unicode string
125 * \param[in] number The number to convert
126 * \return The converted unicode string
127 */
128wstring itow(vint number);
129
130/*!
131 * \brief Convert a 64-bits signed integer to a string
132 * \param[in] number The number to convert
133 * \return The converted string
134 */
135string i64toa(vint64_t number);
136
137/*!
138 * \brief Convert a 64-bits signed integer to an unicode string
139 * \param[in] number The number to convert
140 * \return The converted unicode string
141 */
142wstring i64tow(vint64_t number);
143
144/*!
145 * \brief Convert an unsigned integer to a string
146 * \param[in] number The number to convert
147 * \return The converted string
148 */
149string utoa(vuint number);
150
151/*!
152 * \brief Convert an unsigned integer to an unicode string
153 * \param[in] number The number to convert
154 * \return The converted unicode string
155 */
156wstring utow(vuint number);
157
158/*!
159 * \brief Convert a 64-bits unsigned integer to a string
160 * \param[in] number The number to convert
161 * \return The converted string
162 */
163string u64toa(vuint64_t number);
164
165/*!
166* \brief Convert a 64-bits unsigned integer to an unicode string
167* \param[in] number The number to convert
168* \return The converted unicode string
169*/
170wstring u64tow(vuint64_t number);
171
172/*!
173 * \brief Convert a 64-bits floating pointer number to a string
174 * \param[in] number The number to convert
175 * \return The converted string
176 */
177string ftoa(double number);
178
179/*!
180* \brief Convert a 64-bits floating pointer number to an unicode string
181* \param[in] number The number to convert
182* \return The converted unicode string
183*/
184wstring ftow(double number);
185
186/*!
187 * \brief Convert an unicode string to an Ansi string
188 * \param[in] wstr The unicode string to convert
189 * \return The converted ansi string
190 */
191string wtoa(const wstring& wstr);
192
193vint _wtoa(const wchar_t* w, char* a, vint chars);
194
195/*!
196 * \brief Convert an Ansi string to an unicode string
197 * \param[in] astr The Ansi string to convert
198 * \return The converted unicode string
199 */
200wstring atow(const string& astr);
201vint _atow(const char* a, wchar_t* w, vint chars);
202
203/*!
204 * \brief Get numeric values by splitting the given string based on the given delimiter
205 */
206template <typename T>
207bool SplitStringForValues(const string& items, const char delimiter, vector<T>& values);
208
209/*!
210 * \brief Check if a string is an signed integer, if ture, return the converted integer
211 * \param[in] num_str The string to convert
212 * \param[out] success Return true if succeed
213 * \return The converted number if succeed, otherwise the result is undefined.
214 */
215vint IsInt(const string& num_str, bool& success);
216
217/*!
218 * \brief Check if an unicode string is an signed integer
219 * \param[in] num_str The string to convert
220 * \param[out] success Return true if succeed
221 * \return The converted number if succeed, otherwise the result is undefined.
222 */
223vint IsInt(const wstring& num_str, bool& success);
224
225/*!
226 * \brief Convert a string to an signed 64-bits integer
227 * \param[in] num_str The string to convert
228 * \param[out] success Return true if succeed
229 * \return The converted number if succeed, otherwise the result is undefined.
230 */
231vint64_t IsInt64(const string& num_str, bool& success);
232
233/*!
234 * \brief Convert an unicode string to an signed 64-bits integer
235 * \param[in] num_str The string to convert
236 * \param[out] success Return true if succeed
237 * \return The converted number if succeed, otherwise the result is undefined.
238 */
239vint64_t IsInt64(const wstring& num_str, bool& success);
240
241/*!
242 * \brief Convert an Ansi string to an unsigned integer
243 * \param[in] num_str The string to convert
244 * \param[out] success Return true if succeed
245 * \return The converted number if succeed, otherwise the result is undefined.
246 */
247vuint IsUInt(const string& num_str, bool& success);
248
249/*!
250 * \brief Convert an Unicode string to an unsigned integer
251 * \param[in] num_str The string to convert
252 * \param[out] success Return true if succeed
253 * \return The converted number if succeed, otherwise the result is undefined.
254 */
255vuint IsUInt(const wstring& num_str, bool& success);
256
257/*!
258 * \brief Convert an Ansi string to a 64-bits unsigned integer
259 * \param[in] num_str The string to convert
260 * \param[out] success Return true if succeed
261 * \return The converted number if succeed, otherwise the result is undefined.
262 */
263vuint64_t IsUInt64(const string& num_str, bool& success);
264
265/*!
266 * \brief Convert an Unicode string to a 64-bits unsigned integer
267 * \param[in] num_str The string to convert
268 * \param[out] success Return true if succeed
269 * \return The converted number if succeed, otherwise the result is undefined.
270 */
271vuint64_t IsUInt64(const wstring& num_str, bool& success);
272
273/*!
274 * \brief Convert an Ansi string to 64-bits floating point number
275 * \param[in] num_str The string to convert
276 * \param[out] success Return true if succeed
277 * \return The converted number if succeed, otherwise the result is undefined.
278 */
279double IsDouble(const string& num_str, bool& success);
280
281/*!
282 * \brief Convert an Ansi string to 64-bits floating point number
283 * \param[in] num_str The string to convert
284 * \param[out] success Return true if succeed
285 * \return The converted number if succeed, otherwise the result is undefined.
286 */
287double IsDouble(const wstring& num_str, bool& success);
288
289
290/*!
291 * \brief Check if a string is a number (integer or float)
292 */
293template <typename STRING_T>
294bool IsNumber(const STRING_T& num_str);
295
296/*!
297 * \brief Convert an Ansi or Unicode string to an integer
298 */
299template <typename STRING_T>
300vint ToInt(const STRING_T& num_str);
301
302/*!
303 * \brief Convert an Ansi or Unicode string to an signed 64-bits integer
304 */
305template <typename STRING_T>
306vint64_t ToInt64(const STRING_T& num_str);
307
308/*!
309 * \brief Convert an Ansi or Unicode string to an unsigned integer
310 */
311template <typename STRING_T>
312vuint ToUInt(const STRING_T& num_str);
313
314/*!
315 * \brief Convert an Ansi or Unicode string to a 64-bits unsigned integer
316 */
317template <typename STRING_T>
318vuint64_t ToUInt64(const STRING_T& num_str);
319
320/*!
321 * \brief Convert an Ansi or Unicode string to a 64-bits floating point number
322 */
323template <typename STRING_T>
324double ToDouble(const STRING_T& num_str);
325
326
327template <typename STRING_T>
328vint ToInt(const STRING_T& num_str) {
329 bool success = false;
330 return IsInt(num_str, success);
331}
332
333template <typename STRING_T>
334vint64_t ToInt64(const STRING_T& num_str) {
335 bool success = false;
336 return IsInt64(num_str, success);
337}
338
339template <typename STRING_T>
340vuint ToUInt(const STRING_T& num_str) {
341 bool success = false;
342 return IsUInt(num_str, success);
343}
344
345template <typename STRING_T>
346vuint64_t ToUInt64(const STRING_T& num_str) {
347 bool success = false;
348 return IsUInt64(num_str, success);
349}
350
351template <typename STRING_T>
352double ToDouble(const STRING_T& num_str) {
353 bool success = false;
354 return IsDouble(num_str, success);
355}
356
357/************ Implementation of template functions ******************/
358template <typename T>
359bool SplitStringForValues(const string& items, const char delimiter, vector<T>& values) {
360 vector<string> value_strs = SplitString(items, delimiter);
361 if (value_strs.empty()) { return false; }
362 values.clear();
363 char* end = nullptr;
364 for (auto it = value_strs.begin(); it != value_strs.end(); ++it) {
365 if ((*it).find_first_of("0123456789") == string::npos) {
366 continue;
367 }
368 values.emplace_back(static_cast<T>(strtod((*it).c_str(), &end)));
369 }
370 vector<T>(values).swap(values);
371 return value_strs.size() == values.size();
372}
373
374template <typename STRING_T>
375bool IsNumber(const STRING_T& num_str) {
376 bool is_double = false;
377 IsDouble(num_str, is_double);
378 if (is_double) return true;
379 return false;
380}
381} /* namespace: utils_string */
382} /* namespace: ccgl */
383
384#endif /* CCGL_UTILS_STRING_H */
Basic definitions.
vint ToInt(const STRING_T &num_str)
Convert an Ansi or Unicode string to an integer.
Definition: utils_string.h:328
wstring i64tow(vint64_t number)
Convert a 64-bits signed integer to an unicode string.
wstring ftow(double number)
Convert a 64-bits floating pointer number to an unicode string.
string ValueToString(const T &val)
Convert value to string.
Definition: utils_string.h:88
vint64_t ToInt64(const STRING_T &num_str)
Convert an Ansi or Unicode string to an signed 64-bits integer.
Definition: utils_string.h:334
wstring itow(vint number)
Convert a signed integer to an unicode string.
vector< string > SplitString(const string &item)
Splits the given string by spaces.
void CopyStringMap(const STRING_MAP &in_opts, STRING_MAP &out_opts)
Copy string map.
double ToDouble(const STRING_T &num_str)
Convert an Ansi or Unicode string to a 64-bits floating point number.
Definition: utils_string.h:352
string ftoa(double number)
Convert a 64-bits floating pointer number to a string.
void UpdateStringMap(STRING_MAP &opts, const string &key, const string &value)
Add or modify element in a string map.
string i64toa(vint64_t number)
Convert a 64-bits signed integer to a string.
vuint64_t IsUInt64(const string &num_str, bool &success)
Convert an Ansi string to a 64-bits unsigned integer.
wstring u64tow(vuint64_t number)
Convert a 64-bits unsigned integer to an unicode string.
string itoa(vint number)
Convert a signed integer to a string.
vuint64_t ToUInt64(const STRING_T &num_str)
Convert an Ansi or Unicode string to a 64-bits unsigned integer.
Definition: utils_string.h:346
wstring utow(vuint number)
Convert an unsigned integer to an unicode string.
vint IsInt(const string &num_str, bool &success)
Check if a string is an signed integer, if ture, return the converted integer.
string GetUpper(const string &str)
Get Uppercase of given string.
string u64toa(vuint64_t number)
Convert a 64-bits unsigned integer to a string.
vuint ToUInt(const STRING_T &num_str)
Convert an Ansi or Unicode string to an unsigned integer.
Definition: utils_string.h:340
string & Trim(string &s)
Trim given string's heading and tailing by "<space>,\n,\t,\r".
void TrimSpaces(string &str)
Trim Both leading and trailing spaces.
vint64_t IsInt64(const string &num_str, bool &success)
Convert a string to an signed 64-bits integer.
string wtoa(const wstring &wstr)
Convert an unicode string to an Ansi string.
bool StringMatch(const char *a, const char *b)
Match char ignore cases.
bool SplitStringForValues(const string &items, const char delimiter, vector< T > &values)
Get numeric values by splitting the given string based on the given delimiter.
Definition: utils_string.h:359
bool IsNumber(const STRING_T &num_str)
Check if a string is a number (integer or float)
Definition: utils_string.h:375
vuint IsUInt(const string &num_str, bool &success)
Convert an Ansi string to an unsigned integer.
wstring atow(const string &astr)
Convert an Ansi string to an unicode string.
string utoa(vuint number)
Convert an unsigned integer to a string.
double IsDouble(const string &num_str, bool &success)
Convert an Ansi string to 64-bits floating point number.
Common Cross-platform Geographic Library (CCGL)
std::map< string, string > STRING_MAP
Map of string key and string value.
Definition: basic.h:349