New class Str (string building convenience) and appendEscaped.
authorMichael Andres <ma@suse.de>
Thu, 3 Sep 2009 11:30:24 +0000 (13:30 +0200)
committerMichael Andres <ma@suse.de>
Thu, 3 Sep 2009 11:38:38 +0000 (13:38 +0200)
zypp/base/String.cc
zypp/base/String.h

index 232c345..a101c5b 100644 (file)
@@ -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 )
         {
index 1c306a5..746ec8a 100644 (file)
@@ -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()