- make it compilable with gcc 4.0.2 and newer doxygen
- added split and join
This commit is contained in:
109
mrw/string.hpp
109
mrw/string.hpp
@@ -9,6 +9,10 @@
|
|||||||
@license LGPL, see file <a href="license.html">COPYING</a>
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
||||||
|
|
||||||
$Log$
|
$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
|
Revision 1.6 2005/04/07 20:48:42 marc
|
||||||
docu: new doxygen, new grouping
|
docu: new doxygen, new grouping
|
||||||
|
|
||||||
@@ -35,6 +39,7 @@
|
|||||||
#include <mrw/exception.hpp>
|
#include <mrw/exception.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
namespace mrw {
|
namespace mrw {
|
||||||
|
|
||||||
@@ -174,7 +179,6 @@ namespace mrw {
|
|||||||
*/
|
*/
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
|
|
||||||
/** @brief convert any value to a std::string
|
/** @brief convert any value to a std::string
|
||||||
|
|
||||||
@code
|
@code
|
||||||
@@ -182,7 +186,7 @@ namespace mrw {
|
|||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
@param o a value to be converted to std::string
|
@param o a value to be converted to std::string
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
@pre T must support operator<< to a stream
|
@pre T must support operator<< to a stream
|
||||||
*/
|
*/
|
||||||
template <typename T> std::string string(const T& o)
|
template <typename T> std::string string(const T& o)
|
||||||
@@ -200,7 +204,7 @@ namespace mrw {
|
|||||||
|
|
||||||
@throw mrw::invalid_argument if value can not be created from string
|
@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
|
@param s the string where a value of type @c T is extracted from
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
@pre T must support operator>> from a stream
|
@pre T must support operator>> from a stream
|
||||||
@pre operator>> from T must not throw anything else than std::exception
|
@pre operator>> from T must not throw anything else than std::exception
|
||||||
|
|
||||||
@@ -215,6 +219,65 @@ namespace mrw {
|
|||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @brief join a string from pieces
|
||||||
|
|
||||||
|
@code
|
||||||
|
std::list<std::string> 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<template<class STRING> class LIST>
|
||||||
|
std::string join(const LIST<std::string>& l,
|
||||||
|
const std::string& delimiter=" ")
|
||||||
|
throw(std::bad_exception) {
|
||||||
|
std::string result;
|
||||||
|
for (typename LIST<std::string>::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<std::string> 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<std::string> split(const std::string& text,
|
||||||
|
bool greedy=true,
|
||||||
|
const std::string& delimiters=" \n\t")
|
||||||
|
throw(std::bad_exception) {
|
||||||
|
std::list<std::string> res;
|
||||||
|
for (std::string::size_type pos(0);
|
||||||
|
pos<text.size(); ++pos) {
|
||||||
|
std::string::size_type next(text.find_first_not_of(delimiters, pos));
|
||||||
|
if (next!=pos && !greedy) {
|
||||||
|
res.push_back("");
|
||||||
|
} else {
|
||||||
|
next = std::min(text.find_first_of(delimiters, pos), text.size());
|
||||||
|
res.push_back(text.substr(pos, next-pos));
|
||||||
|
pos = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -232,7 +295,7 @@ namespace mrw {
|
|||||||
|
|
||||||
@param s the string, where o is appended
|
@param s the string, where o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
@pre T must support operator<< to a stream
|
@pre T must support operator<< to a stream
|
||||||
*/
|
*/
|
||||||
template <typename T> std::string& operator<<(std::string& s, const T& o)
|
template <typename T> std::string& operator<<(std::string& s, const T& o)
|
||||||
@@ -256,7 +319,7 @@ template <typename T> std::string& operator<<(std::string& s, const T& o)
|
|||||||
@note when something is extracted from a string, it is removed
|
@note when something is extracted from a string, it is removed
|
||||||
from the string, that means after every shift the string is
|
from the string, that means after every shift the string is
|
||||||
shortened by the shifted element
|
shortened by the shifted element
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
@pre T must support operator>> from a stream
|
@pre T must support operator>> from a stream
|
||||||
|
|
||||||
@note The signature has changed:
|
@note The signature has changed:
|
||||||
@@ -279,7 +342,7 @@ template <typename T> std::string& operator>>(std::string& s, T& o)
|
|||||||
|
|
||||||
@param s the string, where @c o is appended
|
@param s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(const std::string& s, unsigned short o)
|
inline std::string operator+(const std::string& s, unsigned short o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is prepended
|
||||||
@param o the value to prepend in front of @c s
|
@param o the value to prepend in front of @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(unsigned short o, const std::string& s)
|
inline std::string operator+(unsigned short o, const std::string& s)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(const std::string& s, unsigned int o)
|
inline std::string operator+(const std::string& s, unsigned int o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is prepended
|
||||||
@param o the value to prepend in front of @c s
|
@param o the value to prepend in front of @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(unsigned int o, const std::string& s)
|
inline std::string operator+(unsigned int o, const std::string& s)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(const std::string& s, unsigned long o)
|
inline std::string operator+(const std::string& s, unsigned long o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is prepended
|
||||||
@param o the value to prepend in front of @c s
|
@param o the value to prepend in front of @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(unsigned long o, const std::string& s)
|
inline std::string operator+(unsigned long o, const std::string& s)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(const std::string& s, signed short o)
|
inline std::string operator+(const std::string& s, signed short o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is prepended
|
||||||
@param o the value to prepend in front of @c s
|
@param o the value to prepend in front of @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(signed short o, const std::string& s)
|
inline std::string operator+(signed short o, const std::string& s)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(const std::string& s, signed int o)
|
inline std::string operator+(const std::string& s, signed int o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is prepended
|
||||||
@param o the value to prepend in front of @c s
|
@param o the value to prepend in front of @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(signed int o, const std::string& s)
|
inline std::string operator+(signed int o, const std::string& s)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(const std::string& s, signed long o)
|
inline std::string operator+(const std::string& s, signed long o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is prepended
|
||||||
@param o the value to prepend in front of @c s
|
@param o the value to prepend in front of @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string operator+(signed long o, const std::string& s)
|
inline std::string operator+(signed long o, const std::string& s)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string& operator+=(std::string& s, unsigned short o)
|
inline std::string& operator+=(std::string& s, unsigned short o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string& operator+=(std::string& s, unsigned int o)
|
inline std::string& operator+=(std::string& s, unsigned int o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string& operator+=(std::string& s, unsigned long o)
|
inline std::string& operator+=(std::string& s, unsigned long o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string& operator+=(std::string& s, signed short o)
|
inline std::string& operator+=(std::string& s, signed short o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string& operator+=(std::string& s, signed int o)
|
inline std::string& operator+=(std::string& s, signed int o)
|
||||||
throw(std::bad_exception) {
|
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 s the string, where @c o is appended
|
||||||
@param o the value to append to @c s
|
@param o the value to append to @c s
|
||||||
@pre #include <mrw/string.hpp>
|
@pre \#include <mrw/string.hpp>
|
||||||
*/
|
*/
|
||||||
inline std::string& operator+=(std::string& s, signed long o)
|
inline std::string& operator+=(std::string& s, signed long o)
|
||||||
throw(std::bad_exception) {
|
throw(std::bad_exception) {
|
||||||
|
Reference in New Issue
Block a user