diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/protobuf/json/string_transform.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/protobuf/json/string_transform.cpp')
-rw-r--r-- | library/cpp/protobuf/json/string_transform.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/library/cpp/protobuf/json/string_transform.cpp b/library/cpp/protobuf/json/string_transform.cpp new file mode 100644 index 0000000000..7c42daa677 --- /dev/null +++ b/library/cpp/protobuf/json/string_transform.cpp @@ -0,0 +1,64 @@ +#include "string_transform.h" + +#include <google/protobuf/stubs/strutil.h> + +#include <library/cpp/string_utils/base64/base64.h> + +namespace NProtobufJson { + void TCEscapeTransform::Transform(TString& str) const { + str = google::protobuf::CEscape(str); + } + + void TSafeUtf8CEscapeTransform::Transform(TString& str) const { + str = google::protobuf::strings::Utf8SafeCEscape(str); + } + + void TDoubleEscapeTransform::Transform(TString& str) const { + TString escaped = google::protobuf::CEscape(str); + str = ""; + for (char* it = escaped.begin(); *it; ++it) { + if (*it == '\\' || *it == '\"') + str += "\\"; + str += *it; + } + } + + void TDoubleUnescapeTransform::Transform(TString& str) const { + str = google::protobuf::UnescapeCEscapeString(Unescape(str)); + } + + TString TDoubleUnescapeTransform::Unescape(const TString& str) const { + if (str.empty()) { + return str; + } + + TString result; + result.reserve(str.size()); + + char prev = str[0]; + bool doneOutput = true; + for (const char* it = str.c_str() + 1; *it; ++it) { + if (doneOutput && prev == '\\' && (*it == '\\' || *it == '\"')) { + doneOutput = false; + } else { + result += prev; + doneOutput = true; + } + prev = *it; + } + + if ((doneOutput && prev != '\\') || !doneOutput) { + result += prev; + } + + return result; + } + + void TBase64EncodeBytesTransform::TransformBytes(TString &str) const { + str = Base64Encode(str); + } + + void TBase64DecodeBytesTransform::TransformBytes(TString &str) const { + str = Base64Decode(str); + } +} |