3,055
社区成员




#include <algorithm>
#include <iostream>
#include <string>
#define array_length(array) (sizeof (array) / sizeof (array)[0])
namespace Raye {
using namespace std;
struct HTMLReplace {
string match;
string replace;
} codes[] = {
{"&", "&"},
{"<", "<"},
{">", ">"}
};
string HTMLEncode( const string& s )
{
string rs = s;
// Replace each matching token in turn
for ( size_t i = 0; i < array_length( codes ); i++ ) {
// Find the first match
const string& match = codes[i].match;
const string& repl = codes[i].replace;
string::size_type start = rs.find_first_of( match );
// Replace all matches
while ( start != string::npos ) {
rs.replace( start, match.size(), repl );
// Be sure to jump forward by the replacement length
start = rs.find_first_of( match, start + repl.size() );
}
}
return rs;
}
}
int main()
{
using namespace std;
cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n';
return 0;
}