MCPcopy Create free account
hub / github.com/Tracktion/choc / escapeHTMLString

Method escapeHTMLString

choc/text/choc_HTML.h:229–271  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

227}
228
229inline std::string HTMLElement::escapeHTMLString (std::string_view text, bool escapeNewLines)
230{
231 std::string result;
232 result.reserve (text.length());
233
234 for (auto character : text)
235 {
236 auto unicodeChar = static_cast<uint32_t> (character);
237
238 auto isCharLegal = +[] (uint32_t c) -> bool
239 {
240 return (c >= 'a' && c <= 'z')
241 || (c >= 'A' && c <= 'Z')
242 || (c >= '0' && c <= '9')
243 || (c < 127 && std::string_view (" .,;:-()_+=?!$#@[]/|*%~{}\\").find ((char) c) != std::string_view::npos);
244 };
245
246 if (isCharLegal (unicodeChar))
247 {
248 result += character;
249 }
250 else
251 {
252 switch (unicodeChar)
253 {
254 case '<': result += "&lt;"; break;
255 case '>': result += "&gt;"; break;
256 case '&': result += "&amp;"; break;
257 case '"': result += "&quot;"; break;
258
259 default:
260 if (! escapeNewLines && (unicodeChar == '\n' || unicodeChar == '\r'))
261 result += character;
262 else
263 result += "&#" + std::to_string (unicodeChar) + ';';
264
265 break;
266 }
267 }
268 }
269
270 return result;
271}
272
273} // namespace choc::html
274

Callers

nothing calls this directly

Calls 3

findMethod · 0.80
reserveMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected