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" );
}
_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;
}
_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())
_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
}
}
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() );
+ }
+
+
/******************************************************************
**
**
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()
+
//@}
///////////////////////////////////////////////////////////////////