utils_string.h
Go to the documentation of this file.
1
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 {
29namespace utils_string {
35string GetUpper(const string& str);
36
43bool StringMatch(const char* a, const char* b);
44
50bool StringMatch(const string& text1, const string& text2);
51
57void TrimSpaces(string& str);
58
65string& Trim(string& s);
66
72vector<string> SplitString(const string& item);
73
80vector<string> SplitString(const string& item, char delimiter);
81
87template <typename T>
88string ValueToString(const T& val) {
89 std::ostringstream oss;
90 oss << val;
91 return oss.str();
92}
93
97void CopyStringMap(const STRING_MAP& in_opts, STRING_MAP& out_opts);
98
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
121string itoa(vint number);
122
128wstring itow(vint number);
129
135string i64toa(vint64_t number);
136
142wstring i64tow(vint64_t number);
143
149string utoa(vuint number);
150
156wstring utow(vuint number);
157
163string u64toa(vuint64_t number);
164
170wstring u64tow(vuint64_t number);
171
177string ftoa(double number);
178
184wstring ftow(double number);
185
191string wtoa(const wstring& wstr);
192
193vint _wtoa(const wchar_t* w, char* a, vint chars);
194
200wstring atow(const string& astr);
201vint _atow(const char* a, wchar_t* w, vint chars);
202
206template <typename T>
207bool SplitStringForValues(const string& items, const char delimiter, vector<T>& values);
208
215vint IsInt(const string& num_str, bool& success);
216
223vint IsInt(const wstring& num_str, bool& success);
224
231vint64_t IsInt64(const string& num_str, bool& success);
232
239vint64_t IsInt64(const wstring& num_str, bool& success);
240
247vuint IsUInt(const string& num_str, bool& success);
248
255vuint IsUInt(const wstring& num_str, bool& success);
256
263vuint64_t IsUInt64(const string& num_str, bool& success);
264
271vuint64_t IsUInt64(const wstring& num_str, bool& success);
272
279double IsDouble(const string& num_str, bool& success);
280
287double IsDouble(const wstring& num_str, bool& success);
288
289
293template <typename STRING_T>
294bool IsNumber(const STRING_T& num_str);
295
299template <typename STRING_T>
300vint ToInt(const STRING_T& num_str);
301
305template <typename STRING_T>
306vint64_t ToInt64(const STRING_T& num_str);
307
311template <typename STRING_T>
312vuint ToUInt(const STRING_T& num_str);
313
317template <typename STRING_T>
318vuint64_t ToUInt64(const STRING_T& num_str);
319
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.
Definition: utils_string.cpp:170
wstring ftow(double number)
Convert a 64-bits floating pointer number to an unicode string.
Definition: utils_string.cpp:210
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.
Definition: utils_string.cpp:158
void CopyStringMap(const STRING_MAP &in_opts, STRING_MAP &out_opts)
Copy string map.
Definition: utils_string.cpp:78
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.
Definition: utils_string.cpp:200
void UpdateStringMap(STRING_MAP &opts, const string &key, const string &value)
Add or modify element in a string map.
Definition: utils_string.cpp:94
string i64toa(vint64_t number)
Convert a 64-bits signed integer to a string.
Definition: utils_string.cpp:164
vuint64_t IsUInt64(const string &num_str, bool &success)
Convert an Ansi string to a 64-bits unsigned integer.
Definition: utils_string.cpp:292
wstring u64tow(vuint64_t number)
Convert a 64-bits unsigned integer to an unicode string.
Definition: utils_string.cpp:194
string itoa(vint number)
Convert a signed integer to a string.
Definition: utils_string.cpp:152
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.
Definition: utils_string.cpp:182
vint IsInt(const string &num_str, bool &success)
Check if a string is an signed integer, if ture, return the converted integer.
Definition: utils_string.cpp:250
string GetUpper(const string &str)
Get Uppercase of given string.
Definition: utils_string.cpp:16
string u64toa(vuint64_t number)
Convert a 64-bits unsigned integer to a string.
Definition: utils_string.cpp:188
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".
Definition: utils_string.cpp:70
vector< string > SplitString(const string &item, const char delimiter)
Splits the given string based on the given delimiter.
Definition: utils_string.cpp:35
void TrimSpaces(string &str)
Trim Both leading and trailing spaces.
Definition: utils_string.cpp:22
vint64_t IsInt64(const string &num_str, bool &success)
Convert a string to an signed 64-bits integer.
Definition: utils_string.cpp:264
string wtoa(const wstring &wstr)
Convert an unicode string to an Ansi string.
Definition: utils_string.cpp:222
bool StringMatch(const char *a, const char *b)
Match char ignore cases.
Definition: utils_string.cpp:61
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.
Definition: utils_string.cpp:278
wstring atow(const string &astr)
Convert an Ansi string to an unicode string.
Definition: utils_string.cpp:240
string utoa(vuint number)
Convert an unsigned integer to a string.
Definition: utils_string.cpp:176
double IsDouble(const string &num_str, bool &success)
Convert an Ansi string to 64-bits floating point number.
Definition: utils_string.cpp:306
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17
std::map< string, string > STRING_MAP
Map of string key and string value.
Definition: basic.h:349