To HTML encode a string is to convert the " to " and < to < and > to > and & to & and you get the idea :) Essentially it's just a simple find and replace of those few things.
To HTML encode a string is to convert the " to " and < to < and > to > and & to & and you get the idea :) Essentially it's just a simple find and replace of those few things.
yeah, i know, was just looking for a function that was already written and handled all of them rather than doing them each individually. trying to do it the most laz.. I mean efficient way and use an existing function rather than write my own
that's what I am looking for, but a non-.NET version
I'm sure you could find something with google, but it's not hard to write your own either.
#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;
}
Just add to the array when you want to handle another encoding, and be careful about encodings that are order sensitive. For example, the & encoding has to be done first because the others use & in the result. ;)
I'm sure you could find something with google, but it's not hard to write your own either.
#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;
}
Just add to the array when you want to handle another encoding, and be careful about encodings that are order sensitive. For example, the & encoding has to be done first because the others use & in the result. ;)
To HTML encode a string is to convert the " to " and < to < and > to > and & to & and you get the idea :) Essentially it's just a simple find and replace of those few things.
hmm, except if the string represents PCDATA in which case it should be surrounded by <pre></pre> tags.