diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/html | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/html')
-rw-r--r-- | library/cpp/html/escape/escape.cpp | 78 | ||||
-rw-r--r-- | library/cpp/html/escape/escape.h | 6 | ||||
-rw-r--r-- | library/cpp/html/pcdata/pcdata.cpp | 108 | ||||
-rw-r--r-- | library/cpp/html/pcdata/pcdata.h | 8 | ||||
-rw-r--r-- | library/cpp/html/pcdata/pcdata_ut.cpp | 54 |
5 files changed, 127 insertions, 127 deletions
diff --git a/library/cpp/html/escape/escape.cpp b/library/cpp/html/escape/escape.cpp index 5b8ed60f04..4e38112ea1 100644 --- a/library/cpp/html/escape/escape.cpp +++ b/library/cpp/html/escape/escape.cpp @@ -4,63 +4,63 @@ #include <util/generic/strbuf.h> namespace NHtml { - namespace { - struct TReplace { - char Char; - bool ForText; - TStringBuf Entity; - }; + namespace { + struct TReplace { + char Char; + bool ForText; + TStringBuf Entity; + }; TReplace Escapable[] = { {'"', false, TStringBuf(""")}, {'&', true, TStringBuf("&")}, {'<', true, TStringBuf("<")}, {'>', true, TStringBuf(">")}, - }; + }; TString EscapeImpl(const TString& value, bool isText) { - auto ci = value.begin(); - // Looking for escapable characters. - for (; ci != value.end(); ++ci) { - for (size_t i = (isText ? 1 : 0); i < Y_ARRAY_SIZE(Escapable); ++i) { - if (*ci == Escapable[i].Char) { - goto escape; - } - } + auto ci = value.begin(); + // Looking for escapable characters. + for (; ci != value.end(); ++ci) { + for (size_t i = (isText ? 1 : 0); i < Y_ARRAY_SIZE(Escapable); ++i) { + if (*ci == Escapable[i].Char) { + goto escape; + } + } } - // There is no escapable characters, so return original value. - return value; + // There is no escapable characters, so return original value. + return value; - escape: - TString tmp = TString(value.begin(), ci); + escape: + TString tmp = TString(value.begin(), ci); - for (; ci != value.end(); ++ci) { - size_t i = (isText ? 1 : 0); + for (; ci != value.end(); ++ci) { + size_t i = (isText ? 1 : 0); - for (; i < Y_ARRAY_SIZE(Escapable); ++i) { - if (*ci == Escapable[i].Char) { - tmp += Escapable[i].Entity; - break; - } - } - - if (i == Y_ARRAY_SIZE(Escapable)) { - tmp += *ci; - } + for (; i < Y_ARRAY_SIZE(Escapable); ++i) { + if (*ci == Escapable[i].Char) { + tmp += Escapable[i].Entity; + break; + } + } + + if (i == Y_ARRAY_SIZE(Escapable)) { + tmp += *ci; + } } - - return tmp; + + return tmp; } } - TString EscapeAttributeValue(const TString& value) { - return EscapeImpl(value, false); - } + TString EscapeAttributeValue(const TString& value) { + return EscapeImpl(value, false); + } - TString EscapeText(const TString& value) { - return EscapeImpl(value, true); - } + TString EscapeText(const TString& value) { + return EscapeImpl(value, true); + } } diff --git a/library/cpp/html/escape/escape.h b/library/cpp/html/escape/escape.h index 1c45fc5193..e255ec7997 100644 --- a/library/cpp/html/escape/escape.h +++ b/library/cpp/html/escape/escape.h @@ -3,7 +3,7 @@ #include <util/generic/string.h> namespace NHtml { - TString EscapeAttributeValue(const TString& value); - TString EscapeText(const TString& value); + TString EscapeAttributeValue(const TString& value); + TString EscapeText(const TString& value); -} +} diff --git a/library/cpp/html/pcdata/pcdata.cpp b/library/cpp/html/pcdata/pcdata.cpp index 740c240fd2..1f8d2950de 100644 --- a/library/cpp/html/pcdata/pcdata.cpp +++ b/library/cpp/html/pcdata/pcdata.cpp @@ -1,54 +1,54 @@ -#include "pcdata.h" - +#include "pcdata.h" + #include <util/string/strspn.h> -static TCompactStrSpn sspn("\"<>&'"); - +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; - + 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 '\"': + + switch (*s) { + case '\"': strout += TStringBuf("""); ++s; - break; - - case '<': + break; + + case '<': strout += TStringBuf("<"); ++s; - break; - - case '>': + break; + + case '>': strout += TStringBuf(">"); ++s; - break; - - case '\'': + break; + + case '\'': strout += TStringBuf("'"); ++s; - break; - - case '&': - if (qAmp) + break; + + case '&': + if (qAmp) strout += TStringBuf("&"); - else + else strout += TStringBuf("&"); ++s; - break; - } - } + break; + } + } } - + void EncodeHtmlPcdataAppend(const TStringBuf str, TString& strout) { EncodeHtmlPcdataAppendInternal(str, strout, true); } @@ -56,26 +56,26 @@ void EncodeHtmlPcdataAppend(const TStringBuf str, TString& strout) { TString EncodeHtmlPcdata(const TStringBuf str, bool qAmp) { TString strout; EncodeHtmlPcdataAppendInternal(str, strout, qAmp); - return strout; -} - + return strout; +} + TString DecodeHtmlPcdata(const TString& sz) { TString res; const char* codes[] = {""", "<", ">", "'", "'", "&", "'", 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; -} + 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; +} diff --git a/library/cpp/html/pcdata/pcdata.h b/library/cpp/html/pcdata/pcdata.h index 7dd741f53d..f43c3d0c8d 100644 --- a/library/cpp/html/pcdata/pcdata.h +++ b/library/cpp/html/pcdata/pcdata.h @@ -1,10 +1,10 @@ -#pragma once - +#pragma once + #include <util/generic/fwd.h> - + /// Converts a text into HTML-code. Special characters of HTML («<», «>», ...) replaced with entities. TString EncodeHtmlPcdata(const TStringBuf str, bool qAmp = true); void EncodeHtmlPcdataAppend(const TStringBuf str, TString& strout); - + /// Reverse of EncodeHtmlPcdata() TString DecodeHtmlPcdata(const TString& sz); diff --git a/library/cpp/html/pcdata/pcdata_ut.cpp b/library/cpp/html/pcdata/pcdata_ut.cpp index 5833f8bc59..7418ecf11e 100644 --- a/library/cpp/html/pcdata/pcdata_ut.cpp +++ b/library/cpp/html/pcdata/pcdata_ut.cpp @@ -1,36 +1,36 @@ -#include "pcdata.h" - +#include "pcdata.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TPcdata) { Y_UNIT_TEST(TestStress) { - { - ui64 key = 0x000017C0B76C4E87ull; + { + ui64 key = 0x000017C0B76C4E87ull; TString res = EncodeHtmlPcdata(TStringBuf((const char*)&key, sizeof(key))); - } - - for (size_t i = 0; i < 1000; ++i) { + } + + for (size_t i = 0; i < 1000; ++i) { const TString s = NUnitTest::RandomString(i, i); - - UNIT_ASSERT_VALUES_EQUAL(DecodeHtmlPcdata(EncodeHtmlPcdata(s)), s); - } - } - + + UNIT_ASSERT_VALUES_EQUAL(DecodeHtmlPcdata(EncodeHtmlPcdata(s)), s); + } + } + Y_UNIT_TEST(Test1) { const TString tests[] = { - "qw&qw", - "&<", - ">&qw", - "\'&aaa"}; - + "qw&qw", + "&<", + ">&qw", + "\'&aaa"}; + for (auto s : tests) { - UNIT_ASSERT_VALUES_EQUAL(DecodeHtmlPcdata(EncodeHtmlPcdata(s)), s); - } - } - + UNIT_ASSERT_VALUES_EQUAL(DecodeHtmlPcdata(EncodeHtmlPcdata(s)), s); + } + } + Y_UNIT_TEST(Test2) { - UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("&qqq"), "&qqq"); - } + UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("&qqq"), "&qqq"); + } Y_UNIT_TEST(TestEncodeHtmlPcdataAppend) { TString s; @@ -41,8 +41,8 @@ Y_UNIT_TEST_SUITE(TPcdata) { } Y_UNIT_TEST(TestStrangeAmpParameter) { - UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("m&m's", true), "m&m's"); - UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("m&m's"), "m&m's"); //default + UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("m&m's", true), "m&m's"); + UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("m&m's"), "m&m's"); //default UNIT_ASSERT_VALUES_EQUAL(EncodeHtmlPcdata("m&m's", false), "m&m's"); } -} +} |