Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
html_escape.h
1#ifndef SOURCEMETA_CORE_HTML_ESCAPE_H_
2#define SOURCEMETA_CORE_HTML_ESCAPE_H_
3
4#ifndef SOURCEMETA_CORE_HTML_EXPORT
5#include <sourcemeta/core/html_export.h>
6#endif
7
8#include <sourcemeta/core/html_buffer.h>
9#include <sourcemeta/core/preprocessor.h>
10
11#include <array> // std::array
12#include <cassert> // assert
13#include <concepts> // std::same_as
14#include <cstddef> // std::size_t
15#include <cstdint> // std::uint8_t, std::uint64_t
16#include <cstring> // std::memcpy
17#include <string> // std::string
18#include <string_view> // std::string_view
19
20namespace sourcemeta::core {
21
40SOURCEMETA_CORE_HTML_EXPORT
41auto html_escape(std::string &text) -> void;
42
56template <typename Output>
57 requires std::same_as<Output, std::string> || std::same_as<Output, HTMLBuffer>
58inline auto html_escape_append(Output &output, const std::string_view input)
59 -> void {
60 // The bytes that escaping may replace, which are the quotation mark, the
61 // ampersand, the apostrophe, the angle brackets, and the first byte of the
62 // UTF-8 encoding of the no-break space
63 static constexpr std::array<std::uint8_t, 256> SPECIAL_BYTES{{
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10
66 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, // 0x20
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, // 0x30
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x40
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x50
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x60
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x70
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xA0
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xB0
76 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xC0
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xD0
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xE0
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 0xF0
80 }};
81 constexpr std::uint64_t LOW_BITS{0x0101010101010101ULL};
82 constexpr std::uint64_t HIGH_BITS{0x8080808080808080ULL};
83
84 const auto size{input.size()};
85 // Whatever stays as it is goes out in runs, so that text with nothing to
86 // escape takes a single append
87 std::size_t run_start{0};
88 std::size_t position{0};
89 while (position < size) {
90 auto end{size};
91 if (size - position >= 8) {
92 std::uint64_t word{0};
93 std::memcpy(&word, input.data() + position, 8);
94 // A byte of a word equals another when subtracting from their difference
95 // borrows, which also flags the byte right after a genuine match, and
96 // that is harmless when only asking whether the word has a match at all.
97 // Setting the lowest bit of every byte makes the ampersand match the
98 // apostrophe, and setting the second lowest bit makes the less-than sign
99 // match the greater-than sign, so four comparisons cover the six bytes
100 const auto quotations{word ^ (LOW_BITS * '"')};
101 const auto apostrophes{(word | LOW_BITS) ^ (LOW_BITS * '\'')};
102 const auto angles{(word | (LOW_BITS * 2U)) ^ (LOW_BITS * '>')};
103 const auto leads{word ^ (LOW_BITS * 0xC2)};
104 const auto matches{((quotations - LOW_BITS) & ~quotations) |
105 ((apostrophes - LOW_BITS) & ~apostrophes) |
106 ((angles - LOW_BITS) & ~angles) |
107 ((leads - LOW_BITS) & ~leads)};
108 if ((matches & HIGH_BITS) == 0) {
109 position += 8;
110 continue;
111 }
112
113 end = position + 8;
114 }
115
116 while (position < end) {
117 if (SPECIAL_BYTES[static_cast<unsigned char>(input[position])] == 0) {
118 position += 1;
119 continue;
120 }
121
122 std::string_view replacement;
123 std::size_t consumed{1};
124 switch (input[position]) {
125 case '&':
126 replacement = "&amp;";
127 break;
128 case '<':
129 replacement = "&lt;";
130 break;
131 case '>':
132 replacement = "&gt;";
133 break;
134 case '"':
135 replacement = "&quot;";
136 break;
137 case '\'':
138 replacement = "&#39;";
139 break;
140 default:
141 // The no-break space is replaced by its named entity (HTML Living
142 // Standard "escaping a string" step 2). The table above admits only
143 // its lead byte here, the five characters above having their own arm
144 assert(static_cast<unsigned char>(input[position]) == 0xC2);
145 if (position + 1 < size &&
146 static_cast<unsigned char>(input[position + 1]) == 0xA0) {
147 replacement = "&nbsp;";
148 consumed = 2;
149 }
150
151 break;
152 }
153
154 if (replacement.empty()) {
155 position += 1;
156 continue;
157 }
158
159 output.append(input.substr(run_start, position - run_start));
160 output.append(replacement);
161 position += consumed;
162 run_start = position;
163 }
164 }
165
166 output.append(input.substr(run_start));
167}
168
169} // namespace sourcemeta::core
170
171#endif
SOURCEMETA_CORE_HTML_EXPORT auto html_escape(std::string &text) -> void
auto html_escape_append(Output &output, const std::string_view input) -> void
Definition html_escape.h:58
SOURCEMETA_CORE_REGEX_EXPORT auto matches(const Regex &regex, const std::string_view value) -> bool