aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/string_escape.cpp
blob: d1b38f84c3e2fb38dca28c7f7ff6be2395e0db09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "string_escape.h"
#include <vespa/vespalib/stllike/asciistream.h>
#include <vector>
#include <ostream>

namespace vespalib {

namespace {

std::vector<bool> precompute_escaped_xml_chars() {
    std::vector<bool> vec(256, false);
    for (uint32_t i=0; i<32; ++i) {
        vec[i] = true;
    }
    vec['\n'] = false;
    vec['<'] = true;
    vec['>'] = true;
    vec['&'] = true;
    return vec;
}

std::vector<bool> escaped_xml_chars = precompute_escaped_xml_chars();

template <typename StreamT>
void do_write_xml_content_escaped(StreamT& out, vespalib::stringref str) {
    for (const char s : str) {
        if (escaped_xml_chars[static_cast<uint8_t>(s)]) {
            if      (s == '<') out << "&lt;";
            else if (s == '>') out << "&gt;";
            else if (s == '&') out << "&amp;";
            else {
                out << "&#" << static_cast<int>(s) << ";";
            }
        } else {
            out << s;
        }
    }
}

}

vespalib::string xml_attribute_escaped(vespalib::stringref str) {
    vespalib::asciistream ost;
    for (const char s : str) {
        if (s == '"' || s == '\'' || s == '\n'
            || escaped_xml_chars[static_cast<uint8_t>(s)])
        {
            if      (s == '<')  ost << "&lt;";
            else if (s == '>')  ost << "&gt;";
            else if (s == '&')  ost << "&amp;";
            else if (s == '"')  ost << "&quot;";
            else if (s == '\'') ost << "&#39;";
            else {
                ost << "&#" << static_cast<int>(s) << ";";
            }
        } else {
            ost << s;
        }
    }
    return ost.str();
}

vespalib::string xml_content_escaped(vespalib::stringref str) {
    vespalib::asciistream out;
    do_write_xml_content_escaped(out, str);
    return out.str();
}

void write_xml_content_escaped(vespalib::asciistream& out, vespalib::stringref str) {
    do_write_xml_content_escaped(out, str);
}

void write_xml_content_escaped(std::ostream& out, vespalib::stringref str) {
    do_write_xml_content_escaped(out, str);
}

}