Added a function for stripping a string or a vector<string> from leading and trailing white spaces.

This commit is contained in:
Moritz Bunkus 2003-06-21 20:41:08 +00:00
parent 175bcdfb53
commit d52e2f51bf
2 changed files with 32 additions and 0 deletions

View File

@ -608,6 +608,36 @@ vector<string> split(const char *src, const char *pattern, int max_num) {
return v;
}
void strip(string &s) {
int i, len;
const char *c;
c = s.c_str();
i = 0;
while ((c[i] != 0) && isspace(c[i]))
i++;
if (i > 0)
s.erase(0, i);
c = s.c_str();
len = s.length();
i = 0;
while ((i < len) && isspace(c[len - i - 1]))
i++;
if (i > 0)
s.erase(len - i, i);
}
void strip(vector<string> &v) {
int i;
for (i = 0; i < v.size(); i++)
strip(v[i]);
}
/*
* Integer parsing
*/

View File

@ -105,6 +105,8 @@ void *_saferealloc(void *mem, size_t size, const char *file, int line);
vector<string> split(const char *src, const char *pattern = ",",
int max_num = -1);
void strip(string &s);
void strip(vector<string> &v);
UTFstring cstr_to_UTFstring(const char *c);
char *UTFstring_to_cstr(const UTFstring &u);