From 8f330dff30125d72b2ff883f6355536c567c0a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Tue, 29 Nov 2005 12:38:46 +0000 Subject: [PATCH] - make it compilable with gcc 4.0.2 and newer doxygen - added split and join --- mrw/string.hpp | 109 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 86 insertions(+), 23 deletions(-) diff --git a/mrw/string.hpp b/mrw/string.hpp index 46fdabf..28a7e7d 100644 --- a/mrw/string.hpp +++ b/mrw/string.hpp @@ -9,6 +9,10 @@ @license LGPL, see file COPYING $Log$ + Revision 1.7 2005/11/29 12:38:46 marc + - make it compilable with gcc 4.0.2 and newer doxygen + - added split and join + Revision 1.6 2005/04/07 20:48:42 marc docu: new doxygen, new grouping @@ -35,6 +39,7 @@ #include #include #include +#include namespace mrw { @@ -174,7 +179,6 @@ namespace mrw { */ //@{ - /** @brief convert any value to a std::string @code @@ -182,7 +186,7 @@ namespace mrw { @endcode @param o a value to be converted to std::string - @pre #include + @pre \#include @pre T must support operator<< to a stream */ template std::string string(const T& o) @@ -200,7 +204,7 @@ namespace mrw { @throw mrw::invalid_argument if value can not be created from string @param s the string where a value of type @c T is extracted from - @pre #include + @pre \#include @pre T must support operator>> from a stream @pre operator>> from T must not throw anything else than std::exception @@ -215,6 +219,65 @@ namespace mrw { return o; } + /** @brief join a string from pieces + + @code + std::list l; + l<<"hello"<<"world"; // needs mrw/list.hpp + std::string hello_world(mrw::join(l)); + // hello_world is now "hello world" + @endcode + + @param l the list of strings to join + @param delimiter the delimiter between the joined strings + */ + template class LIST> + std::string join(const LIST& l, + const std::string& delimiter=" ") + throw(std::bad_exception) { + std::string result; + for (typename LIST::const_iterator it(l.begin()); + it!=l.end(); ++it) + result+=(result.size()?delimiter:"")+mrw::string(*it); + return result; + } + + /** @brief split a string into pieces + + @code + std::string hello_world("hello world"); + std::list l(mrw::split(hello_world)); + // l contains now "hello" and "world" + @endcode + + @param text the text that has to be split into tokens + @param greedy + - @c true don't generate empty tokens, if a delimiter is followed + by another delimiter, both are removed + - @c false if several delimiters follow each other in the text, + eat them all and don't produce empty tokens + @param delimiters a list of delimiters, each char in the string is a + delimiter + */ + inline std::list split(const std::string& text, + bool greedy=true, + const std::string& delimiters=" \n\t") + throw(std::bad_exception) { + std::list res; + for (std::string::size_type pos(0); + pos + @pre \#include @pre T must support operator<< to a stream */ template std::string& operator<<(std::string& s, const T& o) @@ -256,7 +319,7 @@ template std::string& operator<<(std::string& s, const T& o) @note when something is extracted from a string, it is removed from the string, that means after every shift the string is shortened by the shifted element - @pre #include + @pre \#include @pre T must support operator>> from a stream @note The signature has changed: @@ -279,7 +342,7 @@ template std::string& operator>>(std::string& s, T& o) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string operator+(const std::string& s, unsigned short o) throw(std::bad_exception) { @@ -295,7 +358,7 @@ inline std::string operator+(const std::string& s, unsigned short o) @param s the string, where @c o is prepended @param o the value to prepend in front of @c s - @pre #include + @pre \#include */ inline std::string operator+(unsigned short o, const std::string& s) throw(std::bad_exception) { @@ -311,7 +374,7 @@ inline std::string operator+(unsigned short o, const std::string& s) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string operator+(const std::string& s, unsigned int o) throw(std::bad_exception) { @@ -327,7 +390,7 @@ inline std::string operator+(const std::string& s, unsigned int o) @param s the string, where @c o is prepended @param o the value to prepend in front of @c s - @pre #include + @pre \#include */ inline std::string operator+(unsigned int o, const std::string& s) throw(std::bad_exception) { @@ -343,7 +406,7 @@ inline std::string operator+(unsigned int o, const std::string& s) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string operator+(const std::string& s, unsigned long o) throw(std::bad_exception) { @@ -359,7 +422,7 @@ inline std::string operator+(const std::string& s, unsigned long o) @param s the string, where @c o is prepended @param o the value to prepend in front of @c s - @pre #include + @pre \#include */ inline std::string operator+(unsigned long o, const std::string& s) throw(std::bad_exception) { @@ -375,7 +438,7 @@ inline std::string operator+(unsigned long o, const std::string& s) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string operator+(const std::string& s, signed short o) throw(std::bad_exception) { @@ -391,7 +454,7 @@ inline std::string operator+(const std::string& s, signed short o) @param s the string, where @c o is prepended @param o the value to prepend in front of @c s - @pre #include + @pre \#include */ inline std::string operator+(signed short o, const std::string& s) throw(std::bad_exception) { @@ -407,7 +470,7 @@ inline std::string operator+(signed short o, const std::string& s) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string operator+(const std::string& s, signed int o) throw(std::bad_exception) { @@ -423,7 +486,7 @@ inline std::string operator+(const std::string& s, signed int o) @param s the string, where @c o is prepended @param o the value to prepend in front of @c s - @pre #include + @pre \#include */ inline std::string operator+(signed int o, const std::string& s) throw(std::bad_exception) { @@ -439,7 +502,7 @@ inline std::string operator+(signed int o, const std::string& s) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string operator+(const std::string& s, signed long o) throw(std::bad_exception) { @@ -455,7 +518,7 @@ inline std::string operator+(const std::string& s, signed long o) @param s the string, where @c o is prepended @param o the value to prepend in front of @c s - @pre #include + @pre \#include */ inline std::string operator+(signed long o, const std::string& s) throw(std::bad_exception) { @@ -472,7 +535,7 @@ inline std::string operator+(signed long o, const std::string& s) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string& operator+=(std::string& s, unsigned short o) throw(std::bad_exception) { @@ -488,7 +551,7 @@ inline std::string& operator+=(std::string& s, unsigned short o) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string& operator+=(std::string& s, unsigned int o) throw(std::bad_exception) { @@ -504,7 +567,7 @@ inline std::string& operator+=(std::string& s, unsigned int o) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string& operator+=(std::string& s, unsigned long o) throw(std::bad_exception) { @@ -520,7 +583,7 @@ inline std::string& operator+=(std::string& s, unsigned long o) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string& operator+=(std::string& s, signed short o) throw(std::bad_exception) { @@ -536,7 +599,7 @@ inline std::string& operator+=(std::string& s, signed short o) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string& operator+=(std::string& s, signed int o) throw(std::bad_exception) { @@ -552,7 +615,7 @@ inline std::string& operator+=(std::string& s, signed int o) @param s the string, where @c o is appended @param o the value to append to @c s - @pre #include + @pre \#include */ inline std::string& operator+=(std::string& s, signed long o) throw(std::bad_exception) {