ref: refs/heads/master
commit fc203c4d50f96f4ff3f197ccd72e508c6e7a4b06
Author: Michael Andres
Date: Thu Sep 3 13:30:24 2009 +0200
New class Str (string building convenience) and appendEscaped.
---
zypp/base/String.cc | 4 ++--
zypp/base/String.h | 37 ++++++++++++++++++++++++++++++++++++-
2 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/zypp/base/String.cc b/zypp/base/String.cc
index 232c345..a101c5b 100644
--- a/zypp/base/String.cc
+++ b/zypp/base/String.cc
@@ -334,10 +334,10 @@ namespace zypp
}
- std::string escape( const std::string & str_r, const char sep_r )
+ std::string escape( const C_Str & str_r, const char sep_r )
{
std::vector<char> buf;
- for_( s, str_r.begin(), str_r.end() )
+ for_( s, str_r.c_str(), s+str_r.size() )
{
switch ( *s )
{
diff --git a/zypp/base/String.h b/zypp/base/String.h
index 1c306a5..746ec8a 100644
--- a/zypp/base/String.h
+++ b/zypp/base/String.h
@@ -17,6 +17,7 @@
#include <iosfwd>
#include <vector>
#include <string>
+#include <sstream>
#include "zypp/base/Easy.h"
#include "zypp/base/PtrTypes.h"
@@ -156,6 +157,29 @@ namespace zypp
};
///////////////////////////////////////////////////////////////////
+ /** Convenient building of std::string via std::ostream::operator<<.
+ * Basically this is an \ref ostringstream which is autocenvertible
+ * into a \ref string.
+ * \code
+ * void fnc( const std::string & txt_r );
+ * fnc( str::Str() << "Hello " << 13 );
+ *
+ * std::string txt( str::Str() << 45 );
+ * \endcode
+ */
+ struct Str
+ {
+ template<class _Tp>
+ Str & operator<<( const _Tp & val )
+ { _str << val; return *this; }
+
+ operator std::string() const
+ { return _str.str(); }
+
+ std::ostringstream _str;
+ };
+
+ ///////////////////////////////////////////////////////////////////
/** \name String representation of number.
*
* Optional second argument sets the minimal string width (' ' padded).
@@ -645,7 +669,18 @@ namespace zypp
*
* \todo shoud not be documented in doxy-group 'Join'
*/
- std::string escape(const std::string & str_r, const char c = ' ' );
+ std::string escape( const C_Str & str_r, const char c = ' ' );
+
+ /** Escape \a next_r and append it to \a str_r using separator \a sep_r. */
+ inline void appendEscaped( std::string & str_r, const C_Str & next_r, const char sep_r = ' ' )
+ {
+ if ( ! str_r.empty() )
+ str_r += sep_r;
+ if ( next_r.empty() )
+ str_r += "\"\"";
+ else
+ str_r += escape( next_r, sep_r );
+ }
//! \todo unsecape()
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org