summaryrefslogtreecommitdiffstats
path: root/library/cpp/html/pcdata/pcdata.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/html/pcdata/pcdata.cpp
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/html/pcdata/pcdata.cpp')
-rw-r--r--library/cpp/html/pcdata/pcdata.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/library/cpp/html/pcdata/pcdata.cpp b/library/cpp/html/pcdata/pcdata.cpp
new file mode 100644
index 00000000000..740c240fd23
--- /dev/null
+++ b/library/cpp/html/pcdata/pcdata.cpp
@@ -0,0 +1,81 @@
+#include "pcdata.h"
+
+#include <util/string/strspn.h>
+
+static TCompactStrSpn sspn("\"<>&'");
+
+static void EncodeHtmlPcdataAppendInternal(const TStringBuf str, TString& strout, bool qAmp) {
+ const char* s = str.data();
+ const char* e = s + str.length();
+
+ for (;;) {
+ const char* next = sspn.FindFirstOf(s, e);
+
+ strout.AppendNoAlias(s, next - s);
+ s = next;
+
+ if (s == e)
+ break;
+
+ switch (*s) {
+ case '\"':
+ strout += TStringBuf("&quot;");
+ ++s;
+ break;
+
+ case '<':
+ strout += TStringBuf("&lt;");
+ ++s;
+ break;
+
+ case '>':
+ strout += TStringBuf("&gt;");
+ ++s;
+ break;
+
+ case '\'':
+ strout += TStringBuf("&#39;");
+ ++s;
+ break;
+
+ case '&':
+ if (qAmp)
+ strout += TStringBuf("&amp;");
+ else
+ strout += TStringBuf("&");
+ ++s;
+ break;
+ }
+ }
+}
+
+void EncodeHtmlPcdataAppend(const TStringBuf str, TString& strout) {
+ EncodeHtmlPcdataAppendInternal(str, strout, true);
+}
+
+TString EncodeHtmlPcdata(const TStringBuf str, bool qAmp) {
+ TString strout;
+ EncodeHtmlPcdataAppendInternal(str, strout, qAmp);
+ return strout;
+}
+
+TString DecodeHtmlPcdata(const TString& sz) {
+ TString res;
+ const char* codes[] = {"&quot;", "&lt;", "&gt;", "&#39;", "&#039;", "&amp;", "&apos;", nullptr};
+ const char chars[] = {'\"', '<', '>', '\'', '\'', '&', '\''};
+ for (size_t i = 0; i < sz.length(); ++i) {
+ char c = sz[i];
+ if (c == '&') {
+ for (const char** p = codes; *p; ++p) {
+ size_t len = strlen(*p);
+ if (strncmp(sz.c_str() + i, *p, len) == 0) {
+ i += len - 1;
+ c = chars[p - codes];
+ break;
+ }
+ }
+ }
+ res += c;
+ }
+ return res;
+}