diff options
author | babenko <babenko@yandex-team.com> | 2023-07-26 01:52:36 +0300 |
---|---|---|
committer | root <root@qavm-2ed34686.qemu> | 2023-07-26 01:52:36 +0300 |
commit | e8de0780d2be7eeb40075bfbcfaabc5d46fe5ae6 (patch) | |
tree | 4ca8a18a809a379bd4663e945b9e2dd5fdb720a4 /library/cpp/yt | |
parent | fe143dbcf07c20ee054eb5ca1c3a983bf95f437c (diff) | |
download | ydb-e8de0780d2be7eeb40075bfbcfaabc5d46fe5ae6.tar.gz |
Support h-format for escaped (but unquoted) strings
Diffstat (limited to 'library/cpp/yt')
-rw-r--r-- | library/cpp/yt/string/format-inl.h | 31 | ||||
-rw-r--r-- | library/cpp/yt/string/unittests/format_ut.cpp | 11 |
2 files changed, 32 insertions, 10 deletions
diff --git a/library/cpp/yt/string/format-inl.h b/library/cpp/yt/string/format-inl.h index cb27961d3b..c379af712a 100644 --- a/library/cpp/yt/string/format-inl.h +++ b/library/cpp/yt/string/format-inl.h @@ -71,20 +71,26 @@ inline void FormatValue(TStringBuilderBase* builder, TStringBuf value, TStringBu bool singleQuotes = false; bool doubleQuotes = false; + bool escape = false; while (current < format.end()) { - if (*current == 'q') { - singleQuotes = true; - } else if (*current == 'Q') { - doubleQuotes = true; + switch (*current++) { + case 'q': + singleQuotes = true; + break; + case 'Q': + doubleQuotes = true; + break; + case 'h': + escape = true; + break; } - ++current; } if (padLeft) { builder->AppendChar(' ', padding); } - if (singleQuotes || doubleQuotes) { + if (singleQuotes || doubleQuotes || escape) { for (const char* valueCurrent = value.begin(); valueCurrent < value.end(); ++valueCurrent) { char ch = *valueCurrent; if (ch == '\n') { @@ -592,10 +598,15 @@ void FormatImpl( *argFormatEnd != 'p' && *argFormatEnd != 'n') { - if (*argFormatEnd == 'q') { - singleQuotes = true; - } else if (*argFormatEnd == 'Q') { - doubleQuotes = true; + switch (*argFormatEnd) { + case 'q': + singleQuotes = true; + break; + case 'Q': + doubleQuotes = true; + break; + case 'h': + break; } ++argFormatEnd; } diff --git a/library/cpp/yt/string/unittests/format_ut.cpp b/library/cpp/yt/string/unittests/format_ut.cpp index e8965ee947..5bcc0b0c40 100644 --- a/library/cpp/yt/string/unittests/format_ut.cpp +++ b/library/cpp/yt/string/unittests/format_ut.cpp @@ -157,6 +157,17 @@ TEST(TFormatTest, Quotes) EXPECT_EQ("\"\\\"\"", Format("%Qv", '\"')); } +TEST(TFormatTest, Escape) +{ + EXPECT_EQ("\'\"", Format("%hv", "\'\"")); + EXPECT_EQ("\\x01", Format("%hv", "\x1")); + EXPECT_EQ("\\x1b", Format("%hv", '\x1b')); + EXPECT_EQ("\\\\", Format("%hv", '\\')); + EXPECT_EQ("\\n", Format("%hv", '\n')); + EXPECT_EQ("\\t", Format("%hv", '\t')); + EXPECT_EQ("\'", Format("%hv", '\'')); +} + TEST(TFormatTest, Nullable) { EXPECT_EQ("1", Format("%v", std::make_optional<int>(1))); |