From d52e2f51bf92a7687ed1d39efb6d803306bd3016 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sat, 21 Jun 2003 20:41:08 +0000 Subject: [PATCH] Added a function for stripping a string or a vector from leading and trailing white spaces. --- common.cpp | 30 ++++++++++++++++++++++++++++++ common.h | 2 ++ 2 files changed, 32 insertions(+) diff --git a/common.cpp b/common.cpp index 0b1405d40..2351f87a2 100644 --- a/common.cpp +++ b/common.cpp @@ -608,6 +608,36 @@ vector 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 &v) { + int i; + + for (i = 0; i < v.size(); i++) + strip(v[i]); +} + /* * Integer parsing */ diff --git a/common.h b/common.h index 985ef8d2d..13823b2b6 100644 --- a/common.h +++ b/common.h @@ -105,6 +105,8 @@ void *_saferealloc(void *mem, size_t size, const char *file, int line); vector split(const char *src, const char *pattern = ",", int max_num = -1); +void strip(string &s); +void strip(vector &v); UTFstring cstr_to_UTFstring(const char *c); char *UTFstring_to_cstr(const UTFstring &u);