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,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
81 constexpr std::uint64_t LOW_BITS{0x0101010101010101ULL};
82 constexpr std::uint64_t HIGH_BITS{0x8080808080808080ULL};
84 const auto size{input.size()};
87 std::size_t run_start{0};
88 std::size_t position{0};
89 while (position < size) {
91 if (size - position >= 8) {
92 std::uint64_t word{0};
93 std::memcpy(&word, input.data() + position, 8);
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) {
116 while (position < end) {
117 if (SPECIAL_BYTES[
static_cast<unsigned char>(input[position])] == 0) {
122 std::string_view replacement;
123 std::size_t consumed{1};
124 switch (input[position]) {
126 replacement =
"&";
129 replacement =
"<";
132 replacement =
">";
135 replacement =
""";
138 replacement =
"'";
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 =
" ";
154 if (replacement.empty()) {
159 output.append(input.substr(run_start, position - run_start));
160 output.append(replacement);
161 position += consumed;
162 run_start = position;
166 output.append(input.substr(run_start));