-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
225 lines (203 loc) · 5.17 KB
/
util.cpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Utility functions
Bill Forster, October 2018
*/
#include <iostream>
#include <string>
#include <stdarg.h> // For va_start, etc.
#include "util.h"
namespace util
{
void putline(std::ostream &out,const std::string &line)
{
out.write( line.c_str(), line.length() );
out.write( "\n", 1 );
}
std::string sprintf( const char *fmt, ... )
{
int size = strlen(fmt) * 3; // guess at size
std::string str;
va_list ap;
for(;;)
{
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)str.data(), size, fmt, ap);
va_end(ap);
if( n>-1 && n<size ) // are we done yet?
{
str.resize(n);
return str;
}
if( n > size ) // Needed size returned
size = n + 1; // For null char
else
size *= 4; // Guess at a larger size
}
return str;
}
bool prefix( const std::string &s, const std::string prefix )
{
size_t offset = s.find(prefix);
return (offset == 0);
}
bool suffix( const std::string &s, const std::string suffix )
{
return( s.length() >= suffix.length() &&
suffix == s.substr(s.length()-suffix.length(), suffix.length())
);
}
bool prefix_remove( std::string &s, const std::string prefix )
{
size_t offset = s.find(prefix);
bool found = (offset == 0);
if( found )
s = s.substr(prefix.length());
return found;
}
void ltrim( std::string &s )
{
size_t first_char_offset = s.find_first_not_of(" \n\r\t");
if( first_char_offset == std::string::npos )
s.clear();
else
s = s.substr(first_char_offset);
}
void rtrim( std::string &s )
{
size_t final_char_offset = s.find_last_not_of(" \n\r\t");
if( final_char_offset == std::string::npos )
s.clear();
else
s.erase(final_char_offset+1);
}
// Try for a little efficiency, return true if changes made
bool trim( std::string &s )
{
bool changed=false;
size_t len = s.length();
if( len > 0 )
{
size_t first_char_offset = s.find_first_not_of(" \n\r\t");
if( first_char_offset == std::string::npos )
{
s.clear(); // string is all whitespace
return true;
}
else if( first_char_offset > 0 )
{
s = s.substr(first_char_offset); // effect left trim
changed = true;
}
size_t final_char_offset = s.find_last_not_of(" \n\r\t");
if( final_char_offset != std::string::npos && final_char_offset < len-1 )
{
s.erase(final_char_offset+1); // effect right trim
changed = true;
}
}
return changed;
}
static bool test_expect( std::string test_id, bool b, bool b_expected, const std::string s, const std::string s_expected )
{
bool ok=true;
if( b != b_expected )
{
printf( "test %s: failed, 1st parameter wrong\n", test_id.c_str() );
ok = false;
}
if( s != s_expected )
{
printf( "test %s: failed, 2nd parameter was %s\n", test_id.c_str(), s.c_str() );
ok = false;
}
return ok;
}
void tests()
{
std::string s;
s = " ";
bool changed = trim(s);
test_expect( "1.0", changed, true, s, "" );
s = " hello";
changed = trim(s);
test_expect( "1.1", changed, true, s, "hello" );
s = "hello ";
changed = trim(s);
test_expect( "1.2", changed, true, s, "hello" );
s = " hello ";
changed = trim(s);
test_expect( "1.3", changed, true, s, "hello" );
s = "hello";
changed = trim(s);
test_expect( "1.4", changed, false, s, "hello" );
}
void replace_all( std::string &s, const std::string from, const std::string to )
{
size_t next = 0;
for(;;)
{
size_t offset = s.find(from,next);
if( offset == std::string::npos )
break;
else
s.replace(offset,from.length(),to);
next = offset+to.length();
}
}
void replace_once( std::string &s, const std::string from, const std::string to )
{
size_t offset = s.find(from);
if( offset != std::string::npos )
s.replace(offset,from.length(),to);
}
void split( std::string &s, std::vector<std::string> &fields )
{
fields.clear();
size_t next = 0;
bool more = true;
while( more && s.length()>next)
{
size_t offset = s.find(' ',next);
std::string frag;
if( offset != std::string::npos )
frag = s.substr(next,offset-next);
else
{
frag = s.substr(next);
more = false;
}
fields.push_back(frag);
if( more )
next = offset+1;
}
}
std::string toupper( const std::string &s )
{
std::string r = s;
for( unsigned int i=0; i<r.length(); i++ )
{
char c = r[i];
if( 'a'<=c && c<='z' )
{
c -= 0x20;
r[i] = c;
}
}
return r;
}
std::string tolower( const std::string &s )
{
std::string r = s;
for( unsigned int i=0; i<r.length(); i++ )
{
char c = r[i];
if( 'A'<=c && c<='Z' )
{
c += 0x20;
r[i] = c;
}
}
return r;
}
} //namespace util