diff options
author | hor911 <hor911@ydb.tech> | 2023-02-09 12:40:11 +0300 |
---|---|---|
committer | hor911 <hor911@ydb.tech> | 2023-02-09 12:40:11 +0300 |
commit | 24689527cd888aa8a640ecb5077e656b3520d373 (patch) | |
tree | a613ff4cd9567b7113e8376a17f8b85897a42790 /library/cpp/unified_agent_client/helpers.cpp | |
parent | 8642d3642932f03663ba7d2d9670707c192207fd (diff) | |
download | ydb-24689527cd888aa8a640ecb5077e656b3520d373.tar.gz |
Log backend move
Diffstat (limited to 'library/cpp/unified_agent_client/helpers.cpp')
-rw-r--r-- | library/cpp/unified_agent_client/helpers.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/helpers.cpp b/library/cpp/unified_agent_client/helpers.cpp new file mode 100644 index 0000000000..01908660f3 --- /dev/null +++ b/library/cpp/unified_agent_client/helpers.cpp @@ -0,0 +1,65 @@ +#include "helpers.h" +#include <util/string/ascii.h> + +namespace NUnifiedAgent::NPrivate { + bool IsUtf8(const THashMap<TString, TString>& meta) { + for (const auto& p : meta) { + if (!IsUtf(p.first) || !IsUtf(p.second)) { + return false; + } + } + return true; + } + + ResultReplacingNonUTF ReplaceNonUTF(TStringBuf message, char signBrokenSymbol, size_t maxSize) { + ResultReplacingNonUTF result; + if (maxSize == 0) { + result.IsTruncated = !message.empty(); + return result; + } + if (message.empty()) { + return result; + } + + auto currentPoint = reinterpret_cast<const unsigned char*>(&message[0]); + auto endPoint = currentPoint + message.size(); + + auto pushSignBroken = [&result, signBrokenSymbol]() { + if (result.Data.empty() || result.Data.back() != signBrokenSymbol) { + result.Data.push_back(signBrokenSymbol); + } + ++result.BrokenCount; + }; + + while (currentPoint < endPoint) { + wchar32 rune = 0; + size_t rune_len = 0; + auto statusRead = SafeReadUTF8Char(rune, rune_len, currentPoint, endPoint); + + if (statusRead == RECODE_OK) { + if (rune_len == 1 && !IsAsciiAlnum(*currentPoint) && !IsAsciiPunct(*currentPoint) && !IsAsciiSpace(*currentPoint)) { + ++currentPoint; + pushSignBroken(); + } else { + while (rune_len != 0) { + result.Data.push_back(*currentPoint); + ++currentPoint; + --rune_len; + } + } + } else if (statusRead == RECODE_BROKENSYMBOL) { + ++currentPoint; + pushSignBroken(); + } else { + pushSignBroken(); + break; + } + + if (result.Data.size() >= maxSize && currentPoint < endPoint) { + result.IsTruncated = true; + break; + } + } + return result; + } +} |