I search the web alot and didn't find c++ function that replace xml Special Character with their escape sequence? Is there something like this?
I know about the following:
Special Character Escape Sequence Purpose
& & Ampersand sign
' ' Single quote
" " Double quote
> > Greater than
< < Less than
is there more? what about writing hexadecimal value like 0×00, Is this also a problem?
Writing your own is easy enough, but scanning the string multiple times to search/replace individual characters can be inefficient:
std::string escape(const std::string& src) {
std::stringstream dst;
for (char ch : src) {
switch (ch) {
case '&': dst << "&"; break;
case '\'': dst << "'"; break;
case '"': dst << """; break;
case '<': dst << "<"; break;
case '>': dst << ">"; break;
default: dst << ch; break;
}
}
return dst.str();
}
Note: I used a C++11 range-based for loop for convenience, but you can easily do the same thing with an iterator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With