Thrill  0.1
escape_html.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/escape_html.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
12 
13 #include <cstring>
14 
15 namespace tlx {
16 
18  std::string os;
19  os.reserve(str.size() + str.size() / 16);
20 
21  for (std::string::const_iterator si = str.begin(); si != str.end(); ++si)
22  {
23  if (*si == '&') os += "&amp;";
24  else if (*si == '<') os += "&lt;";
25  else if (*si == '>') os += "&gt;";
26  else if (*si == '"') os += "&quot;";
27  else os += *si;
28  }
29 
30  return os;
31 }
32 
33 std::string escape_html(const char* str) {
34  size_t slen = strlen(str);
35  std::string os;
36  os.reserve(slen + slen / 16);
37 
38  for (const char* si = str; *si != 0; ++si)
39  {
40  if (*si == '&') os += "&amp;";
41  else if (*si == '<') os += "&lt;";
42  else if (*si == '>') os += "&gt;";
43  else if (*si == '"') os += "&quot;";
44  else os += *si;
45  }
46 
47  return os;
48 }
49 
50 } // namespace tlx
51 
52 /******************************************************************************/
std::string escape_html(const std::string &str)
Escape characters for inclusion in HTML documents: replaces the characters <, >, & and " with HTML en...
Definition: escape_html.cpp:17
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220