utils_string.h
浏览该文件的文档.
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);
103void UpdateStringMapIfNotExist(STRING_MAP& opts, const string& key, const string& value);
104
105#if defined(CPP_GCC) || defined(CPP_ICC)
106extern void _itoa_s(vint32_t value, char* buffer, size_t size, vint radix);
107extern void _itow_s(vint32_t value, wchar_t* buffer, size_t size, vint radix);
108extern void _i64toa_s(vint64_t value, char* buffer, size_t size, vint radix);
109extern void _i64tow_s(vint64_t value, wchar_t* buffer, size_t size, vint radix);
110extern void _uitoa_s(vuint32_t value, char* buffer, size_t size, vint radix);
111extern void _uitow_s(vuint32_t value, wchar_t* buffer, size_t size, vint radix);
112extern void _ui64toa_s(vuint64_t value, char* buffer, size_t size, vint radix);
113extern void _ui64tow_s(vuint64_t value, wchar_t* buffer, size_t size, vint radix);
114extern void _gcvt_s(char* buffer, size_t size, double value, vint numberOfDigits);
115#endif
116
117/*!
118 * \brief Convert a signed integer to a string
119 * \param[in] number The number to convert
120 * \return The converted string
121 */
122string itoa(vint number);
123
124/*!
125 * \brief Convert a signed integer to an unicode string
126 * \param[in] number The number to convert
127 * \return The converted unicode string
128 */
129wstring itow(vint number);
130
131/*!
132 * \brief Convert a 64-bits signed integer to a string
133 * \param[in] number The number to convert
134 * \return The converted string
135 */
136string i64toa(vint64_t number);
137
138/*!
139 * \brief Convert a 64-bits signed integer to an unicode string
140 * \param[in] number The number to convert
141 * \return The converted unicode string
142 */
143wstring i64tow(vint64_t number);
144
145/*!
146 * \brief Convert an unsigned integer to a string
147 * \param[in] number The number to convert
148 * \return The converted string
149 */
150string utoa(vuint number);
151
152/*!
153 * \brief Convert an unsigned integer to an unicode string
154 * \param[in] number The number to convert
155 * \return The converted unicode string
156 */
157wstring utow(vuint number);
158
159/*!
160 * \brief Convert a 64-bits unsigned integer to a string
161 * \param[in] number The number to convert
162 * \return The converted string
163 */
164string u64toa(vuint64_t number);
165
166/*!
167* \brief Convert a 64-bits unsigned integer to an unicode string
168* \param[in] number The number to convert
169* \return The converted unicode string
170*/
171wstring u64tow(vuint64_t number);
172
173/*!
174 * \brief Convert a 64-bits floating pointer number to a string
175 * \param[in] number The number to convert
176 * \return The converted string
177 */
178string ftoa(double number);
179
180/*!
181* \brief Convert a 64-bits floating pointer number to an unicode string
182* \param[in] number The number to convert
183* \return The converted unicode string
184*/
185wstring ftow(double number);
186
187/*!
188 * \brief Convert an unicode string to an Ansi string
189 * \param[in] wstr The unicode string to convert
190 * \return The converted ansi string
191 */
192string wtoa(const wstring& wstr);
193
194vint _wtoa(const wchar_t* w, char* a, vint chars);
195
196/*!
197 * \brief Convert an Ansi string to an unicode string
198 * \param[in] astr The Ansi string to convert
199 * \return The converted unicode string
200 */
201wstring atow(const string& astr);
202vint _atow(const char* a, wchar_t* w, vint chars);
203
204/*!
205 * \brief Get numeric values by splitting the given string based on the given delimiter
206 */
207template <typename T>
208bool SplitStringForValues(const string& items, const char delimiter, vector<T>& values);
209
210/*!
211 * \brief Check if a string is an signed integer, if ture, return the converted integer
212 * \param[in] num_str The string to convert
213 * \param[out] success Return true if succeed
214 * \return The converted number if succeed, otherwise the result is undefined.
215 */
216vint IsInt(const string& num_str, bool& success);
217
218/*!
219 * \brief Check if an unicode string is an signed integer
220 * \param[in] num_str The string to convert
221 * \param[out] success Return true if succeed
222 * \return The converted number if succeed, otherwise the result is undefined.
223 */
224vint IsInt(const wstring& num_str, bool& success);
225
226/*!
227 * \brief Convert a string to an signed 64-bits integer
228 * \param[in] num_str The string to convert
229 * \param[out] success Return true if succeed
230 * \return The converted number if succeed, otherwise the result is undefined.
231 */
232vint64_t IsInt64(const string& num_str, bool& success);
233
234/*!
235 * \brief Convert an unicode string to an signed 64-bits integer
236 * \param[in] num_str The string to convert
237 * \param[out] success Return true if succeed
238 * \return The converted number if succeed, otherwise the result is undefined.
239 */
240vint64_t IsInt64(const wstring& num_str, bool& success);
241
242/*!
243 * \brief Convert an Ansi string to an unsigned integer
244 * \param[in] num_str The string to convert
245 * \param[out] success Return true if succeed
246 * \return The converted number if succeed, otherwise the result is undefined.
247 */
248vuint IsUInt(const string& num_str, bool& success);
249
250/*!
251 * \brief Convert an Unicode string to an unsigned integer
252 * \param[in] num_str The string to convert
253 * \param[out] success Return true if succeed
254 * \return The converted number if succeed, otherwise the result is undefined.
255 */
256vuint IsUInt(const wstring& num_str, bool& success);
257
258/*!
259 * \brief Convert an Ansi string to a 64-bits unsigned integer
260 * \param[in] num_str The string to convert
261 * \param[out] success Return true if succeed
262 * \return The converted number if succeed, otherwise the result is undefined.
263 */
264vuint64_t IsUInt64(const string& num_str, bool& success);
265
266/*!
267 * \brief Convert an Unicode string to a 64-bits unsigned integer
268 * \param[in] num_str The string to convert
269 * \param[out] success Return true if succeed
270 * \return The converted number if succeed, otherwise the result is undefined.
271 */
272vuint64_t IsUInt64(const wstring& num_str, bool& success);
273
274/*!
275 * \brief Convert an Ansi string to 64-bits floating point number
276 * \param[in] num_str The string to convert
277 * \param[out] success Return true if succeed
278 * \return The converted number if succeed, otherwise the result is undefined.
279 */
280double IsDouble(const string& num_str, bool& success);
281
282/*!
283 * \brief Convert an Ansi string to 64-bits floating point number
284 * \param[in] num_str The string to convert
285 * \param[out] success Return true if succeed
286 * \return The converted number if succeed, otherwise the result is undefined.
287 */
288double IsDouble(const wstring& num_str, bool& success);
289
290
291/*!
292 * \brief Check if a string is a number (integer or float)
293 */
294template <typename STRING_T>
295bool IsNumber(const STRING_T& num_str);
296
297/*!
298 * \brief Convert an Ansi or Unicode string to an integer
299 */
300template <typename STRING_T>
301vint ToInt(const STRING_T& num_str);
302
303/*!
304 * \brief Convert an Ansi or Unicode string to an signed 64-bits integer
305 */
306template <typename STRING_T>
307vint64_t ToInt64(const STRING_T& num_str);
308
309/*!
310 * \brief Convert an Ansi or Unicode string to an unsigned integer
311 */
312template <typename STRING_T>
313vuint ToUInt(const STRING_T& num_str);
314
315/*!
316 * \brief Convert an Ansi or Unicode string to a 64-bits unsigned integer
317 */
318template <typename STRING_T>
319vuint64_t ToUInt64(const STRING_T& num_str);
320
321/*!
322 * \brief Convert an Ansi or Unicode string to a 64-bits floating point number
323 */
324template <typename STRING_T>
325double ToDouble(const STRING_T& num_str);
326
327
328template <typename STRING_T>
329vint ToInt(const STRING_T& num_str) {
330 bool success = false;
331 return IsInt(num_str, success);
332}
333
334template <typename STRING_T>
335vint64_t ToInt64(const STRING_T& num_str) {
336 bool success = false;
337 return IsInt64(num_str, success);
338}
339
340template <typename STRING_T>
341vuint ToUInt(const STRING_T& num_str) {
342 bool success = false;
343 return IsUInt(num_str, success);
344}
345
346template <typename STRING_T>
347vuint64_t ToUInt64(const STRING_T& num_str) {
348 bool success = false;
349 return IsUInt64(num_str, success);
350}
351
352template <typename STRING_T>
353double ToDouble(const STRING_T& num_str) {
354 bool success = false;
355 return IsDouble(num_str, success);
356}
357
358/************ Implementation of template functions ******************/
359template <typename T>
360bool SplitStringForValues(const string& items, const char delimiter, vector<T>& values) {
361 vector<string> value_strs = SplitString(items, delimiter);
362 if (value_strs.empty()) { return false; }
363 values.clear();
364 char* end = nullptr;
365 for (auto it = value_strs.begin(); it != value_strs.end(); ++it) {
366 if ((*it).find_first_of("0123456789") == string::npos) {
367 continue;
368 }
369 values.emplace_back(static_cast<T>(strtod((*it).c_str(), &end)));
370 }
371 vector<T>(values).swap(values);
372 return value_strs.size() == values.size();
373}
374
375template <typename STRING_T>
376bool IsNumber(const STRING_T& num_str) {
377 bool is_double = false;
378 IsDouble(num_str, is_double);
379 if (is_double) return true;
380 return false;
381}
382} /* namespace: utils_string */
383} /* namespace: ccgl */
384
385#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:329
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:335
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:353
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:347
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:341
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:360
bool IsNumber(const STRING_T &num_str)
Check if a string is a number (integer or float)
Definition: utils_string.h:376
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