Repository navigation
Expand file tree
/
Copy pathstringUtils.cpp
More file actions
211 lines (173 loc) · 3.86 KB
/
Copy pathstringUtils.cpp
File metadata and controls
211 lines (173 loc) · 3.86 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "stringUtils.h"
#include "stringBuffer.h"
#ifdef WIN32
#else
#include <inttypes.h>
#endif
#include <ctype.h>
#include <cstddef>
#include <algorithm>
//__USING_STL_NS
//__WILLOW_BEGIN_NAMESPACE
//--------------------------------------------------------------------------
//
//--------------------------------------------------------------------------
StringUtils::StringUtils()
{}
//--------------------------------------------------------------------------
// static methods
//--------------------------------------------------------------------------
string StringUtils::toUpperCase(const string& s)
{
string d;
std::transform(s.begin(), s.end(),
std::insert_iterator<string>(d, d.begin()), (int(*)(int))toupper);
return d;
}
string StringUtils::toLowerCase(const string& s)
{
string d;
std::transform(s.begin(), s.end(),
std::insert_iterator<string>(d, d.begin()), (int(*)(int))tolower);
return d;
}
string StringUtils::ltrim(const string& s)
{
string::size_type pos = s.find_first_not_of(' ');
if (pos == string::npos)
{
return string();
}
return s.substr(pos);
}
string StringUtils::rtrim(const string& s)
{
string::size_type pos = s.find_first_not_of(' ');
if (pos == string::npos)
{
return string();
}
string::size_type n = s.find_last_not_of(' ') + 1;
return s.substr(0, n);
}
string StringUtils::trim(const string& s)
{
string::size_type pos = s.find_first_not_of(' ');
if (pos == string::npos)
{
return string();
}
string::size_type n = s.find_last_not_of(' ') - pos + 1;
return s.substr(pos, n);
}
string StringUtils::replace(const string& __s,size_t __pos, size_t __n, const string& __str)
{
StringBuffer sb(__s);
return sb.replace(__pos,__n,__str).str();
}
string StringUtils::replaceAll(const string& __s,size_t __pos,const string& __src, const string& __dst)
{
StringBuffer sb(__s);
return sb.replaceAll(__pos,__src,__dst).str();
}
bool StringUtils::equalsIgnoreCase(const string& s1, const string& s2)
{
return toLowerCase(s1) == toLowerCase(s2);
}
int StringUtils::compareNoCase(const string& s1,const string& s2)
{
return strcasecmp(s1.c_str(),s2.c_str()) ;
}
bool StringUtils::endsWith(const string& s, const string& suffix)
{
return (s.length() - s.rfind(suffix)) == suffix.length();
}
bool StringUtils::startsWith(const string& s, const string& prefix)
{
return (s.find(prefix) == 0);
}
string StringUtils::format(const char* format, ...)
{
va_list ap;
//--------------------------------------------
int size=1024;
int n=0;
char* p = 0;
if ((p = (char*)malloc (size)) == 0)
{
return string();
}
while(true)
{
va_start(ap,format);
n = vsnprintf(p,size,format,ap);
va_end(ap);
if(n > -1 && n < size)
{
string str(p,n);
free(p);
return str;
}
if(n>-1)
{
size = n + 1;
}
else
{
size *= 2;
}
if ((p = (char*)realloc (p, size)) == 0)
{
return string();
}
}
//--------------------------------------------
return string();
}
string StringUtils::format(const char* format,va_list& ap)
{
//--------------------------------------------
int size=1024;
int n=0;
char* p = 0;
if ((p = (char*)malloc (size)) == 0)
{
return string();
}
while(true)
{
n = vsnprintf(p,size,format,ap);
if(n > -1 && n < size)
{
string str(p,n);
free(p);
return str;
}
if(n>-1)
{
size = n + 1;
}
else
{
size *= 2;
}
if ((p = (char*)realloc (p, size)) == 0)
{
return string();
}
}
//--------------------------------------------
}
string StringUtils::left(const string& s , int len)
{
return s.substr(0,len) ;
}
string StringUtils::mid(const string& s, int from,int len)
{
return s.substr(from,len) ;
}
string StringUtils::right(const string& s , int len)
{
return s.substr(s.length()-len) ;
}
//__WILLOW_END_NAMESPACE