21template<
typename Output>
22inline void XMLEscape(std::string_view s, Output output)
24 auto normal = [&](
auto f,
auto l) {
26 return std::string_view(&*f, l - f);
29 auto chunk = s.begin();
34 if (
auto uc =
static_cast<unsigned char>(c); uc < 32) {
35 output(normal(chunk, it));
36 std::array<char, 6> buf = {
'&',
'#',
'x',
'0',
'0',
';'};
37 auto hex = [](
unsigned x) {
return (x < 10) ? char(x +
'0') : char(x - 10 +
'a'); };
38 buf[3] = hex(uc / 16);
39 buf[4] = hex(uc % 16);
40 output(std::string_view(buf.data(),
sizeof(buf)));
42 }
else if (c ==
'<') {
43 output(normal(chunk, it));
46 }
else if (c ==
'>') {
47 output(normal(chunk, it));
50 }
else if (c ==
'&') {
51 output(normal(chunk, it));
54 }
else if (c ==
'"') {
55 output(normal(chunk, it));
58 }
else if (c ==
'\'') {
59 output(normal(chunk, it));
67 output(normal(chunk, last));
74 XMLEscape(s, [&](std::string_view chunk) { result += chunk; });
void XMLEscape(std::string_view s, Output output)