-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtil.hxx
86 lines (70 loc) · 2.82 KB
/
StringUtil.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef __StringUtil_hxx
#define __StringUtil_hxx
#include <iostream>
#include <sstream>
#include <vector>
namespace ctkCLI {
template <typename T> inline std::string toString(const T& in)
{
std::ostringstream strstr;
strstr << in;
return strstr.str();
}
template <typename T> inline T stringTo(const std::string& in)
{
T value;
std::istringstream strstr(in);
strstr >> value;
return value;
}
template <> inline std::string toString<>(const std::string& in) { return in; }
template <> inline std::string stringTo<>(const std::string& in) { return in; }
template <> inline std::string toString<>(const bool& in) { return in ? "true" : "false"; }
template <> inline bool stringTo<>(const std::string& in)
{
if (in=="true" || in=="yes") return true;
if (in=="false" || in=="no") return false;
return stringTo<int>(in)>0;
}
template <typename T> inline std::string vectortoString(const std::vector<T>& in, const std::string& delim=" ")
{
if (in.empty()) return std::string();
typename std::vector<T>::const_iterator it=in.begin();
std::string ret=ctkCLI::toString(*it);
for (++it;it!=in.end();++it)
ret+=delim+ctkCLI::toString(*it);
return ret;
}
template <typename T> inline std::vector<T> stringToVector(const std::string& in, const char delim=' ')
{
std::string item;
std::vector<T> ret;
std::istringstream str(in);
for (;std::getline(str,item,delim);str&&!str.eof())
ret.push_back(stringTo<T>(item));
if (item.empty()) ret.pop_back();
return ret;
}
template <> inline std::string toString<>(const std::vector<int>& in) {return vectortoString<int>(in,",");}
template <> inline std::vector<int> stringTo<>(const std::string& in) {return stringToVector<int>(in,',');}
template <> inline std::string toString<>(const std::vector<float>& in) {return vectortoString<float>(in,",");}
template <> inline std::vector<float> stringTo<>(const std::string& in) {return stringToVector<float>(in,',');}
template <> inline std::string toString<>(const std::vector<double>& in) {return vectortoString<double>(in,",");}
template <> inline std::vector<double> stringTo<>(const std::string& in) {return stringToVector<double>(in,',');}
template <> inline std::string toString<>(const std::vector<std::string>& in) {return vectortoString<std::string>(in,",");}
template <> inline std::vector<std::string> stringTo<>(const std::string& in) {return stringToVector<std::string>(in,',');}
inline void rtrim(std::string &str , const std::string& t = " \t")
{
str.erase(str.find_last_not_of(t)+1);
}
inline void ltrim(std::string& str, const std::string& t = " \t")
{
str.erase(0,str.find_first_not_of(t));
}
inline void trim(std::string& str, const std::string& t = " \t")
{
ltrim(str,t);
rtrim(str,t);
}
} // namespace ctkCLI
#endif // __StringUtil_hxx