diff options
| author | osidorkin <[email protected]> | 2026-04-20 17:41:31 +0300 |
|---|---|---|
| committer | osidorkin <[email protected]> | 2026-04-20 22:34:00 +0300 |
| commit | bac55b8485b68056b30a4d51ad60443a945c406f (patch) | |
| tree | 32c4c4d2fc8e2d9a3da33827848403bd0df2140d /library/cpp/yt/string/string.cpp | |
| parent | 089d903eb534caa106ef8f94af51bc30c9e2bbea (diff) | |
YT-27951: Add special formatter for truncated string
commit_hash:88bc9543595f9b630942d63aa1c3aee154056e92
Diffstat (limited to 'library/cpp/yt/string/string.cpp')
| -rw-r--r-- | library/cpp/yt/string/string.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/library/cpp/yt/string/string.cpp b/library/cpp/yt/string/string.cpp index d155da578e4..de9f41a5084 100644 --- a/library/cpp/yt/string/string.cpp +++ b/library/cpp/yt/string/string.cpp @@ -362,6 +362,31 @@ std::string TruncateString(std::string string, int lengthLimit, TStringBuf trunc return string; } +TTruncatedStringView::TTruncatedStringView(const std::string& value, int limit) + : Value_(value) + , Limit_(limit) +{ } + +void TTruncatedStringView::WriteToBuilder(TStringBuilderBase* builder, TStringBuf /* spec */) const +{ + int valueSize = std::ssize(Value_); + int maxSize = Limit_ + std::ssize(DefaultTruncatedMessage); + if (valueSize <= maxSize) { + builder->AppendString(Value_); + return; + } + + char* begin = builder->Preallocate(maxSize); + memcpy(begin, Value_.data(), Limit_); + memcpy(begin + Limit_, DefaultTruncatedMessage.data(), DefaultTruncatedMessage.size()); + builder->Advance(maxSize); +} + +void FormatValue(TStringBuilderBase* builder, TTruncatedStringView value, TStringBuf spec) +{ + value.WriteToBuilder(builder, spec); +} + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT |
