diff options
Diffstat (limited to 'zen/string_tools.h')
-rw-r--r-- | zen/string_tools.h | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/zen/string_tools.h b/zen/string_tools.h index 35a07b64..c8591522 100644 --- a/zen/string_tools.h +++ b/zen/string_tools.h @@ -35,7 +35,8 @@ template <class S, class T> S afterFirst (const S& str, const T& term); //return template <class S, class T> S beforeFirst(const S& str, const T& term); //returns the whole string if term not found template <class S, class T> std::vector<S> split(const S& str, const T& delimiter); -template <class S> void trim(S& str, bool fromLeft = true, bool fromRight = true); +template <class S> void trim ( S& str, bool fromLeft = true, bool fromRight = true); +template <class S> S trimCpy(const S& str, bool fromLeft = true, bool fromRight = true); template <class S, class T, class U> void replace ( S& str, const T& oldTerm, const U& newTerm, bool replaceAll = true); template <class S, class T, class U> S replaceCpy(const S& str, const T& oldTerm, const U& newTerm, bool replaceAll = true); @@ -331,9 +332,9 @@ void trim(S& str, bool fromLeft, bool fromRight) assert(fromLeft || fromRight); typedef typename GetCharType<S>::Type CharType; //don't use value_type! (wxString, Glib::ustring) - const CharType* const oldBegin = str.c_str(); - const CharType* newBegin = oldBegin; - const CharType* newEnd = oldBegin + str.length(); + const CharType* const oldBegin = strBegin(str); + const CharType* newBegin = oldBegin; + const CharType* newEnd = oldBegin + strLength(str); if (fromRight) while (newBegin != newEnd && isWhiteSpace(newEnd[-1])) @@ -350,6 +351,16 @@ void trim(S& str, bool fromLeft, bool fromRight) } +template <class S> inline +S trimCpy(const S& str, bool fromLeft, bool fromRight) +{ + //implementing trimCpy() in terms of trim(), instead of the other way round, avoids memory allocations when trimming from right! + S tmp = str; + trim(tmp, fromLeft, fromRight); + return tmp; +} + + namespace implementation { template <class S, class T> |