diff options
author | ijon <ijon@yandex-team.ru> | 2022-02-10 16:49:57 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:57 +0300 |
commit | fcd93d3533aed781986b121b1362ca188bd96367 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/string_utils | |
parent | a62b5e9ea9837fdaa7a5e327b7f98b41e4d18cf6 (diff) | |
download | ydb-fcd93d3533aed781986b121b1362ca188bd96367.tar.gz |
Restoring authorship annotation for <ijon@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/string_utils')
-rw-r--r-- | library/cpp/string_utils/quote/quote.cpp | 54 | ||||
-rw-r--r-- | library/cpp/string_utils/quote/quote_ut.cpp | 54 |
2 files changed, 54 insertions, 54 deletions
diff --git a/library/cpp/string_utils/quote/quote.cpp b/library/cpp/string_utils/quote/quote.cpp index 6843bc5309..e523350b80 100644 --- a/library/cpp/string_utils/quote/quote.cpp +++ b/library/cpp/string_utils/quote/quote.cpp @@ -106,7 +106,7 @@ static const bool chars_to_url_escape[256] = { template <class It1, class It2, class It3> static inline It1 Escape(It1 to, It2 from, It3 end, const bool* escape_map = chars_to_url_escape) { while (from != end) { - if (escape_map[(unsigned char)*from]) { + if (escape_map[(unsigned char)*from]) { *to++ = '%'; *to++ = d2x((unsigned char)*from >> 4); *to++ = d2x((unsigned char)*from & 0xF); @@ -144,13 +144,13 @@ static inline It1 Unescape(It1 to, It2 from, It3 end, FromHex fromHex) { return to; } -// CGIEscape returns pointer to the end of the result string -// so as it could be possible to populate single long buffer -// with several calls to CGIEscape in a row. +// CGIEscape returns pointer to the end of the result string +// so as it could be possible to populate single long buffer +// with several calls to CGIEscape in a row. char* CGIEscape(char* to, const char* from) { return Escape(to, FixZero(from), TCStringEndIterator()); -} - +} + char* CGIEscape(char* to, const char* from, size_t len) { return Escape(to, from, from + len); } @@ -176,29 +176,29 @@ TString& AppendCgiEscaped(const TStringBuf value, TString& to) { return to; } -// More general version of CGIEscape. The optional safe parameter specifies -// additional characters that should not be quoted — its default value is '/'. +// More general version of CGIEscape. The optional safe parameter specifies +// additional characters that should not be quoted — its default value is '/'. -// Also returns pointer to the end of result string. +// Also returns pointer to the end of result string. template <class It1, class It2, class It3> static inline It1 Quote(It1 to, It2 from, It3 end, const char* safe) { - bool escape_map[256]; - memcpy(escape_map, chars_to_url_escape, 256); - // RFC 3986 Uniform Resource Identifiers (URI): Generic Syntax - // lists following reserved characters: - const char* reserved = ":/?#[]@!$&\'()*+,;="; - for (const char* p = reserved; *p; ++p) { - escape_map[(unsigned char)*p] = 1; - } - // characters we think are safe at the moment - for (const char* p = safe; *p; ++p) { - escape_map[(unsigned char)*p] = 0; - } - + bool escape_map[256]; + memcpy(escape_map, chars_to_url_escape, 256); + // RFC 3986 Uniform Resource Identifiers (URI): Generic Syntax + // lists following reserved characters: + const char* reserved = ":/?#[]@!$&\'()*+,;="; + for (const char* p = reserved; *p; ++p) { + escape_map[(unsigned char)*p] = 1; + } + // characters we think are safe at the moment + for (const char* p = safe; *p; ++p) { + escape_map[(unsigned char)*p] = 0; + } + return Escape(to, from, end, escape_map); -} - +} + char* Quote(char* to, const char* from, const char* safe) { return Quote(to, FixZero(from), TCStringEndIterator(), safe); } @@ -209,11 +209,11 @@ char* Quote(char* to, const TStringBuf s, const char* safe) { void Quote(TString& url, const char* safe) { TTempBuf tempBuf(CgiEscapeBufLen(url.size())); - char* to = tempBuf.Data(); + char* to = tempBuf.Data(); url.AssignNoAlias(to, Quote(to, url, safe)); -} - +} + char* CGIUnescape(char* to, const char* from) { return Unescape(to, FixZero(from), TCStringEndIterator(), TFromHexZeroTerm()); } diff --git a/library/cpp/string_utils/quote/quote_ut.cpp b/library/cpp/string_utils/quote/quote_ut.cpp index 048e6b18a1..6c552b279e 100644 --- a/library/cpp/string_utils/quote/quote_ut.cpp +++ b/library/cpp/string_utils/quote/quote_ut.cpp @@ -4,11 +4,11 @@ Y_UNIT_TEST_SUITE(TCGIEscapeTest) { Y_UNIT_TEST(ReturnsEndOfTo) { - char r[10]; - const char* returned = CGIEscape(r, "123"); - UNIT_ASSERT_VALUES_EQUAL(r + strlen("123"), returned); - UNIT_ASSERT_VALUES_EQUAL('\0', *returned); - } + char r[10]; + const char* returned = CGIEscape(r, "123"); + UNIT_ASSERT_VALUES_EQUAL(r + strlen("123"), returned); + UNIT_ASSERT_VALUES_EQUAL('\0', *returned); + } Y_UNIT_TEST(NotZeroTerminated) { char r[] = {'1', '2', '3', '4'}; @@ -45,8 +45,8 @@ Y_UNIT_TEST_SUITE(TCGIEscapeTest) { TString("¶m=!@%23$%25^%26*%28%29%7B%7D%5B%5D%22+¶m_param=!@%23$%25^%26*%28%29%7B%7D%5B%5D%22+")); } -} - +} + Y_UNIT_TEST_SUITE(TCGIUnescapeTest) { Y_UNIT_TEST(StringBuf) { char tmp[100]; @@ -287,33 +287,33 @@ Y_UNIT_TEST_SUITE(TUrlUnescapeTest) { Y_UNIT_TEST_SUITE(TQuoteTest) { Y_UNIT_TEST(ReturnsEndOfTo) { - char r[10]; - const char* returned = Quote(r, "123"); - UNIT_ASSERT_VALUES_EQUAL(r + strlen("123"), returned); - UNIT_ASSERT_VALUES_EQUAL('\0', *returned); - } - + char r[10]; + const char* returned = Quote(r, "123"); + UNIT_ASSERT_VALUES_EQUAL(r + strlen("123"), returned); + UNIT_ASSERT_VALUES_EQUAL('\0', *returned); + } + Y_UNIT_TEST(SlashIsSafeByDefault) { - char r[100]; - Quote(r, "/path;tail/path,tail/"); - UNIT_ASSERT_VALUES_EQUAL("/path%3Btail/path%2Ctail/", r); + char r[100]; + Quote(r, "/path;tail/path,tail/"); + UNIT_ASSERT_VALUES_EQUAL("/path%3Btail/path%2Ctail/", r); TString s("/path;tail/path,tail/"); - Quote(s); - UNIT_ASSERT_VALUES_EQUAL("/path%3Btail/path%2Ctail/", s.c_str()); - } - + Quote(s); + UNIT_ASSERT_VALUES_EQUAL("/path%3Btail/path%2Ctail/", s.c_str()); + } + Y_UNIT_TEST(SafeColons) { - char r[100]; - Quote(r, "/path;tail/path,tail/", ";,"); - UNIT_ASSERT_VALUES_EQUAL("%2Fpath;tail%2Fpath,tail%2F", r); + char r[100]; + Quote(r, "/path;tail/path,tail/", ";,"); + UNIT_ASSERT_VALUES_EQUAL("%2Fpath;tail%2Fpath,tail%2F", r); TString s("/path;tail/path,tail/"); - Quote(s, ";,"); - UNIT_ASSERT_VALUES_EQUAL("%2Fpath;tail%2Fpath,tail%2F", s.c_str()); - } + Quote(s, ";,"); + UNIT_ASSERT_VALUES_EQUAL("%2Fpath;tail%2Fpath,tail%2F", s.c_str()); + } Y_UNIT_TEST(StringBuf) { char r[100]; char* end = Quote(r, "abc\0/path", ""); UNIT_ASSERT_VALUES_EQUAL("abc\0%2Fpath", TStringBuf(r, end)); } -} +} |