- str::escape(string, char) added
authorJan Kupec <jkupec@suse.cz>
Wed, 24 Sep 2008 17:11:06 +0000 (17:11 +0000)
committerJan Kupec <jkupec@suse.cz>
Wed, 24 Sep 2008 17:11:06 +0000 (17:11 +0000)
- escape aliases in the history file (the rest of the values should not
  contain '|')

tests/zypp/base/String_test.cc
zypp/HistoryLog.cc
zypp/base/String.cc
zypp/base/String.h

index e08a9afc736c23ff1388d30def6d38533fca85a6..f479e22ef6220cef538e67422b09f8b7f27347ea 100644 (file)
@@ -85,5 +85,12 @@ BOOST_AUTO_TEST_CASE(testsplitEscaped)
    v.clear();
    str::splitEscaped( s, std::back_inserter(v), "o" );
    BOOST_CHECK_EQUAL( s, str::joinEscaped( v.begin(), v.end(), 'o' ) );
+}
+
+BOOST_AUTO_TEST_CASE(test_escape)
+{
+  string badass = "bad|ass\\|worse";
+  string escaped = str::escape(badass, '|');
 
+  BOOST_CHECK_EQUAL( escaped, "bad\\|ass\\\\\\|worse" );
 }
index 4b0c59fb887078c86b4b3db2b2b74a68b5348606..447569cb8a7e211a94ba893df2dcac1724240d9c 100644 (file)
@@ -303,7 +303,7 @@ namespace zypp
     _log
       << timestamp()                                   // 1 timestamp
       << _sep << HistoryActionID::REPO_REMOVE.asString(true) // 2 action 
-      << _sep << repo.alias()                          // 3 alias
+      << _sep << str::escape(repo.alias(), _sep)       // 3 alias
       << endl;
   }
 
@@ -316,8 +316,8 @@ namespace zypp
       _log
         << timestamp()                                    // 1 timestamp
         << _sep << HistoryActionID::REPO_CHANGE_ALIAS.asString(true) // 2 action
-        << _sep << oldrepo.alias()                        // 3 old alias
-        << _sep << newrepo.alias();                       // 4 new alias
+        << _sep << str::escape(oldrepo.alias(), _sep)     // 3 old alias
+        << _sep << str::escape(newrepo.alias(), _sep);    // 4 new alias
     }
     
     if (*oldrepo.baseUrlsBegin() != *newrepo.baseUrlsBegin())
@@ -325,8 +325,8 @@ namespace zypp
       _log
         << timestamp()                                    // 1 timestamp
         << _sep << HistoryActionID::REPO_CHANGE_URL.asString(true) // 2 action
-        << _sep << oldrepo.alias()                        // 3 old url
-        << _sep << newrepo.alias();                       // 4 new url
+        << _sep << str::escape(oldrepo.alias(), _sep)              // 3 old url
+        << _sep << *newrepo.baseUrlsBegin();                       // 4 new url
     }
   }
 
index f2af5d5d5910c8360742fa31b835b3ab1235dcef..459e61f3f233617d55fc461d46bd84489ea1f974 100644 (file)
@@ -260,6 +260,30 @@ namespace zypp
       return str;
     }
 
+
+    std::string escape( const std::string & str_r, const char sep_r )
+    {
+      std::vector<char> buf;
+      for_( s, str_r.begin(), str_r.end() )
+      {
+        switch ( *s )
+        {
+        case '"':
+        case '\'':
+        case '\\':
+          buf.push_back( '\\' );
+          buf.push_back( *s );
+          break;
+        default:
+          if ( *s == sep_r )
+            buf.push_back( '\\' );
+          buf.push_back( *s );
+        }
+      }
+      return std::string( buf.begin(), buf.end() );
+    }
+
+
     /******************************************************************
     **
     **
index 68a2cfb446461848c43ee3d258ddaabd65ec0371..30ab713829ee24a7caf470217f137bc69581aa9f 100644 (file)
@@ -566,6 +566,17 @@ namespace zypp
         return std::string( buf.begin(), buf.end() );
       }
 
+
+      /**
+       * Escape desired character \a c using a backslash.
+       * 
+       * For use when printing \a c separated values, and where
+       * \ref joinEscaped() is too heavy.
+       */
+      std::string escape(const std::string & str_r, const char c = ' ' );
+
+      //! \todo unsecape()
+
     //@}
     ///////////////////////////////////////////////////////////////////