diff options
author | Daniel Wilhelm <daniel@wili.li> | 2015-10-02 14:55:19 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2015-10-02 14:55:19 +0200 |
commit | 46fc289a8776ba253e97d01d6948fb1031ea1973 (patch) | |
tree | b16a99c60f21b04c001f29862bf2ee16ae3a0e00 /zen/string_tools.h | |
parent | 6.15 (diff) | |
download | FreeFileSync-46fc289a8776ba253e97d01d6948fb1031ea1973.tar.gz FreeFileSync-46fc289a8776ba253e97d01d6948fb1031ea1973.tar.bz2 FreeFileSync-46fc289a8776ba253e97d01d6948fb1031ea1973.zip |
7.0
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> |