diff options
author | myltsev <myltsev@yandex-team.ru> | 2022-02-10 16:46:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:03 +0300 |
commit | fc361854fd6ee8d747229b090f0b8018e260d1fb (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c | |
parent | 9166d66c30c23c9e85a7c88185a068987148d23f (diff) | |
download | ydb-fc361854fd6ee8d747229b090f0b8018e260d1fb.tar.gz |
Restoring authorship annotation for <myltsev@yandex-team.ru>. Commit 2 of 2.
76 files changed, 1178 insertions, 1178 deletions
diff --git a/build/rules/contrib_restricted.policy b/build/rules/contrib_restricted.policy index c1d2941244..a83ead1904 100644 --- a/build/rules/contrib_restricted.policy +++ b/build/rules/contrib_restricted.policy @@ -74,9 +74,9 @@ ALLOW extsearch/audio/kernel/recoglib -> contrib/restricted/mpg123 ALLOW yandex_io -> contrib/restricted/openal-soft ALLOW speechkit -> contrib/restricted/openal-soft -# rubberband is a GPL audio stretching library -ALLOW dict/mt/video -> contrib/restricted/rubberband - +# rubberband is a GPL audio stretching library +ALLOW dict/mt/video -> contrib/restricted/rubberband + # Allowed subset of abseil is exported via library/ ALLOW library/cpp/containers/absl_flat_hash -> contrib/restricted/abseil-cpp/absl/container ALLOW library/cpp/containers/absl_tstring_flat_hash -> contrib/restricted/abseil-cpp-tstring/y_absl/container diff --git a/build/ya.conf.json b/build/ya.conf.json index 63480eb5b6..5f7cc875d6 100644 --- a/build/ya.conf.json +++ b/build/ya.conf.json @@ -1350,14 +1350,14 @@ } ] }, - "sandboxctl": { - "tools": { + "sandboxctl": { + "tools": { "sandboxctl": { "bottle": "sandboxctl", "executable": "sandboxctl" } - }, - "platforms": [ + }, + "platforms": [ { "host": { "os": "LINUX" @@ -1376,8 +1376,8 @@ }, "default": true } - ] - }, + ] + }, "gcc82": { "tools": { "cc": { @@ -5920,19 +5920,19 @@ ] } }, - "sandboxctl": { - "formula": { + "sandboxctl": { + "formula": { "sandbox_id": [ 832226067 ], "match": "tools/sandboxctl/bin/sandboxctl" - }, - "executable": { + }, + "executable": { "sandboxctl": [ "sandboxctl" ] - } - }, + } + }, "python": { "formula": { "sandbox_id": 981577320, diff --git a/contrib/libs/ya.make b/contrib/libs/ya.make index 25512cef43..9c4640fdcf 100644 --- a/contrib/libs/ya.make +++ b/contrib/libs/ya.make @@ -220,7 +220,7 @@ RECURSE( lzmasdk lzo2 matrixssl - mecab + mecab metrohash mimalloc minilzo diff --git a/contrib/python/dateutil/dateutil/_version.py b/contrib/python/dateutil/dateutil/_version.py index e66b144279..b723056a75 100644 --- a/contrib/python/dateutil/dateutil/_version.py +++ b/contrib/python/dateutil/dateutil/_version.py @@ -1,5 +1,5 @@ -# coding: utf-8 -# file generated by setuptools_scm -# don't change, don't track in version control +# coding: utf-8 +# file generated by setuptools_scm +# don't change, don't track in version control version = '2.8.2' version_tuple = (2, 8, 2) diff --git a/contrib/python/dateutil/ya.make b/contrib/python/dateutil/ya.make index 9cedae1bfc..0c4102c505 100644 --- a/contrib/python/dateutil/ya.make +++ b/contrib/python/dateutil/ya.make @@ -23,7 +23,7 @@ PY_SRCS( TOP_LEVEL dateutil/__init__.py dateutil/_common.py - dateutil/_version.py + dateutil/_version.py dateutil/easter.py dateutil/parser/__init__.py dateutil/parser/_parser.py diff --git a/contrib/python/toml/ya.make b/contrib/python/toml/ya.make index 1384b00b76..104e501e8e 100644 --- a/contrib/python/toml/ya.make +++ b/contrib/python/toml/ya.make @@ -1,6 +1,6 @@ OWNER(g:python-contrib) -PY23_LIBRARY() +PY23_LIBRARY() LICENSE(MIT) diff --git a/contrib/python/ya.make b/contrib/python/ya.make index bf31a76158..d01ced9f3a 100644 --- a/contrib/python/ya.make +++ b/contrib/python/ya.make @@ -526,7 +526,7 @@ RECURSE( invoke ipaddr ipaddress - ipadic + ipadic ipdb ipykernel ipython @@ -616,7 +616,7 @@ RECURSE( matplotlib matplotlib-inline mccabe - mecab-python3 + mecab-python3 meld3 memory-profiler mercurial diff --git a/contrib/restricted/ya.make b/contrib/restricted/ya.make index f1f60c8cf6..b52a206733 100644 --- a/contrib/restricted/ya.make +++ b/contrib/restricted/ya.make @@ -52,7 +52,7 @@ RECURSE( protozero range-v3 rnnoise - rubberband + rubberband spdlog thrift thrift/compiler diff --git a/library/cpp/containers/comptrie/comptrie_builder.h b/library/cpp/containers/comptrie/comptrie_builder.h index 39a394f6ef..cf7d2e39a3 100644 --- a/library/cpp/containers/comptrie/comptrie_builder.h +++ b/library/cpp/containers/comptrie/comptrie_builder.h @@ -115,7 +115,7 @@ protected: // IMPORTANT: never try to reminimize an already minimized trie or a trie with fast layout. // Because of non-local structure and epsilon links, it won't work // as you expect it to, and can destroy the trie in the making. -// If you want both minimization and fast layout, do the minimization first. +// If you want both minimization and fast layout, do the minimization first. template <class TPacker> size_t CompactTrieMinimize(IOutputStream& os, const char* data, size_t datalength, bool verbose = false, const TPacker& packer = TPacker(), NCompactTrie::EMinimizeMode mode = NCompactTrie::MM_DEFAULT); diff --git a/library/cpp/getopt/small/last_getopt_opt.h b/library/cpp/getopt/small/last_getopt_opt.h index f1ed8c7017..a8dd5adca9 100644 --- a/library/cpp/getopt/small/last_getopt_opt.h +++ b/library/cpp/getopt/small/last_getopt_opt.h @@ -637,11 +637,11 @@ namespace NLastGetopt { return DefaultValue("0").StoreResult(target, true); } - // Similar to store_true in Python's argparse - TOpt& StoreTrue(bool* target) { - return NoArgument().SetFlag(target); - } - + // Similar to store_true in Python's argparse + TOpt& StoreTrue(bool* target) { + return NoArgument().SetFlag(target); + } + template <typename TpVal, typename T, typename TpFunc> TOpt& StoreMappedResultT(T* target, const TpFunc& func) { return Handler1T<TpVal>(NPrivate::TStoreMappedResultFunctor<T, TpFunc, TpVal>(target, func)); @@ -675,13 +675,13 @@ namespace NLastGetopt { // Appends FromString<T>(arg) to *target for each argument template <typename T> TOpt& AppendTo(TVector<T>* target) { - return Handler1T<T>([target](auto&& value) { target->push_back(std::move(value)); }); + return Handler1T<T>([target](auto&& value) { target->push_back(std::move(value)); }); } // Appends FromString<T>(arg) to *target for each argument template <typename T> TOpt& InsertTo(THashSet<T>* target) { - return Handler1T<T>([target](auto&& value) { target->insert(std::move(value)); }); + return Handler1T<T>([target](auto&& value) { target->insert(std::move(value)); }); } // Emplaces TString arg to *target for each argument diff --git a/library/cpp/getopt/ut/last_getopt_ut.cpp b/library/cpp/getopt/ut/last_getopt_ut.cpp index cec948ecac..c99a1d053d 100644 --- a/library/cpp/getopt/ut/last_getopt_ut.cpp +++ b/library/cpp/getopt/ut/last_getopt_ut.cpp @@ -455,20 +455,20 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { } Y_UNIT_TEST(TestSetFlag) { - bool a = false, b = true, c = false, d = true; + bool a = false, b = true, c = false, d = true; TOptsNoDefault opts; opts.AddLongOption('a', "alpha").NoArgument().SetFlag(&a); opts.AddLongOption('b', "beta").NoArgument().SetFlag(&b); - opts.AddCharOption('c').StoreTrue(&c); - opts.AddCharOption('d').StoreTrue(&d); + opts.AddCharOption('c').StoreTrue(&c); + opts.AddCharOption('d').StoreTrue(&d); - TOptsParseResultTestWrapper r(&opts, V({"cmd", "-a", "-c"})); + TOptsParseResultTestWrapper r(&opts, V({"cmd", "-a", "-c"})); UNIT_ASSERT(a); UNIT_ASSERT(!b); - UNIT_ASSERT(c); - UNIT_ASSERT(!d); + UNIT_ASSERT(c); + UNIT_ASSERT(!d); } Y_UNIT_TEST(TestDefaultValue) { diff --git a/library/cpp/http/io/headers.cpp b/library/cpp/http/io/headers.cpp index fe82383318..4ec27a29e8 100644 --- a/library/cpp/http/io/headers.cpp +++ b/library/cpp/http/io/headers.cpp @@ -75,12 +75,12 @@ const THttpInputHeader* THttpHeaders::FindHeader(const TStringBuf header) const void THttpHeaders::RemoveHeader(const TStringBuf header) { for (auto h = Headers_.begin(); h != Headers_.end(); ++h) { if (AsciiCompareIgnoreCase(h->Name(), header) == 0) { - Headers_.erase(h); - return; - } - } -} - + Headers_.erase(h); + return; + } + } +} + void THttpHeaders::AddOrReplaceHeader(const THttpInputHeader& header) { for (auto& hdr : Headers_) { if (AsciiCompareIgnoreCase(hdr.Name(), header.Name()) == 0) { diff --git a/library/cpp/http/io/headers.h b/library/cpp/http/io/headers.h index 3b15d60462..a71793d1c6 100644 --- a/library/cpp/http/io/headers.h +++ b/library/cpp/http/io/headers.h @@ -102,9 +102,9 @@ public: // Проверяет, есть ли такой заголовок bool HasHeader(TStringBuf header) const; - /// Удаляет заголовок, если он есть. + /// Удаляет заголовок, если он есть. void RemoveHeader(TStringBuf header); - + /// Ищет заголовок по указанному имени /// Возвращает nullptr, если не нашел const THttpInputHeader* FindHeader(TStringBuf header) const; diff --git a/library/cpp/http/io/stream.cpp b/library/cpp/http/io/stream.cpp index 90d063a4ba..6689be684f 100644 --- a/library/cpp/http/io/stream.cpp +++ b/library/cpp/http/io/stream.cpp @@ -776,7 +776,7 @@ private: TString scheme = Request_->BestCompressionScheme(ComprSchemas_); if (scheme != "identity") { AddOrReplaceHeader(THttpInputHeader("Content-Encoding", scheme)); - RemoveHeader("Content-Length"); + RemoveHeader("Content-Length"); } } @@ -851,9 +851,9 @@ private: } inline void RemoveHeader(const TString& hdr) { - Headers_.RemoveHeader(hdr); - } - + Headers_.RemoveHeader(hdr); + } + private: IOutputStream* Slave_; TState State_; diff --git a/library/cpp/http/io/stream_ut.cpp b/library/cpp/http/io/stream_ut.cpp index 64b48b4a66..1ea35df675 100644 --- a/library/cpp/http/io/stream_ut.cpp +++ b/library/cpp/http/io/stream_ut.cpp @@ -420,27 +420,27 @@ Y_UNIT_TEST_SUITE(THttpStreamTest) { } Y_UNIT_TEST(ContentLengthRemoval) { - TMemoryInput request("GET / HTTP/1.1\r\nAccept-Encoding: gzip\r\n\r\n"); - THttpInput i(&request); + TMemoryInput request("GET / HTTP/1.1\r\nAccept-Encoding: gzip\r\n\r\n"); + THttpInput i(&request); TString result; - TStringOutput out(result); - THttpOutput httpOut(&out, &i); - - httpOut.EnableKeepAlive(true); - httpOut.EnableCompression(true); - httpOut << "HTTP/1.1 200 OK\r\n"; - char answer[] = "Mary had a little lamb."; + TStringOutput out(result); + THttpOutput httpOut(&out, &i); + + httpOut.EnableKeepAlive(true); + httpOut.EnableCompression(true); + httpOut << "HTTP/1.1 200 OK\r\n"; + char answer[] = "Mary had a little lamb."; httpOut << "Content-Length: " << strlen(answer) << "\r\n" "\r\n"; - httpOut << answer; - httpOut.Finish(); - - Cdbg << result; - result.to_lower(); - UNIT_ASSERT(result.Contains("content-encoding: gzip")); - UNIT_ASSERT(!result.Contains("content-length")); - } - + httpOut << answer; + httpOut.Finish(); + + Cdbg << result; + result.to_lower(); + UNIT_ASSERT(result.Contains("content-encoding: gzip")); + UNIT_ASSERT(!result.Contains("content-length")); + } + Y_UNIT_TEST(CodecsPriority) { TMemoryInput request("GET / HTTP/1.1\r\nAccept-Encoding: gzip, br\r\n\r\n"); TVector<TStringBuf> codecs = {"br", "gzip"}; diff --git a/library/cpp/http/misc/httpdate.cpp b/library/cpp/http/misc/httpdate.cpp index 1d6617eeec..4a3031bbf4 100644 --- a/library/cpp/http/misc/httpdate.cpp +++ b/library/cpp/http/misc/httpdate.cpp @@ -75,9 +75,9 @@ char* format_http_date(time_t when, char* buf, size_t buflen) { return buf; } - + TString FormatHttpDate(time_t when) { - char str[64] = {0}; + char str[64] = {0}; format_http_date(str, Y_ARRAY_SIZE(str), when); return TString(str); -} +} diff --git a/library/cpp/http/misc/httpdate.h b/library/cpp/http/misc/httpdate.h index 2a33545a7e..04876f38fe 100644 --- a/library/cpp/http/misc/httpdate.h +++ b/library/cpp/http/misc/httpdate.h @@ -17,5 +17,5 @@ inline time_t parse_http_date(const TStringBuf& datestring) { int format_http_date(char buf[], size_t size, time_t when); char* format_http_date(time_t when, char* buf, size_t len); - + TString FormatHttpDate(time_t when); diff --git a/library/cpp/http/misc/httpdate_ut.cpp b/library/cpp/http/misc/httpdate_ut.cpp index 8d700a9593..c1a0103501 100644 --- a/library/cpp/http/misc/httpdate_ut.cpp +++ b/library/cpp/http/misc/httpdate_ut.cpp @@ -10,6 +10,6 @@ Y_UNIT_TEST_SUITE(TestHttpDate) { UNIT_ASSERT((int)strlen(format_http_date(0, buf1, sizeof(buf1))) == format_http_date(buf2, sizeof(buf2), 0)); } Y_UNIT_TEST(Test2) { - UNIT_ASSERT_STRINGS_EQUAL(FormatHttpDate(1234567890), "Fri, 13 Feb 2009 23:31:30 GMT"); - } + UNIT_ASSERT_STRINGS_EQUAL(FormatHttpDate(1234567890), "Fri, 13 Feb 2009 23:31:30 GMT"); + } } diff --git a/library/cpp/http/server/response.h b/library/cpp/http/server/response.h index 05a4d53caa..a75cb85605 100644 --- a/library/cpp/http/server/response.h +++ b/library/cpp/http/server/response.h @@ -51,10 +51,10 @@ public: return *this; } - TString GetContent() const { - return Content; - } - + TString GetContent() const { + return Content; + } + /** * @note If @arg content isn't empty its size is automatically added as a * "Content-Length" header during output to IOutputStream. diff --git a/library/cpp/json/easy_parse/json_easy_parser_impl.h b/library/cpp/json/easy_parse/json_easy_parser_impl.h index 9f9486c6f5..ec55d838b3 100644 --- a/library/cpp/json/easy_parse/json_easy_parser_impl.h +++ b/library/cpp/json/easy_parse/json_easy_parser_impl.h @@ -3,34 +3,34 @@ #include <util/generic/string.h> namespace NJson { - namespace NImpl { + namespace NImpl { enum EType { ARRAY, MAP, MAP_KEY }; - } + } template <class TStringType> - struct TPathElemImpl { - NImpl::EType Type; + struct TPathElemImpl { + NImpl::EType Type; TStringType Key; int ArrayCounter; - TPathElemImpl(NImpl::EType type) + TPathElemImpl(NImpl::EType type) : Type(type) , ArrayCounter() { } TPathElemImpl(const TStringType& key) - : Type(NImpl::MAP_KEY) + : Type(NImpl::MAP_KEY) , Key(key) , ArrayCounter() { } TPathElemImpl(int arrayCounter) - : Type(NImpl::ARRAY) + : Type(NImpl::ARRAY) , ArrayCounter(arrayCounter) { } diff --git a/library/cpp/json/json_value.h b/library/cpp/json/json_value.h index 9109377521..f70f4d2ee8 100644 --- a/library/cpp/json/json_value.h +++ b/library/cpp/json/json_value.h @@ -1,3 +1,3 @@ -#pragma once - +#pragma once + #include <library/cpp/json/writer/json_value.h> diff --git a/library/cpp/json/json_writer.cpp b/library/cpp/json/json_writer.cpp index c9fc529135..3d058bae36 100644 --- a/library/cpp/json/json_writer.cpp +++ b/library/cpp/json/json_writer.cpp @@ -73,14 +73,14 @@ namespace NJson { throw yexception() << "JSON writer: invalid UTF-8"; if (Buf.KeyExpected()) { Buf.WriteKey(value); - } else { + } else { if (DontEscapeStrings) { Buf.UnsafeWriteValue(TString("\"") + value + '"'); } else { Buf.WriteString(value); } - } - } + } + } void TJsonWriter::WriteNull() { Buf.WriteNull(); diff --git a/library/cpp/json/json_writer.h b/library/cpp/json/json_writer.h index 471ff3ed71..c7f5c9499a 100644 --- a/library/cpp/json/json_writer.h +++ b/library/cpp/json/json_writer.h @@ -1,7 +1,7 @@ #pragma once // Deprecated. Use library/cpp/json/writer in new code. - + #include "json_value.h" #include <library/cpp/json/writer/json.h> diff --git a/library/cpp/json/ut/json_writer_ut.cpp b/library/cpp/json/ut/json_writer_ut.cpp index f4d9cde0e8..ca11d34dad 100644 --- a/library/cpp/json/ut/json_writer_ut.cpp +++ b/library/cpp/json/ut/json_writer_ut.cpp @@ -71,17 +71,17 @@ Y_UNIT_TEST_SUITE(TJsonWriterTest) { Y_UNIT_TEST(SortKeys) { TString expected = "{\"a\":null,\"j\":null,\"n\":null,\"y\":null,\"z\":null}"; - TJsonValue v; - v["z"] = JSON_NULL; - v["n"] = JSON_NULL; - v["a"] = JSON_NULL; - v["y"] = JSON_NULL; - v["j"] = JSON_NULL; - TStringStream out; - WriteJson(&out, &v, false, true); - UNIT_ASSERT_STRINGS_EQUAL(out.Str(), expected); - } - + TJsonValue v; + v["z"] = JSON_NULL; + v["n"] = JSON_NULL; + v["a"] = JSON_NULL; + v["y"] = JSON_NULL; + v["j"] = JSON_NULL; + TStringStream out; + WriteJson(&out, &v, false, true); + UNIT_ASSERT_STRINGS_EQUAL(out.Str(), expected); + } + Y_UNIT_TEST(SimpleUnsignedIntegerWriteTest) { { TString expected = "{\"test\":1}"; diff --git a/library/cpp/json/writer/README b/library/cpp/json/writer/README index 6e6f754658..a20489f32e 100644 --- a/library/cpp/json/writer/README +++ b/library/cpp/json/writer/README @@ -1,23 +1,23 @@ -JSON writer with no external dependencies, producing output -where HTML special characters are always escaped. - -Use it like this: - +JSON writer with no external dependencies, producing output +where HTML special characters are always escaped. + +Use it like this: + #include <library/cpp/json/writer/json.h> - ... - - NJsonWriter::TBuf json; - json.BeginList() - .WriteString("<script>") - .EndList(); - Cout << json.Str(); // output: ["\u003Cscript\u003E"] - -For compatibility with legacy formats where object keys -are not quoted, use CompatWriteKeyWithoutQuotes: - - NJsonWriter::TBuf json; - json.BeginObject() - .CompatWriteKeyWithoutQuotes("r").WriteInt(1) - .CompatWriteKeyWithoutQuotes("n").WriteInt(0) - .EndObject(); - Cout << json.Str(); // output: {r:1,n:0} + ... + + NJsonWriter::TBuf json; + json.BeginList() + .WriteString("<script>") + .EndList(); + Cout << json.Str(); // output: ["\u003Cscript\u003E"] + +For compatibility with legacy formats where object keys +are not quoted, use CompatWriteKeyWithoutQuotes: + + NJsonWriter::TBuf json; + json.BeginObject() + .CompatWriteKeyWithoutQuotes("r").WriteInt(1) + .CompatWriteKeyWithoutQuotes("n").WriteInt(0) + .EndObject(); + Cout << json.Str(); // output: {r:1,n:0} diff --git a/library/cpp/json/writer/json.cpp b/library/cpp/json/writer/json.cpp index 50523bf754..02370c2d79 100644 --- a/library/cpp/json/writer/json.cpp +++ b/library/cpp/json/writer/json.cpp @@ -1,14 +1,14 @@ -#include "json.h" +#include "json.h" #include <library/cpp/json/json_value.h> #include <util/string/cast.h> #include <util/string/strspn.h> -#include <util/generic/algorithm.h> -#include <util/generic/ymath.h> +#include <util/generic/algorithm.h> +#include <util/generic/ymath.h> #include <util/generic/singleton.h> - -namespace NJsonWriter { + +namespace NJsonWriter { TBuf::TBuf(EHtmlEscapeMode mode, IOutputStream* stream) : Stream(stream) , NeedComma(false) @@ -25,7 +25,7 @@ namespace NJsonWriter { StringStream.Reset(new TStringStream); Stream = StringStream.Get(); } - + Stack.reserve(64); // should be enough for most cases StackPush(JE_OUTER_SPACE); } @@ -47,12 +47,12 @@ namespace NJsonWriter { inline void TBuf::StackPush(EJsonEntity e) { Stack.push_back(e); - } - + } + inline EJsonEntity TBuf::StackTop() const { return Stack.back(); } - + inline void TBuf::StackPop() { Y_ASSERT(!Stack.empty()); const EJsonEntity current = StackTop(); @@ -81,8 +81,8 @@ namespace NJsonWriter { << EntityToStr(StackTop()) << " on the stack"; } StackPop(); - } - + } + void TBuf::PrintIndentation(bool closing) { if (!IndentSpaces) return; @@ -126,7 +126,7 @@ namespace NJsonWriter { } WriteComma(); } - + inline void TBuf::BeginKey() { if (Y_UNLIKELY(!KeyExpected())) { ythrow TError() << "JSON writer: key written outside of an object"; @@ -135,14 +135,14 @@ namespace NJsonWriter { StackPush(JE_PAIR); NeedComma = false; NeedNewline = false; - } + } inline void TBuf::EndValue() { if (StackTop() == JE_PAIR) { StackPop(); } } - + TValueContext TBuf::BeginList() { NeedNewline = true; BeginValue(); @@ -150,8 +150,8 @@ namespace NJsonWriter { StackPush(JE_LIST); NeedComma = false; return TValueContext(*this); - } - + } + TPairContext TBuf::BeginObject() { NeedNewline = true; BeginValue(); @@ -159,28 +159,28 @@ namespace NJsonWriter { StackPush(JE_OBJECT); NeedComma = false; return TPairContext(*this); - } - + } + TAfterColonContext TBuf::UnsafeWriteKey(const TStringBuf& s) { BeginKey(); RawWriteChar('"'); UnsafeWriteRawBytes(s); UnsafeWriteRawBytes("\":", 2); return TAfterColonContext(*this); - } - + } + TAfterColonContext TBuf::WriteKey(const TStringBuf& s) { // use the default escaping mode for this object return WriteKey(s, EscapeMode); } - + TAfterColonContext TBuf::WriteKey(const TStringBuf& s, EHtmlEscapeMode hem) { BeginKey(); WriteBareString(s, hem); RawWriteChar(':'); return TAfterColonContext(*this); } - + TAfterColonContext TBuf::CompatWriteKeyWithoutQuotes(const TStringBuf& s) { BeginKey(); Y_ASSERT(AllOf(s, [](char x) { return 'a' <= x && x <= 'z'; })); @@ -194,44 +194,44 @@ namespace NJsonWriter { EndValue(); return *this; } - + TBuf& TBuf::EndObject() { CheckAndPop(JE_OBJECT); EndValue(); return *this; } - + TValueContext TBuf::WriteString(const TStringBuf& s) { // use the default escaping mode for this object return WriteString(s, EscapeMode); } - + TValueContext TBuf::WriteString(const TStringBuf& s, EHtmlEscapeMode hem) { BeginValue(); WriteBareString(s, hem); EndValue(); return TValueContext(*this); } - + TValueContext TBuf::WriteNull() { UnsafeWriteValue(TStringBuf("null")); return TValueContext(*this); } - + TValueContext TBuf::WriteBool(bool b) { constexpr TStringBuf trueVal = "true"; constexpr TStringBuf falseVal = "false"; UnsafeWriteValue(b ? trueVal : falseVal); return TValueContext(*this); } - + TValueContext TBuf::WriteInt(int i) { char buf[22]; // enough to hold any 64-bit number size_t len = ToString(i, buf, sizeof(buf)); UnsafeWriteValue(buf, len); return TValueContext(*this); } - + TValueContext TBuf::WriteLongLong(long long i) { static_assert(sizeof(long long) <= 8, "expect sizeof(long long) <= 8"); char buf[22]; // enough to hold any 64-bit number @@ -239,14 +239,14 @@ namespace NJsonWriter { UnsafeWriteValue(buf, len); return TValueContext(*this); } - + TValueContext TBuf::WriteULongLong(unsigned long long i) { char buf[22]; // enough to hold any 64-bit number size_t len = ToString(i, buf, sizeof(buf)); UnsafeWriteValue(buf, len); return TValueContext(*this); } - + template <class TFloat> TValueContext TBuf::WriteFloatImpl(TFloat f, EFloatToStringMode mode, int ndigits) { char buf[512]; // enough to hold most floats, the same buffer is used in FloatToString implementation @@ -271,7 +271,7 @@ namespace NJsonWriter { TValueContext TBuf::WriteDouble(double f, EFloatToStringMode mode, int ndigits) { return WriteFloatImpl(f, mode, ndigits); } - + namespace { struct TFinder: public TCompactStrSpn { inline TFinder() @@ -306,12 +306,12 @@ namespace NJsonWriter { } UnsafeWriteRawBytes(b, e - b); RawWriteChar('"'); - } - + } + inline void TBuf::RawWriteChar(char c) { Stream->Write(c); } - + void TBuf::WriteHexEscape(unsigned char c) { Y_ASSERT(c < 0x80); UnsafeWriteRawBytes("\\u00", 4); @@ -319,13 +319,13 @@ namespace NJsonWriter { RawWriteChar(hexDigits[(c & 0xf0) >> 4]); RawWriteChar(hexDigits[(c & 0x0f)]); } - + #define MATCH(sym, string) \ case sym: \ UnsafeWriteRawBytes(beg, cur - beg); \ UnsafeWriteRawBytes(TStringBuf(string)); \ return true - + inline bool TBuf::EscapedWriteChar(const char* beg, const char* cur, EHtmlEscapeMode hem) { unsigned char c = *cur; if (hem == HEM_ESCAPE_HTML) { @@ -337,11 +337,11 @@ namespace NJsonWriter { MATCH('&', "&"); } //for other characters, we fall through to the non-HTML-escaped part - } - + } + if (hem == HEM_RELAXED && c == '/') return false; - + if (hem != HEM_UNSAFE) { switch (c) { case '/': @@ -356,16 +356,16 @@ namespace NJsonWriter { return true; } // for other characters, fall through to the non-escaped part - } - + } + switch (c) { - MATCH('"', "\\\""); - MATCH('\\', "\\\\"); - MATCH('\b', "\\b"); - MATCH('\f', "\\f"); - MATCH('\n', "\\n"); - MATCH('\r', "\\r"); - MATCH('\t', "\\t"); + MATCH('"', "\\\""); + MATCH('\\', "\\\\"); + MATCH('\b', "\\b"); + MATCH('\f', "\\f"); + MATCH('\n', "\\n"); + MATCH('\r', "\\r"); + MATCH('\t', "\\t"); } if (c < 0x20) { UnsafeWriteRawBytes(beg, cur - beg); @@ -374,14 +374,14 @@ namespace NJsonWriter { } return false; - } + } #undef MATCH static bool LessStrPtr(const TString* a, const TString* b) { return *a < *b; } - + TValueContext TBuf::WriteJsonValue(const NJson::TJsonValue* v, bool sortKeys, EFloatToStringMode mode, int ndigits) { using namespace NJson; switch (v->GetType()) { @@ -411,7 +411,7 @@ namespace NJsonWriter { WriteJsonValue(&it, sortKeys, mode, ndigits); EndList(); break; - } + } case JSON_MAP: { BeginObject(); const TJsonValue::TMapType& map = v->GetMap(); @@ -436,10 +436,10 @@ namespace NJsonWriter { } EndObject(); break; - } - } + } + } return TValueContext(*this); - } + } TPairContext TBuf::UnsafeWritePair(const TStringBuf& s) { if (Y_UNLIKELY(StackTop() != JE_OBJECT)) { @@ -448,28 +448,28 @@ namespace NJsonWriter { WriteComma(); UnsafeWriteRawBytes(s); return TPairContext(*this); - } - + } + void TBuf::UnsafeWriteValue(const TStringBuf& s) { BeginValue(); UnsafeWriteRawBytes(s); EndValue(); - } - + } + void TBuf::UnsafeWriteValue(const char* s, size_t len) { BeginValue(); UnsafeWriteRawBytes(s, len); EndValue(); } - + void TBuf::UnsafeWriteRawBytes(const char* src, size_t len) { Stream->Write(src, len); } - + void TBuf::UnsafeWriteRawBytes(const TStringBuf& s) { UnsafeWriteRawBytes(s.data(), s.size()); } - + const TString& TBuf::Str() const { if (!StringStream) { ythrow TError() << "JSON writer: Str() called " @@ -480,7 +480,7 @@ namespace NJsonWriter { } return StringStream->Str(); } - + void TBuf::FlushTo(IOutputStream* stream) { if (!StringStream) { ythrow TError() << "JSON writer: FlushTo() called " @@ -488,7 +488,7 @@ namespace NJsonWriter { } stream->Write(StringStream->Str()); StringStream->Clear(); - } + } TString WrapJsonToCallback(const TBuf& buf, TStringBuf callback) { if (!callback) { @@ -496,12 +496,12 @@ namespace NJsonWriter { } else { return TString::Join(callback, "(", buf.Str(), ")"); } - } - + } + TBufState TBuf::State() const { return TBufState{NeedComma, NeedNewline, Stack}; - } - + } + void TBuf::Reset(const TBufState& from) { NeedComma = from.NeedComma; NeedNewline = from.NeedNewline; diff --git a/library/cpp/json/writer/json.h b/library/cpp/json/writer/json.h index f8e0783574..0aae2531b9 100644 --- a/library/cpp/json/writer/json.h +++ b/library/cpp/json/writer/json.h @@ -1,38 +1,38 @@ -#pragma once - -#include <util/generic/noncopyable.h> -#include <util/generic/ptr.h> +#pragma once + +#include <util/generic/noncopyable.h> +#include <util/generic/ptr.h> #include <util/generic/string.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> -#include <util/stream/str.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> +#include <util/stream/str.h> #include <util/string/cast.h> - -namespace NJson { - class TJsonValue; -} - -namespace NJsonWriter { + +namespace NJson { + class TJsonValue; +} + +namespace NJsonWriter { enum EJsonEntity : ui8 { - JE_OUTER_SPACE = 1, - JE_LIST, - JE_OBJECT, - JE_PAIR, - }; - - enum EHtmlEscapeMode { - HEM_ESCAPE_HTML = 1, // Use HTML escaping: < > & \/ - HEM_DONT_ESCAPE_HTML, // Use JSON escaping: \u003C \u003E \u0026 \/ - HEM_RELAXED, // Use JSON escaping: \u003C \u003E \u0026 / - HEM_UNSAFE, // Turn escaping off: < > & / - }; - - class TError: public yexception {}; - - class TValueContext; - class TPairContext; - class TAfterColonContext; - + JE_OUTER_SPACE = 1, + JE_LIST, + JE_OBJECT, + JE_PAIR, + }; + + enum EHtmlEscapeMode { + HEM_ESCAPE_HTML = 1, // Use HTML escaping: < > & \/ + HEM_DONT_ESCAPE_HTML, // Use JSON escaping: \u003C \u003E \u0026 \/ + HEM_RELAXED, // Use JSON escaping: \u003C \u003E \u0026 / + HEM_UNSAFE, // Turn escaping off: < > & / + }; + + class TError: public yexception {}; + + class TValueContext; + class TPairContext; + class TAfterColonContext; + struct TBufState { bool NeedComma; bool NeedNewline; @@ -40,102 +40,102 @@ namespace NJsonWriter { }; class TBuf : TNonCopyable { - public: + public: TBuf(EHtmlEscapeMode mode = HEM_DONT_ESCAPE_HTML, IOutputStream* stream = nullptr); - + TValueContext WriteString(const TStringBuf& s, EHtmlEscapeMode hem); TValueContext WriteString(const TStringBuf& s); - TValueContext WriteInt(int i); + TValueContext WriteInt(int i); TValueContext WriteLongLong(long long i); TValueContext WriteULongLong(unsigned long long i); TValueContext WriteFloat(float f, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 6); TValueContext WriteDouble(double f, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 10); - TValueContext WriteBool(bool b); - TValueContext WriteNull(); + TValueContext WriteBool(bool b); + TValueContext WriteNull(); TValueContext WriteJsonValue(const NJson::TJsonValue* value, bool sortKeys = false, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 10); - - TValueContext BeginList(); - TBuf& EndList(); - - TPairContext BeginObject(); + + TValueContext BeginList(); + TBuf& EndList(); + + TPairContext BeginObject(); TAfterColonContext WriteKey(const TStringBuf& key, EHtmlEscapeMode hem); TAfterColonContext WriteKey(const TStringBuf& key); TAfterColonContext UnsafeWriteKey(const TStringBuf& key); bool KeyExpected() const { return Stack.back() == JE_OBJECT; } - - //! deprecated, do not use in new code + + //! deprecated, do not use in new code TAfterColonContext CompatWriteKeyWithoutQuotes(const TStringBuf& key); - - TBuf& EndObject(); - - /*** Indent the resulting JSON with spaces. - * By default (spaces==0) no formatting is done. */ - TBuf& SetIndentSpaces(int spaces) { - IndentSpaces = spaces; - return *this; - } - + + TBuf& EndObject(); + + /*** Indent the resulting JSON with spaces. + * By default (spaces==0) no formatting is done. */ + TBuf& SetIndentSpaces(int spaces) { + IndentSpaces = spaces; + return *this; + } + /*** NaN and Inf are not valid json values, * so if WriteNanAsString is set, writer would write string * intead of throwing exception (default case) */ - TBuf& SetWriteNanAsString(bool writeNanAsString = true) { + TBuf& SetWriteNanAsString(bool writeNanAsString = true) { WriteNanAsString = writeNanAsString; - return *this; + return *this; } - /*** Return the string formed in the internal TStringStream. - * You may only call it if the `stream' parameter was NULL - * at construction time. */ + /*** Return the string formed in the internal TStringStream. + * You may only call it if the `stream' parameter was NULL + * at construction time. */ const TString& Str() const; - - /*** Dump and forget the string constructed so far. - * You may only call it if the `stream' parameter was NULL - * at construction time. */ + + /*** Dump and forget the string constructed so far. + * You may only call it if the `stream' parameter was NULL + * at construction time. */ void FlushTo(IOutputStream* stream); - - /*** Write a literal string that represents a JSON value - * (string, number, object, array, bool, or null). - * - * Example: - * j.UnsafeWriteValue("[1, 2, 3, \"o'clock\", 4, \"o'clock rock\"]"); - * - * As in all of the Unsafe* functions, no escaping is done. */ + + /*** Write a literal string that represents a JSON value + * (string, number, object, array, bool, or null). + * + * Example: + * j.UnsafeWriteValue("[1, 2, 3, \"o'clock\", 4, \"o'clock rock\"]"); + * + * As in all of the Unsafe* functions, no escaping is done. */ void UnsafeWriteValue(const TStringBuf& s); - void UnsafeWriteValue(const char* s, size_t len); - - /*** When in the context of an object, write a literal string - * that represents a key:value pair (or several pairs). - * - * Example: - * j.BeginObject(); - * j.UnsafeWritePair("\"adam\": \"male\", \"eve\": \"female\""); - * j.EndObject(); - * - * As in all of the Unsafe* functions, no escaping is done. */ + void UnsafeWriteValue(const char* s, size_t len); + + /*** When in the context of an object, write a literal string + * that represents a key:value pair (or several pairs). + * + * Example: + * j.BeginObject(); + * j.UnsafeWritePair("\"adam\": \"male\", \"eve\": \"female\""); + * j.EndObject(); + * + * As in all of the Unsafe* functions, no escaping is done. */ TPairContext UnsafeWritePair(const TStringBuf& s); - - /*** Copy the supplied string directly into the output stream. */ + + /*** Copy the supplied string directly into the output stream. */ void UnsafeWriteRawBytes(const TStringBuf& s); - void UnsafeWriteRawBytes(const char* c, size_t len); - + void UnsafeWriteRawBytes(const char* c, size_t len); + TBufState State() const; void Reset(const TBufState& from); void Reset(TBufState&& from); - private: - void BeginValue(); - void EndValue(); - void BeginKey(); - void RawWriteChar(char c); + private: + void BeginValue(); + void EndValue(); + void BeginKey(); + void RawWriteChar(char c); bool EscapedWriteChar(const char* b, const char* c, EHtmlEscapeMode hem); void WriteBareString(const TStringBuf s, EHtmlEscapeMode hem); - void WriteComma(); - void PrintIndentation(bool closing); + void WriteComma(); + void PrintIndentation(bool closing); void PrintWhitespaces(size_t count, bool prependWithNewLine); - void WriteHexEscape(unsigned char c); - + void WriteHexEscape(unsigned char c); + void StackPush(EJsonEntity e); void StackPop(); void CheckAndPop(EJsonEntity e); @@ -146,23 +146,23 @@ namespace NJsonWriter { private: IOutputStream* Stream; - THolder<TStringStream> StringStream; + THolder<TStringStream> StringStream; typedef TVector<const TString*> TKeys; TKeys Keys; TVector<EJsonEntity> Stack; - bool NeedComma; + bool NeedComma; bool NeedNewline; - const EHtmlEscapeMode EscapeMode; - int IndentSpaces; + const EHtmlEscapeMode EscapeMode; + int IndentSpaces; bool WriteNanAsString; - }; - - // Please don't try to instantiate the classes declared below this point. - + }; + + // Please don't try to instantiate the classes declared below this point. + template <typename TOutContext> class TValueWriter { - public: + public: TOutContext WriteNull(); TOutContext WriteString(const TStringBuf&); TOutContext WriteString(const TStringBuf& s, EHtmlEscapeMode hem); @@ -177,82 +177,82 @@ namespace NJsonWriter { TOutContext WriteJsonValue(const NJson::TJsonValue* value, bool sortKeys = false); TOutContext UnsafeWriteValue(const TStringBuf&); - TValueContext BeginList(); - TPairContext BeginObject(); - - protected: - TValueWriter(TBuf& buf) - : Buf(buf) + TValueContext BeginList(); + TPairContext BeginObject(); + + protected: + TValueWriter(TBuf& buf) + : Buf(buf) { } - friend class TBuf; - - protected: - TBuf& Buf; - }; - - class TValueContext: public TValueWriter<TValueContext> { - public: - TBuf& EndList() { - return Buf.EndList(); - } + friend class TBuf; + + protected: + TBuf& Buf; + }; + + class TValueContext: public TValueWriter<TValueContext> { + public: + TBuf& EndList() { + return Buf.EndList(); + } TString Str() const { return Buf.Str(); } - private: - TValueContext(TBuf& buf) - : TValueWriter<TValueContext>(buf) + private: + TValueContext(TBuf& buf) + : TValueWriter<TValueContext>(buf) { } - friend class TBuf; - friend class TValueWriter<TValueContext>; - }; - - class TAfterColonContext: public TValueWriter<TPairContext> { - private: + friend class TBuf; + friend class TValueWriter<TValueContext>; + }; + + class TAfterColonContext: public TValueWriter<TPairContext> { + private: TAfterColonContext(TBuf& iBuf) : TValueWriter<TPairContext>(iBuf) { } - friend class TBuf; - friend class TPairContext; - }; - - class TPairContext { - public: + friend class TBuf; + friend class TPairContext; + }; + + class TPairContext { + public: TAfterColonContext WriteKey(const TStringBuf& s, EHtmlEscapeMode hem) { - return Buf.WriteKey(s, hem); - } + return Buf.WriteKey(s, hem); + } TAfterColonContext WriteKey(const TStringBuf& s) { - return Buf.WriteKey(s); - } + return Buf.WriteKey(s); + } TAfterColonContext UnsafeWriteKey(const TStringBuf& s) { return Buf.UnsafeWriteKey(s); } TAfterColonContext CompatWriteKeyWithoutQuotes(const TStringBuf& s) { - return Buf.CompatWriteKeyWithoutQuotes(s); - } + return Buf.CompatWriteKeyWithoutQuotes(s); + } TPairContext UnsafeWritePair(const TStringBuf& s) { - return Buf.UnsafeWritePair(s); - } - TBuf& EndObject() { - return Buf.EndObject(); - } - - private: - TPairContext(TBuf& buf) - : Buf(buf) + return Buf.UnsafeWritePair(s); + } + TBuf& EndObject() { + return Buf.EndObject(); + } + + private: + TPairContext(TBuf& buf) + : Buf(buf) { } - - friend class TBuf; - friend class TValueWriter<TPairContext>; - - private: - TBuf& Buf; - }; - + + friend class TBuf; + friend class TValueWriter<TPairContext>; + + private: + TBuf& Buf; + }; + #define JSON_VALUE_WRITER_WRAP(function, params, args) \ template <typename TOutContext> \ TOutContext TValueWriter<TOutContext>::function params { \ @@ -277,13 +277,13 @@ namespace NJsonWriter { template <typename TOutContext> TValueContext TValueWriter<TOutContext>::BeginList() { - return Buf.BeginList(); - } - + return Buf.BeginList(); + } + template <typename TOutContext> TPairContext TValueWriter<TOutContext>::BeginObject() { - return Buf.BeginObject(); - } + return Buf.BeginObject(); + } TString WrapJsonToCallback(const TBuf& buf, TStringBuf callback); -} +} diff --git a/library/cpp/json/writer/json_ut.cpp b/library/cpp/json/writer/json_ut.cpp index d6842bcc6d..9980555683 100644 --- a/library/cpp/json/writer/json_ut.cpp +++ b/library/cpp/json/writer/json_ut.cpp @@ -1,191 +1,191 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/system/sanitizers.h> - -#include "json.h" + +#include "json.h" #include <library/cpp/json/json_value.h> - + #include <limits> Y_UNIT_TEST_SUITE(JsonWriter) { Y_UNIT_TEST(Struct) { - NJsonWriter::TBuf w; - w.BeginList(); - w.BeginObject() - .WriteKey("key") - .WriteString("value") - .UnsafeWritePair("\"xk\":13") - .WriteKey("key2") - .BeginList() + NJsonWriter::TBuf w; + w.BeginList(); + w.BeginObject() + .WriteKey("key") + .WriteString("value") + .UnsafeWritePair("\"xk\":13") + .WriteKey("key2") + .BeginList() .BeginObject() .EndObject() .BeginObject() .EndObject() - .EndList() + .EndList() .EndObject(); - w.WriteInt(43); - w.UnsafeWriteValue("\"x\""); - w.WriteString("..."); - w.EndList(); - const char* exp = "[{\"key\":\"value\",\"xk\":13,\"key2\":[{},{}]},43,\"x\",\"...\"]"; - UNIT_ASSERT_EQUAL(w.Str(), exp); - } + w.WriteInt(43); + w.UnsafeWriteValue("\"x\""); + w.WriteString("..."); + w.EndList(); + const char* exp = "[{\"key\":\"value\",\"xk\":13,\"key2\":[{},{}]},43,\"x\",\"...\"]"; + UNIT_ASSERT_EQUAL(w.Str(), exp); + } Y_UNIT_TEST(EscapedString) { - NJsonWriter::TBuf w(NJsonWriter::HEM_ESCAPE_HTML); - w.WriteString(" \n \r \t \007 \b \f ' <tag> &ent; \"txt\" "); + NJsonWriter::TBuf w(NJsonWriter::HEM_ESCAPE_HTML); + w.WriteString(" \n \r \t \007 \b \f ' <tag> &ent; \"txt\" "); TString ws = w.Str(); - const char* exp = "\" \\n \\r \\t \\u0007 \\b \\f ' <tag> &ent; "txt" \""; - UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); - } + const char* exp = "\" \\n \\r \\t \\u0007 \\b \\f ' <tag> &ent; "txt" \""; + UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); + } Y_UNIT_TEST(UnescapedString) { - NJsonWriter::TBuf w; - w.WriteString(" \n \r \t \b \f '; -- <tag> &ent; \"txt\"", NJsonWriter::HEM_DONT_ESCAPE_HTML); + NJsonWriter::TBuf w; + w.WriteString(" \n \r \t \b \f '; -- <tag> &ent; \"txt\"", NJsonWriter::HEM_DONT_ESCAPE_HTML); TString ws = w.Str(); - const char* exp = "\" \\n \\r \\t \\b \\f \\u0027; -- \\u003Ctag\\u003E &ent; \\\"txt\\\"\""; - UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); - } + const char* exp = "\" \\n \\r \\t \\b \\f \\u0027; -- \\u003Ctag\\u003E &ent; \\\"txt\\\"\""; + UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); + } Y_UNIT_TEST(UnescapedChaining) { - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); - w.UnsafeWriteRawBytes("(", 1); - w.BeginList().WriteString("<>&'\\").BeginList(); - w.EndList().EndList(); + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); + w.UnsafeWriteRawBytes("(", 1); + w.BeginList().WriteString("<>&'\\").BeginList(); + w.EndList().EndList(); TString ws = w.Str(); - const char* exp = "([\"\\u003C\\u003E&\\u0027\\\\\",[]]"; - UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); - } + const char* exp = "([\"\\u003C\\u003E&\\u0027\\\\\",[]]"; + UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); + } Y_UNIT_TEST(Utf8) { TString ws = NJsonWriter::TBuf().WriteString("яЯ σΣ ש א").Str(); - const char* exp = "\"яЯ σΣ ש א\""; - UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); - } + const char* exp = "\"яЯ σΣ ש א\""; + UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); + } Y_UNIT_TEST(WrongObject) { - NJsonWriter::TBuf w; - w.BeginObject(); - UNIT_ASSERT_EXCEPTION(w.WriteString("hehe"), NJsonWriter::TError); - } + NJsonWriter::TBuf w; + w.BeginObject(); + UNIT_ASSERT_EXCEPTION(w.WriteString("hehe"), NJsonWriter::TError); + } Y_UNIT_TEST(WrongList) { - NJsonWriter::TBuf w; - w.BeginList(); - UNIT_ASSERT_EXCEPTION(w.WriteKey("hehe"), NJsonWriter::TError); - } + NJsonWriter::TBuf w; + w.BeginList(); + UNIT_ASSERT_EXCEPTION(w.WriteKey("hehe"), NJsonWriter::TError); + } Y_UNIT_TEST(Incomplete) { - NJsonWriter::TBuf w; - w.BeginList(); - UNIT_ASSERT_EXCEPTION(w.Str(), NJsonWriter::TError); - } + NJsonWriter::TBuf w; + w.BeginList(); + UNIT_ASSERT_EXCEPTION(w.Str(), NJsonWriter::TError); + } Y_UNIT_TEST(BareKey) { - NJsonWriter::TBuf w; - w.BeginObject() + NJsonWriter::TBuf w; + w.BeginObject() .CompatWriteKeyWithoutQuotes("p") .WriteInt(1) .CompatWriteKeyWithoutQuotes("n") .WriteInt(0) .EndObject(); TString ws = w.Str(); - const char* exp = "{p:1,n:0}"; - UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); - } + const char* exp = "{p:1,n:0}"; + UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); + } Y_UNIT_TEST(UnescapedStringInObject) { - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); - w.BeginObject().WriteKey("key").WriteString("</&>'").EndObject(); + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); + w.BeginObject().WriteKey("key").WriteString("</&>'").EndObject(); TString ws = w.Str(); - const char* exp = "{\"key\":\"\\u003C\\/&\\u003E\\u0027\"}"; - UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); - } + const char* exp = "{\"key\":\"\\u003C\\/&\\u003E\\u0027\"}"; + UNIT_ASSERT_STRINGS_EQUAL(ws.c_str(), exp); + } Y_UNIT_TEST(ForeignStreamStr) { - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML, &Cerr); - UNIT_ASSERT_EXCEPTION(w.Str(), NJsonWriter::TError); - } + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML, &Cerr); + UNIT_ASSERT_EXCEPTION(w.Str(), NJsonWriter::TError); + } Y_UNIT_TEST(ForeignStreamValue) { - TStringStream ss; - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML, &ss); - w.WriteInt(1543); - UNIT_ASSERT_STRINGS_EQUAL(ss.Str(), "1543"); - } + TStringStream ss; + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML, &ss); + w.WriteInt(1543); + UNIT_ASSERT_STRINGS_EQUAL(ss.Str(), "1543"); + } Y_UNIT_TEST(Indentation) { - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); - w.SetIndentSpaces(2); - w.BeginList() - .WriteInt(1) - .WriteString("hello") - .BeginObject() + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); + w.SetIndentSpaces(2); + w.BeginList() + .WriteInt(1) + .WriteString("hello") + .BeginObject() .WriteKey("abc") .WriteInt(3) .WriteKey("def") .WriteInt(4) - .EndObject() + .EndObject() .EndList(); - const char* exp = "[\n" - " 1,\n" - " \"hello\",\n" - " {\n" + const char* exp = "[\n" + " 1,\n" + " \"hello\",\n" + " {\n" " \"abc\":3,\n" " \"def\":4\n" - " }\n" - "]"; - UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); - } + " }\n" + "]"; + UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); + } Y_UNIT_TEST(WriteJsonValue) { - using namespace NJson; - TJsonValue val; - val.AppendValue(1); - val.AppendValue("2"); - val.AppendValue(3.5); - TJsonValue obj; - obj.InsertValue("key", TJsonValue("value")); - - val.AppendValue(obj); - val.AppendValue(TJsonValue(JSON_NULL)); - - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); - w.WriteJsonValue(&val); - - const char exp[] = "[1,\"2\",3.5,{\"key\":\"value\"},null]"; - UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); - } + using namespace NJson; + TJsonValue val; + val.AppendValue(1); + val.AppendValue("2"); + val.AppendValue(3.5); + TJsonValue obj; + obj.InsertValue("key", TJsonValue("value")); + + val.AppendValue(obj); + val.AppendValue(TJsonValue(JSON_NULL)); + + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); + w.WriteJsonValue(&val); + + const char exp[] = "[1,\"2\",3.5,{\"key\":\"value\"},null]"; + UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); + } Y_UNIT_TEST(WriteJsonValueSorted) { - using namespace NJson; - TJsonValue val; - val.InsertValue("1", TJsonValue(1)); - val.InsertValue("2", TJsonValue(2)); - - TJsonValue obj; - obj.InsertValue("zero", TJsonValue(0)); - obj.InsertValue("succ", TJsonValue(1)); - val.InsertValue("0", obj); - - NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); - w.WriteJsonValue(&val, true); - - const char exp[] = "{\"0\":{\"succ\":1,\"zero\":0},\"1\":1,\"2\":2}"; - UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); - } + using namespace NJson; + TJsonValue val; + val.InsertValue("1", TJsonValue(1)); + val.InsertValue("2", TJsonValue(2)); + + TJsonValue obj; + obj.InsertValue("zero", TJsonValue(0)); + obj.InsertValue("succ", TJsonValue(1)); + val.InsertValue("0", obj); + + NJsonWriter::TBuf w(NJsonWriter::HEM_DONT_ESCAPE_HTML); + w.WriteJsonValue(&val, true); + + const char exp[] = "{\"0\":{\"succ\":1,\"zero\":0},\"1\":1,\"2\":2}"; + UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); + } Y_UNIT_TEST(Unescaped) { - NJsonWriter::TBuf buf(NJsonWriter::HEM_UNSAFE); - buf.WriteString("</security>'"); - UNIT_ASSERT_STRINGS_EQUAL("\"</security>'\"", buf.Str()); - } + NJsonWriter::TBuf buf(NJsonWriter::HEM_UNSAFE); + buf.WriteString("</security>'"); + UNIT_ASSERT_STRINGS_EQUAL("\"</security>'\"", buf.Str()); + } Y_UNIT_TEST(LittleBobbyJsonp) { - NJsonWriter::TBuf buf; - buf.WriteString("hello\xe2\x80\xa8\xe2\x80\xa9stranger"); - UNIT_ASSERT_STRINGS_EQUAL("\"hello\\u2028\\u2029stranger\"", buf.Str()); - } + NJsonWriter::TBuf buf; + buf.WriteString("hello\xe2\x80\xa8\xe2\x80\xa9stranger"); + UNIT_ASSERT_STRINGS_EQUAL("\"hello\\u2028\\u2029stranger\"", buf.Str()); + } Y_UNIT_TEST(LittleBobbyInvalid) { - NJsonWriter::TBuf buf; - TStringBuf incomplete("\xe2\x80\xa8", 2); - buf.WriteString(incomplete); - // garbage in - garbage out - UNIT_ASSERT_STRINGS_EQUAL("\"\xe2\x80\"", buf.Str()); - } + NJsonWriter::TBuf buf; + TStringBuf incomplete("\xe2\x80\xa8", 2); + buf.WriteString(incomplete); + // garbage in - garbage out + UNIT_ASSERT_STRINGS_EQUAL("\"\xe2\x80\"", buf.Str()); + } Y_UNIT_TEST(OverlyZealous) { - NJsonWriter::TBuf buf; - buf.WriteString("—"); - UNIT_ASSERT_STRINGS_EQUAL("\"—\"", buf.Str()); - } + NJsonWriter::TBuf buf; + buf.WriteString("—"); + UNIT_ASSERT_STRINGS_EQUAL("\"—\"", buf.Str()); + } Y_UNIT_TEST(RelaxedEscaping) { - NJsonWriter::TBuf buf(NJsonWriter::HEM_RELAXED); - buf.WriteString("</>"); - UNIT_ASSERT_STRINGS_EQUAL("\"\\u003C/\\u003E\"", buf.Str()); - } + NJsonWriter::TBuf buf(NJsonWriter::HEM_RELAXED); + buf.WriteString("</>"); + UNIT_ASSERT_STRINGS_EQUAL("\"\\u003C/\\u003E\"", buf.Str()); + } Y_UNIT_TEST(FloatFormatting) { NJsonWriter::TBuf buf(NJsonWriter::HEM_DONT_ESCAPE_HTML); @@ -304,4 +304,4 @@ Y_UNIT_TEST_SUITE(JsonWriter) { // doesn't really matter UNIT_ASSERT(!p.empty()); } -} +} diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp index 26fa35eed3..c61e8d1dc4 100644 --- a/library/cpp/json/writer/json_value.cpp +++ b/library/cpp/json/writer/json_value.cpp @@ -1,5 +1,5 @@ #include "json_value.h" -#include "json.h" +#include "json.h" #include <util/generic/ymath.h> #include <util/generic/ylimits.h> @@ -161,7 +161,7 @@ namespace NJson { Value.Double = value; } - TJsonValue::TJsonValue(TString value) { + TJsonValue::TJsonValue(TString value) { SetType(JSON_STRING); Value.String = std::move(value); } @@ -1100,6 +1100,6 @@ namespace NJson { template <> void Out<NJson::TJsonValue>(IOutputStream& out, const NJson::TJsonValue& v) { - NJsonWriter::TBuf buf(NJsonWriter::HEM_DONT_ESCAPE_HTML, &out); - buf.WriteJsonValue(&v); + NJsonWriter::TBuf buf(NJsonWriter::HEM_DONT_ESCAPE_HTML, &out); + buf.WriteJsonValue(&v); } diff --git a/library/cpp/json/writer/json_value.h b/library/cpp/json/writer/json_value.h index b24029b8bb..3f0f50bc4c 100644 --- a/library/cpp/json/writer/json_value.h +++ b/library/cpp/json/writer/json_value.h @@ -48,7 +48,7 @@ namespace NJson { TJsonValue(long long value) noexcept; TJsonValue(unsigned long long value) noexcept; TJsonValue(double value) noexcept; - TJsonValue(TString value); + TJsonValue(TString value); TJsonValue(const char* value); template <class T> TJsonValue(const T*) = delete; diff --git a/library/cpp/json/writer/ya.make b/library/cpp/json/writer/ya.make index cfbab3c818..3989ff3504 100644 --- a/library/cpp/json/writer/ya.make +++ b/library/cpp/json/writer/ya.make @@ -1,4 +1,4 @@ -LIBRARY() +LIBRARY() OWNER( mvel @@ -9,12 +9,12 @@ OWNER( PEERDIR( library/cpp/json/common ) - -SRCS( - json_value.cpp - json.cpp -) - + +SRCS( + json_value.cpp + json.cpp +) + GENERATE_ENUM_SERIALIZATION(json_value.h) -END() +END() diff --git a/library/cpp/packers/region_packer.h b/library/cpp/packers/region_packer.h index 0e82475531..2c661cb5bc 100644 --- a/library/cpp/packers/region_packer.h +++ b/library/cpp/packers/region_packer.h @@ -1,18 +1,18 @@ #pragma once - + #include "packers.h" #include <util/generic/array_ref.h> - -// Stores an array of PODs in the trie (copying them with memcpy). -// Byte order and alignment are your problem. - + +// Stores an array of PODs in the trie (copying them with memcpy). +// Byte order and alignment are your problem. + template <class TRecord> class TRegionPacker { public: typedef TArrayRef<TRecord> TRecords; - void UnpackLeaf(const char* p, TRecords& result) const { + void UnpackLeaf(const char* p, TRecords& result) const { size_t len; NPackers::TIntegralPacker<size_t>().UnpackLeaf(p, len); size_t start = NPackers::TIntegralPacker<size_t>().SkipLeaf(p); @@ -32,7 +32,7 @@ public: return NPackers::TIntegralPacker<size_t>().MeasureLeaf(len) + len * sizeof(TRecord); } - size_t SkipLeaf(const char* p) const { + size_t SkipLeaf(const char* p) const { size_t result = NPackers::TIntegralPacker<size_t>().SkipLeaf(p); size_t len; NPackers::TIntegralPacker<size_t>().UnpackLeaf(p, len); diff --git a/library/cpp/packers/ut/region_packer_ut.cpp b/library/cpp/packers/ut/region_packer_ut.cpp index 0da6a8b773..0cb08ccf65 100644 --- a/library/cpp/packers/ut/region_packer_ut.cpp +++ b/library/cpp/packers/ut/region_packer_ut.cpp @@ -1,40 +1,40 @@ -#include "region_packer.h" +#include "region_packer.h" #include <library/cpp/testing/unittest/registar.h> - -template <typename TValue> -void TestPacker() { - TValue values[] = {1, 2, 3, 42}; + +template <typename TValue> +void TestPacker() { + TValue values[] = {1, 2, 3, 42}; TString buffer; - - TRegionPacker<TValue> p; - + + TRegionPacker<TValue> p; + using TValues = TArrayRef<TValue>; TValues valueRegion = TValues(values, Y_ARRAY_SIZE(values)); - size_t sz = p.MeasureLeaf(valueRegion); - UNIT_ASSERT_VALUES_EQUAL(sz, 1 + sizeof(values)); - - buffer.resize(sz); - p.PackLeaf(buffer.begin(), valueRegion, sz); - UNIT_ASSERT_VALUES_EQUAL(buffer[0], 4); - + size_t sz = p.MeasureLeaf(valueRegion); + UNIT_ASSERT_VALUES_EQUAL(sz, 1 + sizeof(values)); + + buffer.resize(sz); + p.PackLeaf(buffer.begin(), valueRegion, sz); + UNIT_ASSERT_VALUES_EQUAL(buffer[0], 4); + p.UnpackLeaf(buffer.data(), valueRegion); UNIT_ASSERT_EQUAL(valueRegion.data(), (const TValue*)(buffer.begin() + 1)); UNIT_ASSERT_EQUAL(valueRegion.size(), Y_ARRAY_SIZE(values)); UNIT_ASSERT_EQUAL(0, memcmp(values, valueRegion.data(), sizeof(values))); -} - +} + Y_UNIT_TEST_SUITE(RegionPacker) { Y_UNIT_TEST(Test0) { - TestPacker<char>(); - TestPacker<signed char>(); - TestPacker<unsigned char>(); - TestPacker<i8>(); - TestPacker<ui8>(); - TestPacker<i16>(); - TestPacker<ui16>(); - TestPacker<i32>(); - TestPacker<ui32>(); - TestPacker<i64>(); - TestPacker<ui64>(); - } -} + TestPacker<char>(); + TestPacker<signed char>(); + TestPacker<unsigned char>(); + TestPacker<i8>(); + TestPacker<ui8>(); + TestPacker<i16>(); + TestPacker<ui16>(); + TestPacker<i32>(); + TestPacker<ui32>(); + TestPacker<i64>(); + TestPacker<ui64>(); + } +} diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index fc2d21cc18..4c33498dfa 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -8,7 +8,7 @@ #include <util/stream/input.h> #include <util/stream/str.h> -#include <util/stream/mem.h> +#include <util/stream/mem.h> namespace google { namespace protobuf { @@ -218,5 +218,5 @@ namespace NProtobufJson { T Json2Proto(const char* ptr, const TJson2ProtoConfig& config = TJson2ProtoConfig()) { return Json2Proto<T>(TStringBuf(ptr), config); } - + } diff --git a/library/cpp/sighandler/async_signals_handler.cpp b/library/cpp/sighandler/async_signals_handler.cpp index f1b5a09c43..00ce1c18fb 100644 --- a/library/cpp/sighandler/async_signals_handler.cpp +++ b/library/cpp/sighandler/async_signals_handler.cpp @@ -215,14 +215,14 @@ void SetAsyncSignalHandler(int, TAutoPtr<TEventHandler>) { #endif namespace { - template <typename TFunc> - class TFunctionEventHandler: public TEventHandler { - TFunc Func; + template <typename TFunc> + class TFunctionEventHandler: public TEventHandler { + TFunc Func; public: - TFunctionEventHandler(TFunc func) { - if (func) - Func = func; + TFunctionEventHandler(TFunc func) { + if (func) + Func = func; } int Handle(int signum) override { @@ -238,8 +238,8 @@ namespace { void SetAsyncSignalHandler(int signum, void (*handler)(int)) { SetAsyncSignalHandler(signum, new TFunctionEventHandler<void (*)(int)>(handler)); } - + void SetAsyncSignalFunction(int signum, std::function<void(int)> func) { typedef std::function<void(int)> TFunc; - SetAsyncSignalHandler(signum, new TFunctionEventHandler<TFunc>(func)); -} + SetAsyncSignalHandler(signum, new TFunctionEventHandler<TFunc>(func)); +} diff --git a/library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h b/library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h index 5c8e4adb14..8a240bfed8 100644 --- a/library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h +++ b/library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h @@ -141,16 +141,16 @@ namespace NLevenshtein { template <class TStringType> size_t Distance(const TStringType& str1, const TStringType& str2) { - TEditChain editChain; - GetEditChain(str1, str2, editChain); - size_t result = 0; - for (auto edit : editChain) { - if (IsImportantEditMove(edit)) - result++; - } - return result; - } - + TEditChain editChain; + GetEditChain(str1, str2, editChain); + size_t result = 0; + for (auto edit : editChain) { + if (IsImportantEditMove(edit)) + result++; + } + return result; + } + /// Calculates substrings to be replaced for str1->str2 transformation struct TReplacement { int CorrectOffset, CorrectLength, MisspelledOffset, MisspelledLength; diff --git a/library/cpp/string_utils/levenshtein_diff/levenshtein_diff_ut.cpp b/library/cpp/string_utils/levenshtein_diff/levenshtein_diff_ut.cpp index c5f2165854..cf0f78637f 100644 --- a/library/cpp/string_utils/levenshtein_diff/levenshtein_diff_ut.cpp +++ b/library/cpp/string_utils/levenshtein_diff/levenshtein_diff_ut.cpp @@ -1,7 +1,7 @@ #include "levenshtein_diff.h" - + #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/string.h> namespace { @@ -28,8 +28,8 @@ Y_UNIT_TEST_SUITE(Levenstein) { Y_UNIT_TEST(Distance) { UNIT_ASSERT_VALUES_EQUAL(NLevenshtein::Distance(TStringBuf("hello"), TStringBuf("hulloah")), 3); UNIT_ASSERT_VALUES_EQUAL(NLevenshtein::Distance(TStringBuf("yeoman"), TStringBuf("yo man")), 2); - } -} + } +} Y_UNIT_TEST_SUITE(WeightedLevenstein) { Y_UNIT_TEST(EqualStrings) { diff --git a/library/cpp/string_utils/levenshtein_diff/ut/ya.make b/library/cpp/string_utils/levenshtein_diff/ut/ya.make index 79130d6264..a3b9b8fea5 100644 --- a/library/cpp/string_utils/levenshtein_diff/ut/ya.make +++ b/library/cpp/string_utils/levenshtein_diff/ut/ya.make @@ -1,9 +1,9 @@ UNITTEST_FOR(library/cpp/string_utils/levenshtein_diff) -OWNER(myltsev) - +OWNER(myltsev) + SRCS( levenshtein_diff_ut.cpp ) - -END() + +END() diff --git a/library/cpp/string_utils/ya.make b/library/cpp/string_utils/ya.make index c802c438dc..cd731bda95 100644 --- a/library/cpp/string_utils/ya.make +++ b/library/cpp/string_utils/ya.make @@ -1,4 +1,4 @@ -RECURSE( +RECURSE( ascii_encode ascii_encode/ut base64 @@ -33,5 +33,5 @@ RECURSE( tskv_format/fuzz url url/ut - ztstrbuf -) + ztstrbuf +) diff --git a/library/cpp/string_utils/ztstrbuf/ya.make b/library/cpp/string_utils/ztstrbuf/ya.make index 10bdad091f..28b3f32f58 100644 --- a/library/cpp/string_utils/ztstrbuf/ya.make +++ b/library/cpp/string_utils/ztstrbuf/ya.make @@ -1,9 +1,9 @@ -LIBRARY() - -OWNER(myltsev) - -SRCS( - ztstrbuf.cpp -) - -END() +LIBRARY() + +OWNER(myltsev) + +SRCS( + ztstrbuf.cpp +) + +END() diff --git a/library/cpp/string_utils/ztstrbuf/ztstrbuf.cpp b/library/cpp/string_utils/ztstrbuf/ztstrbuf.cpp index 98f6cead1c..4a7269ff4a 100644 --- a/library/cpp/string_utils/ztstrbuf/ztstrbuf.cpp +++ b/library/cpp/string_utils/ztstrbuf/ztstrbuf.cpp @@ -1,8 +1,8 @@ -#include "ztstrbuf.h" - -#include <util/stream/output.h> - -template <> -void Out<TZtStringBuf>(IOutputStream& os, const TZtStringBuf& sb) { - os << static_cast<const TStringBuf&>(sb); -} +#include "ztstrbuf.h" + +#include <util/stream/output.h> + +template <> +void Out<TZtStringBuf>(IOutputStream& os, const TZtStringBuf& sb) { + os << static_cast<const TStringBuf&>(sb); +} diff --git a/library/cpp/string_utils/ztstrbuf/ztstrbuf.h b/library/cpp/string_utils/ztstrbuf/ztstrbuf.h index 04c509bcc5..5fab768d8c 100644 --- a/library/cpp/string_utils/ztstrbuf/ztstrbuf.h +++ b/library/cpp/string_utils/ztstrbuf/ztstrbuf.h @@ -1,36 +1,36 @@ -#pragma once - -#include <util/generic/strbuf.h> +#pragma once + +#include <util/generic/strbuf.h> #include <util/generic/string.h> - -/* - * Zero-terminated string view. - * - * Has a c_str() for use with system/cstdlib calls (like TString) - * but can be constructed from a string literal or command-line arg - * without memory allocation (like TStringBuf). - * - * Use it to reference filenames, thread names, string formats etc. - */ - -class TZtStringBuf: public TStringBuf { -public: - TZtStringBuf(const char* s) - : TStringBuf(s) - { - } - - TZtStringBuf(const TString& s) - : TStringBuf(s) - { - } - - TZtStringBuf() - : TZtStringBuf(TString{}) - { - } - - const char* c_str() const { - return data(); - } -}; + +/* + * Zero-terminated string view. + * + * Has a c_str() for use with system/cstdlib calls (like TString) + * but can be constructed from a string literal or command-line arg + * without memory allocation (like TStringBuf). + * + * Use it to reference filenames, thread names, string formats etc. + */ + +class TZtStringBuf: public TStringBuf { +public: + TZtStringBuf(const char* s) + : TStringBuf(s) + { + } + + TZtStringBuf(const TString& s) + : TStringBuf(s) + { + } + + TZtStringBuf() + : TZtStringBuf(TString{}) + { + } + + const char* c_str() const { + return data(); + } +}; diff --git a/library/cpp/xml/document/README b/library/cpp/xml/document/README index c7396aec30..b2649523d8 100644 --- a/library/cpp/xml/document/README +++ b/library/cpp/xml/document/README @@ -1,20 +1,20 @@ -A wrapper around the DOM interface of libxml2. - -The standard way to use it is as follows: - +A wrapper around the DOM interface of libxml2. + +The standard way to use it is as follows: + #include <library/cpp/xml/document/xml-document.h> - ... - - // open a document - NXml::TDocument xml("filename.xml"); - - // get a nodeset from an XPath query - NXml::TConstNodes nodes = xml.Root().Nodes("xpath/expression/here"); - - // iterate over the nodeset - for (size_t i = 0; i < nodes.size(); ++i) { + ... + + // open a document + NXml::TDocument xml("filename.xml"); + + // get a nodeset from an XPath query + NXml::TConstNodes nodes = xml.Root().Nodes("xpath/expression/here"); + + // iterate over the nodeset + for (size_t i = 0; i < nodes.size(); ++i) { using namespace NXml; - TConstNode& node = nodes[i]; + TConstNode& node = nodes[i]; // query node TString name = node.Name(); TString lang = node.Attr<TString>("lang"); @@ -26,8 +26,8 @@ The standard way to use it is as follows: node.SetAttr("x", 2); node.SetValue(5); node.AddText(" apples"); - } - + } + // edit documents with copy-paste NXml::TDocument xml2("<xpath><node/></xpath>", NXml::TDocument::String); NXml::TNode place = xml2.Root().Node("xpath/node"); @@ -39,4 +39,4 @@ The standard way to use it is as follows: TString modifiedDoc = xml2.ToString("ISO-8559-1"); -See xml-document_ut.cpp for more examples. +See xml-document_ut.cpp for more examples. diff --git a/library/cpp/xml/document/libxml-guards.h b/library/cpp/xml/document/libxml-guards.h index 619ca23ce7..4188cecff1 100644 --- a/library/cpp/xml/document/libxml-guards.h +++ b/library/cpp/xml/document/libxml-guards.h @@ -2,10 +2,10 @@ #include <library/cpp/xml/init/ptr.h> #include <util/generic/ptr.h> -#include <libxml/xmlstring.h> -#include <libxml/tree.h> -#include <libxml/xpath.h> -#include <libxml/uri.h> +#include <libxml/xmlstring.h> +#include <libxml/tree.h> +#include <libxml/xpath.h> +#include <libxml/uri.h> #include <libxml/xmlsave.h> namespace NXml { @@ -36,15 +36,15 @@ namespace NXml { } - typedef TxmlXPathContextPtr TXPathContextPtr; - typedef TxmlXPathObjectPtr TXPathObjectPtr; + typedef TxmlXPathContextPtr TXPathContextPtr; + typedef TxmlXPathObjectPtr TXPathObjectPtr; typedef TAutoPtr<char, NDetail::TSignedCharPtrTraits> TSignedCharPtr; typedef TAutoPtr<xmlChar, NDetail::TCharPtrTraits> TCharPtr; typedef TxmlDocHolder TDocHolder; - typedef TxmlURIPtr TURIPtr; - typedef TxmlNodePtr TNodePtr; + typedef TxmlURIPtr TURIPtr; + typedef TxmlNodePtr TNodePtr; typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr; - typedef TxmlParserCtxtPtr TParserCtxtPtr; + typedef TxmlParserCtxtPtr TParserCtxtPtr; typedef TAutoPtr<xmlSaveCtxt, NDetail::TSaveCtxtPtrTraits> TSaveCtxtPtr; } diff --git a/library/cpp/xml/document/node-attr.h b/library/cpp/xml/document/node-attr.h index 2642eb96f9..6e74403943 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -1,24 +1,24 @@ #pragma once #include "xml-document-decl.h" -#include "libxml-guards.h" +#include "libxml-guards.h" #include <util/stream/str.h> #include <util/string/cast.h> namespace NXml { #define THROW(x, y) ythrow yexception() << #x << ": " << y - // libxml defines unsigned char -> xmlChar, - // and all functions use xmlChar. + // libxml defines unsigned char -> xmlChar, + // and all functions use xmlChar. inline static const char* CAST2CHAR(const xmlChar* x) { return reinterpret_cast<const char*>(x); } inline static const xmlChar* XMLCHAR(const char* x) { return reinterpret_cast<const xmlChar*>(x); } - + template <class T> - void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const { + void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const { try { res = FromString<T>(CAST2CHAR(value.Get())); } catch (TFromStringException&) { @@ -27,16 +27,16 @@ namespace NXml { } template <> - inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const { + inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const { TString tmp(CAST2CHAR(value.Get())); res.swap(tmp); } template <class T> - T TNode::Attr(TZtStringBuf name) const { - TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + T TNode::Attr(TZtStringBuf name) const { + TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!value) { - THROW(AttributeNotFound, Path() << "@" << name); + THROW(AttributeNotFound, Path() << "@" << name); } T t; @@ -45,8 +45,8 @@ namespace NXml { } template <class T> - T TNode::Attr(TZtStringBuf name, const T& defvalue) const { - TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + T TNode::Attr(TZtStringBuf name, const T& defvalue) const { + TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { return defvalue; } @@ -57,18 +57,18 @@ namespace NXml { } template <class T> - void TNode::Attr(TZtStringBuf name, T& value) const { - TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + void TNode::Attr(TZtStringBuf name, T& value) const { + TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { - THROW(AttributeNotFound, Path() << name); + THROW(AttributeNotFound, Path() << name); } AttrInternal(attr, value, name); } template <class T> - void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { - TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { + TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { value = defvalue; @@ -80,10 +80,10 @@ namespace NXml { template <class T> T TNode::Value() const { if (!NodePointer || xmlIsBlankNode(NodePointer)) { - THROW(NodeIsBlank, Path()); + THROW(NodeIsBlank, Path()); } - TCharPtr val(xmlNodeGetContent(NodePointer)); + TCharPtr val(xmlNodeGetContent(NodePointer)); T t; AttrInternal(val, t, this->Name()); return t; @@ -95,26 +95,26 @@ namespace NXml { return defvalue; } - TCharPtr val(xmlNodeGetContent(NodePointer)); + TCharPtr val(xmlNodeGetContent(NodePointer)); T t; AttrInternal(val, t, this->Name()); return t; } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type - TNode::SetValue(const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type + TNode::SetValue(const T& value) { TStringStream ss; ss << value; - SetValue(ss.Str()); - } - - inline void TNode::SetValue(TStringBuf value) { + SetValue(ss.Str()); + } + + inline void TNode::SetValue(TStringBuf value) { xmlNodeSetContent(NodePointer, XMLCHAR("")); - xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size()); + xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size()); } - inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) { + inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); if (!attr) { @@ -127,14 +127,14 @@ namespace NXml { } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type - TNode::SetAttr(TZtStringBuf name, const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type + TNode::SetAttr(TZtStringBuf name, const T& value) { TStringStream ss; ss << value; - SetAttr(name, TZtStringBuf(ss.Str())); + SetAttr(name, TZtStringBuf(ss.Str())); } - inline void TNode::SetAttr(TZtStringBuf name) { + inline void TNode::SetAttr(TZtStringBuf name) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr); if (!attr) { @@ -144,7 +144,7 @@ namespace NXml { } } - inline void TNode::DelAttr(TZtStringBuf name) { + inline void TNode::DelAttr(TZtStringBuf name) { if (xmlUnsetProp(NodePointer, XMLCHAR(name.c_str())) < 0) THROW(XmlException, "Can't delete node attribute <" << name @@ -152,14 +152,14 @@ namespace NXml { } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type - TNode::AddChild(TZtStringBuf name, const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type + TNode::AddChild(TZtStringBuf name, const T& value) { TStringStream ss; ss << value; - return AddChild(name, TZtStringBuf(ss.Str())); + return AddChild(name, TZtStringBuf(ss.Str())); } - inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { + inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { if (IsNull()) { THROW(XmlException, "addChild [name=" << name << ", value=" << value << "]: can't add child to null node"); @@ -179,18 +179,18 @@ namespace NXml { << "]: xmlNewTextChild returned NULL"); } - return TNode(DocPointer, child); + return TNode(DocPointer, child); } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type - TNode::AddText(const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type + TNode::AddText(const T& value) { TStringStream ss; ss << value; return AddText(ss.Str()); } - inline TNode TNode::AddText(TStringBuf value) { + inline TNode TNode::AddText(TStringBuf value) { if (IsNull()) { THROW(XmlException, "addChild [value=" << value << "]: can't add child to null node"); diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index 7b900939ab..bfda1fb7e6 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -1,19 +1,19 @@ #pragma once -#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> - +#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> + #include <util/generic/string.h> #include <util/generic/vector.h> #include <util/stream/output.h> #include <util/stream/str.h> #include <algorithm> -#include "libxml-guards.h" +#include "libxml-guards.h" namespace NXml { - class TNode; + class TNode; class TConstNodes; - class TConstNode; + class TConstNode; using TXPathContext = xmlXPathContext; @@ -24,11 +24,11 @@ namespace NXml { String, RootName, }; - + public: /** - * create TDocument - * @param source: filename, XML string, or name for the root element (depends on @src) + * create TDocument + * @param source: filename, XML string, or name for the root element (depends on @src) * @param src: source type: File | String | RootName * throws if file not found or cannot be parsed */ @@ -44,10 +44,10 @@ namespace NXml { /** * get root element */ - TNode Root(); - TConstNode Root() const; + TNode Root(); + TConstNode Root() const; - void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const { + void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const { int bufferSize = 0; xmlChar* xmlBuff = nullptr; const char* encoding = enc.size() ? enc.data() : Doc->encoding ? nullptr : "UTF-8"; @@ -56,14 +56,14 @@ namespace NXml { stream.Write(xmlBuff, bufferSize); } - TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const { + TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const { TStringStream s; Save(s, enc, shouldFormat); return s.Str(); } void Swap(TDocument& that) { - std::swap(this->Doc, that.Doc); + std::swap(this->Doc, that.Doc); } xmlDocPtr GetImpl() { @@ -72,7 +72,7 @@ namespace NXml { private: void ParseFile(const TString& file); - void ParseString(TZtStringBuf xml); + void ParseString(TZtStringBuf xml); TDocument(TDocHolder doc) : Doc(std::move(doc)) @@ -111,7 +111,7 @@ namespace NXml { * get node by id * @param number: node id */ - TConstNode operator[](size_t number) const; + TConstNode operator[](size_t number) const; /** * get number of nodes @@ -123,50 +123,50 @@ namespace NXml { return SizeValue; } - struct TNodeIter { - const TConstNodes& Nodes; - size_t Index; + struct TNodeIter { + const TConstNodes& Nodes; + size_t Index; TConstNode operator*() const; bool operator==(const TNodeIter& other) const { - return Index == other.Index; - } + return Index == other.Index; + } bool operator!=(const TNodeIter& other) const { - return !(*this == other); - } - TNodeIter operator++() { - Index++; - return *this; - } - }; + return !(*this == other); + } + TNodeIter operator++() { + Index++; + return *this; + } + }; TNodeIter begin() const { return TNodeIter{*this, 0}; } TNodeIter end() const { return TNodeIter{*this, size()}; } - + private: friend class TDocument; - friend class TConstNode; - friend class TNode; + friend class TConstNode; + friend class TNode; TConstNodes(xmlDoc* doc, TXPathObjectPtr obj); - size_t SizeValue; - xmlDoc* Doc; - TXPathObjectPtr Obj; + size_t SizeValue; + xmlDoc* Doc; + TXPathObjectPtr Obj; }; class TNode { public: friend class TDocument; - friend class TConstNode; + friend class TConstNode; friend class TTextReader; /** * check if node is null */ - bool IsNull() const; + bool IsNull() const; /** * check if node is element node @@ -186,15 +186,15 @@ namespace NXml { TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const; /** - * get all element nodes matching given xpath expression + * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; + TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; /** * get all element nodes matching given xpath expression @@ -205,37 +205,37 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; + TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; + + /** + * get all nodes matching given xpath expression + * @param xpath: xpath expression + * @param quiet: don't throw exception if zero nodes found + * @param ns: explicitly specify XML namespaces to use and their prefixes + */ + TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; /** - * get all nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found - * @param ns: explicitly specify XML namespaces to use and their prefixes - */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; - - /** * get all nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ctxt: reusable xpath context */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; + TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** - * get the first element node matching given xpath expression + * get the first element node matching given xpath expression * @param xpath: path to node (from current node) * @param quiet: don't throw exception if node not found, * return null node (@see IsNull()) * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ /// @todo: quiet should be default, empty nodeset is not an error - TNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()); - TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; + TNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()); + TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; /** * get the first element node matching given xpath expression @@ -247,8 +247,8 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt); - TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; + TNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt); + TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** * get node first child @@ -256,8 +256,8 @@ namespace NXml { * @note if name is empty, returns the first child node of type "element" * @note returns null node if no child found */ - TNode FirstChild(TZtStringBuf name); - TConstNode FirstChild(TZtStringBuf name) const; + TNode FirstChild(TZtStringBuf name); + TConstNode FirstChild(TZtStringBuf name) const; TNode FirstChild(); TConstNode FirstChild() const; @@ -275,8 +275,8 @@ namespace NXml { * @note if name is empty, returns the next sibling node of type "element" * @node returns null node if no neighbour found */ - TNode NextSibling(TZtStringBuf name); - TConstNode NextSibling(TZtStringBuf name) const; + TNode NextSibling(TZtStringBuf name); + TConstNode NextSibling(TZtStringBuf name) const; TNode NextSibling(); TConstNode NextSibling() const; @@ -286,7 +286,7 @@ namespace NXml { * @param name: child name * returns new empty node */ - TNode AddChild(TZtStringBuf name); + TNode AddChild(TZtStringBuf name); /** * create child node with given value @@ -294,11 +294,11 @@ namespace NXml { * @param value: node value */ template <class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type - AddChild(TZtStringBuf name, const T& value); + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type + AddChild(TZtStringBuf name, const T& value); + + TNode AddChild(TZtStringBuf name, TZtStringBuf value); - TNode AddChild(TZtStringBuf name, TZtStringBuf value); - /** * add child node, making recursive copy of original * @param node: node to copy from @@ -312,18 +312,18 @@ namespace NXml { * @param value: node value */ template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type - AddText(const T& value); + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type + AddText(const T& value); + + TNode AddText(TStringBuf value); - TNode AddText(TStringBuf value); - /** * get node attribute * @param name: attribute name * throws exception if attribute not found */ template <class T> - T Attr(TZtStringBuf name) const; + T Attr(TZtStringBuf name) const; /** * get node attribute @@ -331,7 +331,7 @@ namespace NXml { * returns default value if attribute not found */ template <class T> - T Attr(TZtStringBuf name, const T& defvalue) const; + T Attr(TZtStringBuf name, const T& defvalue) const; /** * get node attribute @@ -340,7 +340,7 @@ namespace NXml { * throws exception if attribute not found */ template <class T> - void Attr(TZtStringBuf name, T& value) const; + void Attr(TZtStringBuf name, T& value) const; /** * get node attribute @@ -350,14 +350,14 @@ namespace NXml { * returns default value if attribute not found, attr value otherwise */ template <class T> - void Attr(TZtStringBuf name, T& value, const T& defvalue) const; + void Attr(TZtStringBuf name, T& value, const T& defvalue) const; /** * get node value (text) * @throws exception if node is blank */ template <class T> - T Value() const; + T Value() const; /** * get node value @@ -365,37 +365,37 @@ namespace NXml { * returns default value if node is blank */ template <class T> - T Value(const T& defvalue) const; + T Value(const T& defvalue) const; /** * set node value * @param value: new text value */ template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type - SetValue(const T& value); + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type + SetValue(const T& value); + + void SetValue(TStringBuf value); - void SetValue(TStringBuf value); - /** * set/reset node attribute value, * if attribute does not exist, it'll be created * @param name: attribute name * @param value: attribute value */ - template<class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type - SetAttr(TZtStringBuf name, const T& value); + template<class T> + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type + SetAttr(TZtStringBuf name, const T& value); + + void SetAttr(TZtStringBuf name, TZtStringBuf value); - void SetAttr(TZtStringBuf name, TZtStringBuf value); + void SetAttr(TZtStringBuf name); - void SetAttr(TZtStringBuf name); - /** * delete node attribute * @param name: attribute name */ - void DelAttr(TZtStringBuf name); + void DelAttr(TZtStringBuf name); /** * set node application data @@ -421,13 +421,13 @@ namespace NXml { /** * get node xml representation */ - TString ToString(TZtStringBuf enc = "") const { + TString ToString(TZtStringBuf enc = "") const { TStringStream s; Save(s, enc); return s.Str(); } - void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; - void SaveAsHtml(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; + void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; + void SaveAsHtml(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; /** * get pointer to internal node @@ -448,7 +448,7 @@ namespace NXml { /** * constructs null node */ - TNode() + TNode() : NodePointer(nullptr) , DocPointer(nullptr) { @@ -457,33 +457,33 @@ namespace NXml { private: friend class TConstNodes; - TNode(xmlDoc* doc, xmlNode* node) - : NodePointer(node) - , DocPointer(doc) + TNode(xmlDoc* doc, xmlNode* node) + : NodePointer(node) + , DocPointer(doc) { } - TNode Find(xmlNode* start, TZtStringBuf name); + TNode Find(xmlNode* start, TZtStringBuf name); template <class T> - void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; + void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; - void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; + void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; - xmlNode* NodePointer; - xmlDoc* DocPointer; + xmlNode* NodePointer; + xmlDoc* DocPointer; }; class TConstNode { public: friend class TDocument; friend class TConstNodes; - friend class TNode; + friend class TNode; /** * check if node is null */ bool IsNull() const { - return ActualNode.IsNull(); + return ActualNode.IsNull(); } bool IsElementNode() const { @@ -491,9 +491,9 @@ namespace NXml { } TConstNode Parent() const { - return ActualNode.Parent(); - } - + return ActualNode.Parent(); + } + /** * Create xpath context to be used later for fast xpath evaluation. * @param nss: explicitly specify XML namespaces to use and their prefixes @@ -503,16 +503,16 @@ namespace NXml { } /** - * get all element nodes matching given xpath expression + * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { - return ActualNode.Nodes(xpath, quiet, ns); + TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { + return ActualNode.Nodes(xpath, quiet, ns); } /** @@ -524,42 +524,42 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return ActualNode.Nodes(xpath, quiet, ctxt); } /** - * get all nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found + * get all nodes matching given xpath expression + * @param xpath: xpath expression + * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes - */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { - return ActualNode.XPath(xpath, quiet, ns); - } - - /** + */ + TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { + return ActualNode.XPath(xpath, quiet, ns); + } + + /** * get all nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ctxt: reusable xpath context */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return ActualNode.XPath(xpath, quiet, ctxt); } /** - * get the first element node matching given xpath expression + * get the first element node matching given xpath expression * @param xpath: path to node (from current node) * @param quiet: don't throw exception if node not found, * return null node (@see IsNull()) * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ - TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { - return ActualNode.Node(xpath, quiet, ns); + TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { + return ActualNode.Node(xpath, quiet, ns); } /** @@ -572,12 +572,12 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return ActualNode.Node(xpath, quiet, ctxt); } - TConstNode FirstChild(TZtStringBuf name) const { - return ActualNode.FirstChild(name); + TConstNode FirstChild(TZtStringBuf name) const { + return ActualNode.FirstChild(name); } TConstNode FirstChild() const { @@ -589,8 +589,8 @@ namespace NXml { * @param name: neighbour name * throws exception if no neighbour found */ - TConstNode NextSibling(TZtStringBuf name) const { - return ActualNode.NextSibling(name); + TConstNode NextSibling(TZtStringBuf name) const { + return ActualNode.NextSibling(name); } TConstNode NextSibling() const { @@ -603,8 +603,8 @@ namespace NXml { * throws exception if attribute not found */ template <class T> - T Attr(TZtStringBuf name) const { - return ActualNode.Attr<T>(name); + T Attr(TZtStringBuf name) const { + return ActualNode.Attr<T>(name); } /** @@ -613,8 +613,8 @@ namespace NXml { * returns default value if attribute not found */ template <class T> - T Attr(TZtStringBuf name, const T& defvalue) const { - return ActualNode.Attr(name, defvalue); + T Attr(TZtStringBuf name, const T& defvalue) const { + return ActualNode.Attr(name, defvalue); } /** @@ -624,8 +624,8 @@ namespace NXml { * throws exception if attribute not found */ template <class T> - void Attr(TZtStringBuf name, T& value) const { - return ActualNode.Attr(name, value); + void Attr(TZtStringBuf name, T& value) const { + return ActualNode.Attr(name, value); } /** @@ -636,8 +636,8 @@ namespace NXml { * returns default value if attribute not found, attr value otherwise */ template <class T> - void Attr(TZtStringBuf name, T& value, const T& defvalue) const { - return ActualNode.Attr(name, value, defvalue); + void Attr(TZtStringBuf name, T& value, const T& defvalue) const { + return ActualNode.Attr(name, value, defvalue); } /** @@ -646,7 +646,7 @@ namespace NXml { */ template <class T> T Value() const { - return ActualNode.Value<T>(); + return ActualNode.Value<T>(); } /** @@ -656,7 +656,7 @@ namespace NXml { */ template <class T> T Value(const T& defvalue) const { - return ActualNode.Value(defvalue); + return ActualNode.Value(defvalue); } /** @@ -691,28 +691,28 @@ namespace NXml { * get node xpath */ TString Path() const { - return ActualNode.Path(); + return ActualNode.Path(); } /** * get node xml representation */ - TString ToString(TZtStringBuf enc = "") const { + TString ToString(TZtStringBuf enc = "") const { return ActualNode.ToString(enc); } TConstNode() = default; - TConstNode(TNode node) - : ActualNode(node) + TConstNode(TNode node) + : ActualNode(node) { } TNode ConstCast() const { - return ActualNode; - } - + return ActualNode; + } + private: - TNode ActualNode; + TNode ActualNode; }; } diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp index 23c16cf495..18a554d732 100644 --- a/library/cpp/xml/document/xml-document.cpp +++ b/library/cpp/xml/document/xml-document.cpp @@ -75,7 +75,7 @@ namespace NXml { Doc = std::move(doc); } - void TDocument::ParseString(TZtStringBuf xml) { + void TDocument::ParseString(TZtStringBuf xml) { TParserCtxtPtr pctx(xmlNewParserCtxt()); if (pctx.Get() == nullptr) THROW(XmlException, "Can't create parser context"); @@ -89,19 +89,19 @@ namespace NXml { } TNode TDocument::Root() { - xmlNode* r = xmlDocGetRootElement(Doc.Get()); + xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) THROW(XmlException, "TDocument hasn't root element"); - return TNode(Doc.Get(), r); + return TNode(Doc.Get(), r); } TConstNode TDocument::Root() const { - xmlNode* r = xmlDocGetRootElement(Doc.Get()); + xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) THROW(XmlException, "TDocument hasn't root element"); - return TConstNode(TNode(Doc.Get(), r)); + return TConstNode(TNode(Doc.Get(), r)); } bool TNode::IsNull() const { @@ -126,12 +126,12 @@ namespace NXml { return ctx; } - TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { + TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return XPath(xpath, quiet, *ctxt); } - TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { if (xmlXPathSetContextNode(NodePointer, &ctxt) != 0) THROW(XmlException, "Can't set xpath context node, probably the context is associated with another document"); @@ -141,59 +141,59 @@ namespace NXml { TConstNodes nodes(DocPointer, obj); - if (nodes.Size() == 0 && !quiet) + if (nodes.Size() == 0 && !quiet) THROW(NodeNotFound, xpath); return nodes; } - TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { + TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Nodes(xpath, quiet, *ctxt); } - TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { TConstNodes nodes = XPath(xpath, quiet, ctxt); - if (nodes.Size() != 0 && !nodes[0].IsElementNode()) - THROW(XmlException, "xpath points to non-element nodes: " << xpath); - return nodes; - } + if (nodes.Size() != 0 && !nodes[0].IsElementNode()) + THROW(XmlException, "xpath points to non-element nodes: " << xpath); + return nodes; + } - TNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) { + TNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); } - TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { + TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); } - TNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) { + TNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) { TConstNodes n = Nodes(xpath, quiet, ctxt); - if (n.Size() == 0 && !quiet) + if (n.Size() == 0 && !quiet) THROW(NodeNotFound, xpath); - if (n.Size() == 0) - return TNode(); + if (n.Size() == 0) + return TNode(); else return n[0].ConstCast(); } - TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return const_cast<TNode*>(this)->Node(xpath, quiet, ctxt); } - TNode TNode::FirstChild(TZtStringBuf name) { - if (IsNull()) + TNode TNode::FirstChild(TZtStringBuf name) { + if (IsNull()) THROW(XmlException, "Node is null"); - return Find(NodePointer->children, name); + return Find(NodePointer->children, name); } - TConstNode TNode::FirstChild(TZtStringBuf name) const { - return const_cast<TNode*>(this)->FirstChild(name); + TConstNode TNode::FirstChild(TZtStringBuf name) const { + return const_cast<TNode*>(this)->FirstChild(name); } TNode TNode::FirstChild() { @@ -218,15 +218,15 @@ namespace NXml { return const_cast<TNode*>(this)->Parent(); } - TNode TNode::NextSibling(TZtStringBuf name) { - if (IsNull()) + TNode TNode::NextSibling(TZtStringBuf name) { + if (IsNull()) THROW(XmlException, "Node is null"); - return Find(NodePointer->next, name); + return Find(NodePointer->next, name); } - TConstNode TNode::NextSibling(TZtStringBuf name) const { - return const_cast<TNode*>(this)->NextSibling(name); + TConstNode TNode::NextSibling(TZtStringBuf name) const { + return const_cast<TNode*>(this)->NextSibling(name); } TNode TNode::NextSibling() { @@ -242,7 +242,7 @@ namespace NXml { /* NOTE: by default child will inherit it's parent ns */ - TNode TNode::AddChild(TZtStringBuf name) { + TNode TNode::AddChild(TZtStringBuf name) { return AddChild(name, ""); } @@ -262,12 +262,12 @@ namespace NXml { return NodePointer->_private; } - TNode TNode::Find(xmlNode* start, TZtStringBuf name) { + TNode TNode::Find(xmlNode* start, TZtStringBuf name) { for (; start; start = start->next) if (start->type == XML_ELEMENT_NODE && (name.empty() || !xmlStrcmp(start->name, XMLCHAR(name.c_str())))) - return TNode(DocPointer, start); + return TNode(DocPointer, start); - return TNode(); + return TNode(); } TString TNode::Name() const { @@ -278,7 +278,7 @@ namespace NXml { } TString TNode::Path() const { - TCharPtr path(xmlGetNodePath(NodePointer)); + TCharPtr path(xmlGetNodePath(NodePointer)); if (!!path) return CAST2CHAR(path.Get()); else @@ -313,7 +313,7 @@ namespace NXml { return len; } - void TNode::SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const { + void TNode::SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const { const char* encoding = enc.size() ? enc.data() : "utf-8"; TSaveCtxtPtr ctx(xmlSaveToIO(XmlWriteToOstream, /* close */ nullptr, &stream, encoding, options)); @@ -321,45 +321,45 @@ namespace NXml { THROW(XmlException, "Failed saving node to stream"); } - void TNode::Save(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { + void TNode::Save(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { SaveInternal(stream, enc, shouldFormat ? XML_SAVE_FORMAT : 0); } - void TNode::SaveAsHtml(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { + void TNode::SaveAsHtml(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { int options = XML_SAVE_AS_HTML; options |= shouldFormat ? XML_SAVE_FORMAT : 0; SaveInternal(stream, enc, options); } TConstNodes::TConstNodes(const TConstNodes& nodes) - : SizeValue(nodes.Size()) - , Doc(nodes.Doc) - , Obj(nodes.Obj) + : SizeValue(nodes.Size()) + , Doc(nodes.Doc) + , Obj(nodes.Obj) { } TConstNodes& TConstNodes::operator=(const TConstNodes& nodes) { if (this != &nodes) { - SizeValue = nodes.Size(); - Doc = nodes.Doc; - Obj = nodes.Obj; + SizeValue = nodes.Size(); + Doc = nodes.Doc; + Obj = nodes.Obj; } return *this; } TConstNodes::TConstNodes(TConstNodesRef ref) - : SizeValue(ref.r_.Size()) - , Doc(ref.r_.Doc) - , Obj(ref.r_.Obj) + : SizeValue(ref.r_.Size()) + , Doc(ref.r_.Doc) + , Obj(ref.r_.Obj) { } TConstNodes& TConstNodes::operator=(TConstNodesRef ref) { if (this != &ref.r_) { - SizeValue = ref.r_.Size(); - Doc = ref.r_.Doc; - Obj = ref.r_.Obj; + SizeValue = ref.r_.Size(); + Doc = ref.r_.Doc; + Obj = ref.r_.Obj; } return *this; } @@ -376,18 +376,18 @@ namespace NXml { } TConstNode TConstNodes::operator[](size_t number) const { - if (number + 1 > Size()) + if (number + 1 > Size()) THROW(XmlException, "index out of range " << number); - if (!Obj || !Obj->nodesetval) - THROW(XmlException, "Broken TConstNodes object, Obj is null"); + if (!Obj || !Obj->nodesetval) + THROW(XmlException, "Broken TConstNodes object, Obj is null"); - xmlNode* node = Obj->nodesetval->nodeTab[number]; - return TNode(Doc, node); + xmlNode* node = Obj->nodesetval->nodeTab[number]; + return TNode(Doc, node); } TConstNode TConstNodes::TNodeIter::operator*() const { - return Nodes[Index]; - } - + return Nodes[Index]; + } + } diff --git a/library/cpp/xml/document/xml-document.h b/library/cpp/xml/document/xml-document.h index 728f76761f..829ba09cc4 100644 --- a/library/cpp/xml/document/xml-document.h +++ b/library/cpp/xml/document/xml-document.h @@ -1,4 +1,4 @@ #pragma once -#include "xml-document-decl.h" -#include "node-attr.h" +#include "xml-document-decl.h" +#include "node-attr.h" diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp index a8cfb45117..9f537b75c4 100644 --- a/library/cpp/xml/document/xml-document_ut.cpp +++ b/library/cpp/xml/document/xml-document_ut.cpp @@ -1,8 +1,8 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/generic/map.h> - -#include "xml-document.h" - + +#include "xml-document.h" + Y_UNIT_TEST_SUITE(TestXmlDocument) { Y_UNIT_TEST(Iteration) { NXml::TDocument xml( @@ -19,40 +19,40 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { } Y_UNIT_TEST(ParseString) { - NXml::TDocument xml( + NXml::TDocument xml( "<?xml version=\"1.0\"?>\n" "<root>\n" "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n" "<text>Некоторый текст</text>\n" "</root>", NXml::TDocument::String); - - NXml::TConstNode root = xml.Root(); - NXml::TConstNode b = root.Node("a/b"); - UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15); - UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true); - - NXml::TConstNode text = root.Node("text"); + + NXml::TConstNode root = xml.Root(); + NXml::TConstNode b = root.Node("a/b"); + UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15); + UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true); + + NXml::TConstNode text = root.Node("text"); UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст"); - } + } Y_UNIT_TEST(SerializeString) { - NXml::TDocument xml("frob", NXml::TDocument::RootName); - xml.Root().SetAttr("xyzzy", "Frobozz"); - xml.Root().SetAttr("kulness", 0.3); - xml.Root().SetAttr("timelimit", 3); - - NXml::TNode authors = xml.Root().AddChild("authors"); - authors.AddChild("graham").SetAttr("name", "Nelson"); - authors.AddChild("zarf").SetValue("Andrew Plotkin"); - authors.AddChild("emshort", "Emily Short"); - + NXml::TDocument xml("frob", NXml::TDocument::RootName); + xml.Root().SetAttr("xyzzy", "Frobozz"); + xml.Root().SetAttr("kulness", 0.3); + xml.Root().SetAttr("timelimit", 3); + + NXml::TNode authors = xml.Root().AddChild("authors"); + authors.AddChild("graham").SetAttr("name", "Nelson"); + authors.AddChild("zarf").SetValue("Andrew Plotkin"); + authors.AddChild("emshort", "Emily Short"); + TString data = xml.ToString("utf-8"); - UNIT_ASSERT_EQUAL(data, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" - "<frob xyzzy=\"Frobozz\" kulness=\"0.3\" timelimit=\"3\">\n" + UNIT_ASSERT_EQUAL(data, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<frob xyzzy=\"Frobozz\" kulness=\"0.3\" timelimit=\"3\">\n" " <authors>\n" " <graham name=\"Nelson\"/>\n" " <zarf>Andrew Plotkin</zarf>\n" - " <emshort>Emily Short</emshort>\n" + " <emshort>Emily Short</emshort>\n" " </authors>\n" "</frob>\n"); // check default utf8 output with ru @@ -62,7 +62,7 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_VALUES_EQUAL(xml2.ToString(), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<frob xyzzy=\"привет =)\"/>\n"); } - } + } Y_UNIT_TEST(XPathNs) { using namespace NXml; TDocument xml( @@ -114,11 +114,11 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_EQUAL(node.Value<TString>(), ""); node = node.NextSibling(); UNIT_ASSERT_EQUAL(node.IsNull(), true); - TStringStream iterLog; + TStringStream iterLog; for (const auto& node2 : root.Nodes("/root/*")) { iterLog << node2.Name() << ';'; - } - UNIT_ASSERT_STRINGS_EQUAL(iterLog.Str(), "a;c;"); + } + UNIT_ASSERT_STRINGS_EQUAL(iterLog.Str(), "a;c;"); // get only element nodes, ignore text nodes with empty "name" param node = root.FirstChild(TString()); @@ -152,29 +152,29 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_EXCEPTION(node.IsText(), yexception); } Y_UNIT_TEST(DefVal) { - using namespace NXml; - TDocument xml("<?xml version=\"1.0\"?>\n" + using namespace NXml; + TDocument xml("<?xml version=\"1.0\"?>\n" "<root><a></a></root>", NXml::TDocument::String); - UNIT_ASSERT_EQUAL(xml.Root().Node("a", true).Node("b", true).Value<int>(3), 3); - } + UNIT_ASSERT_EQUAL(xml.Root().Node("a", true).Node("b", true).Value<int>(3), 3); + } Y_UNIT_TEST(NodesVsXPath) { - using namespace NXml; - TDocument xml("<?xml version=\"1.0\"?>\n" + using namespace NXml; + TDocument xml("<?xml version=\"1.0\"?>\n" "<root><a x=\"y\"></a></root>", NXml::TDocument::String); - UNIT_ASSERT_EXCEPTION(xml.Root().Nodes("/root/a/@x"), yexception); - UNIT_ASSERT_VALUES_EQUAL(xml.Root().XPath("/root/a/@x").Size(), 1); - } + UNIT_ASSERT_EXCEPTION(xml.Root().Nodes("/root/a/@x"), yexception); + UNIT_ASSERT_VALUES_EQUAL(xml.Root().XPath("/root/a/@x").Size(), 1); + } Y_UNIT_TEST(NodeIsFirst) { - using namespace NXml; - TDocument xml("<?xml version=\"1.0\"?>\n" - "<root><a x=\"y\">first</a>" + using namespace NXml; + TDocument xml("<?xml version=\"1.0\"?>\n" + "<root><a x=\"y\">first</a>" "<a>second</a></root>", NXml::TDocument::String); - UNIT_ASSERT_EXCEPTION(xml.Root().Node("/root/a/@x"), yexception); + UNIT_ASSERT_EXCEPTION(xml.Root().Node("/root/a/@x"), yexception); UNIT_ASSERT_STRINGS_EQUAL(xml.Root().Node("/root/a").Value<TString>(), "first"); - } + } Y_UNIT_TEST(CopyNode) { using namespace NXml; // default-construct empty node @@ -304,16 +304,16 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_EXCEPTION(xml1.Root(), yexception); UNIT_ASSERT_VALUES_EQUAL(xml2.Root().ToString(), "<foo><bar/></foo>"); } - - Y_UNIT_TEST(StringConversion) { - using namespace NXml; - TDocument xml("foo", TDocument::RootName); - auto root = xml.Root(); - const TStringBuf stringBuf = "bar"; - root.SetAttr("bar", stringBuf); - const TString tString = "baz"; - root.SetAttr("baz", tString); - root.SetAttr("quux", "literal"); - root.SetAttr("frob", 500); - } -} + + Y_UNIT_TEST(StringConversion) { + using namespace NXml; + TDocument xml("foo", TDocument::RootName); + auto root = xml.Root(); + const TStringBuf stringBuf = "bar"; + root.SetAttr("bar", stringBuf); + const TString tString = "baz"; + root.SetAttr("baz", tString); + root.SetAttr("quux", "literal"); + root.SetAttr("frob", 500); + } +} diff --git a/library/cpp/xml/document/xml-textreader.cpp b/library/cpp/xml/document/xml-textreader.cpp index ab5c0436f3..b946f1fbf2 100644 --- a/library/cpp/xml/document/xml-textreader.cpp +++ b/library/cpp/xml/document/xml-textreader.cpp @@ -113,15 +113,15 @@ namespace NXml { return TempStringResult(xmlTextReaderGetAttributeNo(Impl.Get(), number)); } - TString TTextReader::GetAttribute(TZtStringBuf name) const { + TString TTextReader::GetAttribute(TZtStringBuf name) const { return TempStringResult(xmlTextReaderGetAttribute(Impl.Get(), XMLCHAR(name.data()))); } - TString TTextReader::GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const { + TString TTextReader::GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const { return TempStringResult(xmlTextReaderGetAttributeNs(Impl.Get(), XMLCHAR(localName.data()), XMLCHAR(nsUri.data()))); } - TString TTextReader::LookupNamespace(TZtStringBuf prefix) const { + TString TTextReader::LookupNamespace(TZtStringBuf prefix) const { return TempStringResult(xmlTextReaderLookupNamespace(Impl.Get(), XMLCHAR(prefix.data()))); } @@ -129,11 +129,11 @@ namespace NXml { return BoolResult(xmlTextReaderMoveToAttributeNo(Impl.Get(), number)); } - bool TTextReader::MoveToAttribute(TZtStringBuf name) { + bool TTextReader::MoveToAttribute(TZtStringBuf name) { return BoolResult(xmlTextReaderMoveToAttribute(Impl.Get(), XMLCHAR(name.data()))); } - bool TTextReader::MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri) { + bool TTextReader::MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri) { return BoolResult(xmlTextReaderMoveToAttributeNs(Impl.Get(), XMLCHAR(localName.data()), XMLCHAR(nsUri.data()))); } diff --git a/library/cpp/xml/document/xml-textreader.h b/library/cpp/xml/document/xml-textreader.h index 1507bfe22c..ab4c329d26 100644 --- a/library/cpp/xml/document/xml-textreader.h +++ b/library/cpp/xml/document/xml-textreader.h @@ -5,8 +5,8 @@ #include <contrib/libs/libxml/include/libxml/xmlreader.h> -#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> - +#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> + #include <util/generic/noncopyable.h> #include <util/generic/ptr.h> #include <util/generic/strbuf.h> @@ -222,21 +222,21 @@ namespace NXml { * Provides the value of the attribute with the specified qualified name. * @param name the qualified name of the attribute */ - TString GetAttribute(TZtStringBuf name) const; + TString GetAttribute(TZtStringBuf name) const; /** * Provides the value of the specified attribute. * @param localName the local name of the attribute * @param nsUri the namespace URI of the attribute */ - TString GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const; + TString GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const; /** * Resolves a namespace prefix in the scope of the current element. * @param prefix the prefix whose namespace URI is to be resolved. To return the default namespace, specify empty string. * @return a string containing the namespace URI to which the prefix maps. */ - TString LookupNamespace(TZtStringBuf prefix) const; + TString LookupNamespace(TZtStringBuf prefix) const; /** * Moves the position of the current instance to the attribute with the specified index relative to the containing element. @@ -250,7 +250,7 @@ namespace NXml { * @param name the qualified name of the attribute * @return true in case of success, false if not found */ - bool MoveToAttribute(TZtStringBuf name); + bool MoveToAttribute(TZtStringBuf name); /** * Moves the position of the current instance to the attribute with the specified local name and namespace URI. @@ -258,7 +258,7 @@ namespace NXml { * @param nsUri the namespace URI of the attribute * @return true in case of success, false if not found */ - bool MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri); + bool MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri); /** * Moves the position of the current instance to the first attribute associated with the current node. diff --git a/library/cpp/xml/document/ya.make b/library/cpp/xml/document/ya.make index c66f368d28..86bbd639cf 100644 --- a/library/cpp/xml/document/ya.make +++ b/library/cpp/xml/document/ya.make @@ -11,7 +11,7 @@ SRCS( PEERDIR( library/cpp/xml/init contrib/libs/libxml - library/cpp/string_utils/ztstrbuf + library/cpp/string_utils/ztstrbuf ) END() diff --git a/library/cpp/xml/init/ptr.h b/library/cpp/xml/init/ptr.h index bbc7ed9404..7387c7cc40 100644 --- a/library/cpp/xml/init/ptr.h +++ b/library/cpp/xml/init/ptr.h @@ -6,7 +6,7 @@ #include <libxml/xpath.h> #include <libxml/xpathInternals.h> #include <libxml/xmlsave.h> -#include <libxml/uri.h> +#include <libxml/uri.h> #include <libxml/xmlschemas.h> template <class T, void (*DestroyFun)(T*)> diff --git a/library/python/ya.make b/library/python/ya.make index c7870aae5b..2e1eb6e0e1 100644 --- a/library/python/ya.make +++ b/library/python/ya.make @@ -203,7 +203,7 @@ RECURSE( vault_client watch_dog watch_dog/example - wiki + wiki windows windows/ut yandex_tracker_client diff --git a/tools/ya.make b/tools/ya.make index 7dbd603fe8..51a6b8b426 100644 --- a/tools/ya.make +++ b/tools/ya.make @@ -132,7 +132,7 @@ RECURSE( rescompiler rescompressor rorescompiler - sandboxctl + sandboxctl sanitizer segutils seinfo diff --git a/util/charset/wide_ut.cpp b/util/charset/wide_ut.cpp index d24d8ceb87..d8f3233e73 100644 --- a/util/charset/wide_ut.cpp +++ b/util/charset/wide_ut.cpp @@ -193,7 +193,7 @@ public: void TestUTF8ToWide(); void TestWideToUTF8(); void TestGetNumOfUTF8Chars(); - void TestSubstrUTF8(); + void TestSubstrUTF8(); void TestUnicodeCase(); void TestUnicodeDetails(); void TestHexConversion(); @@ -549,14 +549,14 @@ void TConversionTest::TestGetNumOfUTF8Chars() { UNIT_ASSERT_EQUAL(GetNumberOfUTF8Chars("привет!"), 7); } -void TConversionTest::TestSubstrUTF8() { - TStringBuf utextBuf(utext, sizeof(utext)); - UNIT_ASSERT(SubstrUTF8(utextBuf, 0, 2) == utextBuf.substr(0, 4)); - UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 1) == utextBuf.substr(2, 2)); - UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 2) == utextBuf.substr(2, 4)); - UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 3) == utextBuf.substr(2, 6)); -} - +void TConversionTest::TestSubstrUTF8() { + TStringBuf utextBuf(utext, sizeof(utext)); + UNIT_ASSERT(SubstrUTF8(utextBuf, 0, 2) == utextBuf.substr(0, 4)); + UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 1) == utextBuf.substr(2, 2)); + UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 2) == utextBuf.substr(2, 4)); + UNIT_ASSERT(SubstrUTF8(utextBuf, 1, 3) == utextBuf.substr(2, 6)); +} + inline bool MustBeSurrogate(wchar32 ch) { return ch > 0xFFFF; } diff --git a/util/draft/datetime.h b/util/draft/datetime.h index df814d26da..8a387ea6f1 100644 --- a/util/draft/datetime.h +++ b/util/draft/datetime.h @@ -138,7 +138,7 @@ inline TString date2str(const time_t date) { memset(&dateTm, 0, sizeof(dateTm)); localtime_r(&date, &dateTm); char buf[9]; - strftime(buf, sizeof(buf), "%Y%m%d", &dateTm); + strftime(buf, sizeof(buf), "%Y%m%d", &dateTm); return TString(buf); } diff --git a/util/generic/mapfindptr.h b/util/generic/mapfindptr.h index 98aad17984..bc10cac60f 100644 --- a/util/generic/mapfindptr.h +++ b/util/generic/mapfindptr.h @@ -36,14 +36,14 @@ struct TMapOps { inline auto FindPtr(const K& key) const { return MapFindPtr(static_cast<const Derived&>(*this), key); } - + template <class K, class DefaultValue> inline auto Value(const K& key, const DefaultValue& defaultValue) const -> std::remove_reference_t<decltype(*this->FindPtr(key))> { if (auto found = FindPtr(key)) { - return *found; - } + return *found; + } return defaultValue; - } + } template <class K, class V> inline const V& ValueRef(const K& key, V& defaultValue) const { diff --git a/util/generic/mapfindptr_ut.cpp b/util/generic/mapfindptr_ut.cpp index 52a7c23794..613da7a96b 100644 --- a/util/generic/mapfindptr_ut.cpp +++ b/util/generic/mapfindptr_ut.cpp @@ -29,9 +29,9 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { const TTestMap& b = a; UNIT_ASSERT(b.FindPtr(42) && *b.FindPtr(42) == "dog"); UNIT_ASSERT_EQUAL(b.FindPtr(0), nullptr); - - UNIT_ASSERT_STRINGS_EQUAL(b.Value(42, "cat"), "dog"); - UNIT_ASSERT_STRINGS_EQUAL(b.Value(0, "alien"), "alien"); + + UNIT_ASSERT_STRINGS_EQUAL(b.Value(42, "cat"), "dog"); + UNIT_ASSERT_STRINGS_EQUAL(b.Value(0, "alien"), "alien"); } Y_UNIT_TEST(TestTemplateFind) { @@ -39,8 +39,8 @@ Y_UNIT_TEST_SUITE(TMapFindPtrTest) { m[TString("x")] = 2; - UNIT_ASSERT(m.FindPtr(TStringBuf("x"))); - UNIT_ASSERT_EQUAL(*m.FindPtr(TStringBuf("x")), 2); + UNIT_ASSERT(m.FindPtr(TStringBuf("x"))); + UNIT_ASSERT_EQUAL(*m.FindPtr(TStringBuf("x")), 2); } Y_UNIT_TEST(TestValue) { diff --git a/util/generic/strbuf.h b/util/generic/strbuf.h index f1b4e654c5..70b9360d58 100644 --- a/util/generic/strbuf.h +++ b/util/generic/strbuf.h @@ -303,20 +303,20 @@ public: } Y_PURE_FUNCTION inline TdSelf Before(TCharType c) const noexcept { - TdSelf l, r; + TdSelf l, r; return TrySplit(c, l, r) ? l : *this; } - + Y_PURE_FUNCTION inline TdSelf RAfter(TCharType c) const noexcept { TdSelf l, r; return TryRSplit(c, l, r) ? r : *this; } Y_PURE_FUNCTION inline TdSelf RBefore(TCharType c) const noexcept { - TdSelf l, r; + TdSelf l, r; return TryRSplit(c, l, r) ? l : *this; - } - + } + public: inline bool AfterPrefix(const TdSelf& prefix, TdSelf& result) const noexcept { if (this->StartsWith(prefix)) { diff --git a/util/generic/strbuf_ut.cpp b/util/generic/strbuf_ut.cpp index 695ca33b06..69cde785af 100644 --- a/util/generic/strbuf_ut.cpp +++ b/util/generic/strbuf_ut.cpp @@ -55,7 +55,7 @@ Y_UNIT_TEST_SUITE(TStrBufTest) { UNIT_ASSERT_VALUES_EQUAL(str.After('w'), TStringBuf("erty")); UNIT_ASSERT_VALUES_EQUAL(str.After('x'), TStringBuf("qwerty")); UNIT_ASSERT_VALUES_EQUAL(str.After('y'), TStringBuf()); - UNIT_ASSERT_STRINGS_EQUAL(str.After('='), str); + UNIT_ASSERT_STRINGS_EQUAL(str.After('='), str); // Also works properly on empty strings TStringBuf empty; @@ -72,15 +72,15 @@ Y_UNIT_TEST_SUITE(TStrBufTest) { } Y_UNIT_TEST(TestRAfterBefore) { - TStringBuf str("a/b/c"); - UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('/'), "c"); - UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('_'), str); - UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('a'), "/b/c"); - UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('/'), "a/b"); - UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('_'), str); - UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('a'), ""); - } - + TStringBuf str("a/b/c"); + UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('/'), "c"); + UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('_'), str); + UNIT_ASSERT_STRINGS_EQUAL(str.RAfter('a'), "/b/c"); + UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('/'), "a/b"); + UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('_'), str); + UNIT_ASSERT_STRINGS_EQUAL(str.RBefore('a'), ""); + } + Y_UNIT_TEST(TestAfterPrefix) { TStringBuf str("cat_dog"); diff --git a/util/generic/string_ut.cpp b/util/generic/string_ut.cpp index 775e571b42..ac82e9091d 100644 --- a/util/generic/string_ut.cpp +++ b/util/generic/string_ut.cpp @@ -32,7 +32,7 @@ public: UNIT_ASSERT(s.size() == sizeof(data)); UNIT_ASSERT(s.StartsWith(s)); UNIT_ASSERT(s.EndsWith(s)); - UNIT_ASSERT(s.Contains('\0')); + UNIT_ASSERT(s.Contains('\0')); const char raw_def[] = "def"; const char raw_zero[] = "\0"; diff --git a/util/stream/file.cpp b/util/stream/file.cpp index 895e627966..dc5d2f6311 100644 --- a/util/stream/file.cpp +++ b/util/stream/file.cpp @@ -20,7 +20,7 @@ TUnbufferedFileInput::TUnbufferedFileInput(const TFile& file) } size_t TUnbufferedFileInput::DoRead(void* buf, size_t len) { - return File_.ReadOrFail(buf, len); + return File_.ReadOrFail(buf, len); } size_t TUnbufferedFileInput::DoSkip(size_t len) { diff --git a/util/stream/file_ut.cpp b/util/stream/file_ut.cpp index 7780a72e04..ac0f09796e 100644 --- a/util/stream/file_ut.cpp +++ b/util/stream/file_ut.cpp @@ -56,19 +56,19 @@ Y_UNIT_TEST_SUITE(TFileTest) { UNIT_ASSERT(s.empty()); } } - -#ifdef _unix_ - Y_UNIT_TEST(PipeReadLineTest) { - int fds[2]; - UNIT_ASSERT(pipe(fds) == 0); - TFile readEnd(fds[0]); - TFileInput fileInput(readEnd); - UNIT_ASSERT_VALUES_EQUAL(write(fds[1], "hello\n", 6), 6); - - TString line; - UNIT_ASSERT(fileInput.ReadLine(line)); - UNIT_ASSERT_STRINGS_EQUAL(line, "hello"); - close(fds[1]); - } -#endif + +#ifdef _unix_ + Y_UNIT_TEST(PipeReadLineTest) { + int fds[2]; + UNIT_ASSERT(pipe(fds) == 0); + TFile readEnd(fds[0]); + TFileInput fileInput(readEnd); + UNIT_ASSERT_VALUES_EQUAL(write(fds[1], "hello\n", 6), 6); + + TString line; + UNIT_ASSERT(fileInput.ReadLine(line)); + UNIT_ASSERT_STRINGS_EQUAL(line, "hello"); + close(fds[1]); + } +#endif } diff --git a/util/stream/mem.cpp b/util/stream/mem.cpp index 31b46e907f..22a3339e27 100644 --- a/util/stream/mem.cpp +++ b/util/stream/mem.cpp @@ -18,8 +18,8 @@ TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept : Buf_(buf.data()) , Len_(buf.size()) { -} - +} + TMemoryInput::~TMemoryInput() = default; size_t TMemoryInput::DoNext(const void** ptr, size_t len) { diff --git a/util/stream/str.h b/util/stream/str.h index adac7ec4bc..028bd572c0 100644 --- a/util/stream/str.h +++ b/util/stream/str.h @@ -26,7 +26,7 @@ public: * this stream is in use. * * For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`). - * + * * @param s String to read from. */ inline TStringInput(const TString& s) noexcept @@ -108,27 +108,27 @@ private: * String input/output stream, similar to `std::stringstream`. */ class TStringStream: private TEmbedPolicy<TString>, public TStringInput, public TStringOutput { - using TEmbeddedString = TEmbedPolicy<TString>; + using TEmbeddedString = TEmbedPolicy<TString>; public: inline TStringStream() - : TEmbeddedString() - , TStringInput(*TEmbeddedString::Ptr()) - , TStringOutput(*TEmbeddedString::Ptr()) + : TEmbeddedString() + , TStringInput(*TEmbeddedString::Ptr()) + , TStringOutput(*TEmbeddedString::Ptr()) { } inline TStringStream(const TString& string) - : TEmbeddedString(string) - , TStringInput(*TEmbeddedString::Ptr()) - , TStringOutput(*TEmbeddedString::Ptr()) + : TEmbeddedString(string) + , TStringInput(*TEmbeddedString::Ptr()) + , TStringOutput(*TEmbeddedString::Ptr()) { } inline TStringStream(const TStringStream& other) - : TEmbeddedString(other.Str()) - , TStringInput(*TEmbeddedString::Ptr()) - , TStringOutput(*TEmbeddedString::Ptr()) + : TEmbeddedString(other.Str()) + , TStringInput(*TEmbeddedString::Ptr()) + , TStringOutput(*TEmbeddedString::Ptr()) { } diff --git a/util/stream/zlib.cpp b/util/stream/zlib.cpp index a9fe352239..60f4e9439f 100644 --- a/util/stream/zlib.cpp +++ b/util/stream/zlib.cpp @@ -80,17 +80,17 @@ namespace { class TZLibDecompress::TImpl: private TZLibCommon, public TChunkedZeroCopyInput { public: - inline TImpl(IZeroCopyInput* in, ZLib::StreamType type, TStringBuf dict) + inline TImpl(IZeroCopyInput* in, ZLib::StreamType type, TStringBuf dict) : TChunkedZeroCopyInput(in) - , Dict(dict) + , Dict(dict) { if (inflateInit2(Z(), opts[type]) != Z_OK) { ythrow TZLibDecompressorError() << "can not init inflate engine"; } - - if (dict.size() && type == ZLib::Raw) { - SetDict(); - } + + if (dict.size() && type == ZLib::Raw) { + SetDict(); + } } virtual ~TImpl() { @@ -113,11 +113,11 @@ public: } switch (inflate(Z(), Z_SYNC_FLUSH)) { - case Z_NEED_DICT: { - SetDict(); - continue; - } - + case Z_NEED_DICT: { + SetDict(); + continue; + } + case Z_STREAM_END: { if (AllowMultipleStreams_) { if (inflateReset(Z()) != Z_OK) { @@ -151,21 +151,21 @@ private: return Next(&Z()->next_in, &Z()->avail_in); } - void SetDict() { - if (inflateSetDictionary(Z(), (const Bytef*)Dict.data(), Dict.size()) != Z_OK) { - ythrow TZLibCompressorError() << "can not set inflate dictionary"; - } - } - + void SetDict() { + if (inflateSetDictionary(Z(), (const Bytef*)Dict.data(), Dict.size()) != Z_OK) { + ythrow TZLibCompressorError() << "can not set inflate dictionary"; + } + } + bool AllowMultipleStreams_ = true; - TStringBuf Dict; + TStringBuf Dict; }; namespace { class TDecompressStream: public IZeroCopyInput, public TZLibDecompress::TImpl, public TAdditionalStorage<TDecompressStream> { public: - inline TDecompressStream(IInputStream* input, ZLib::StreamType type, TStringBuf dict) - : TZLibDecompress::TImpl(this, type, dict) + inline TDecompressStream(IInputStream* input, ZLib::StreamType type, TStringBuf dict) + : TZLibDecompress::TImpl(this, type, dict) , Stream_(input) { } @@ -318,13 +318,13 @@ private: THolder<gz_header> GZHeader_; }; -TZLibDecompress::TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type, TStringBuf dict) - : Impl_(new TZeroCopyDecompress(input, type, dict)) +TZLibDecompress::TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type, TStringBuf dict) + : Impl_(new TZeroCopyDecompress(input, type, dict)) { } -TZLibDecompress::TZLibDecompress(IInputStream* input, ZLib::StreamType type, size_t buflen, TStringBuf dict) - : Impl_(new (buflen) TDecompressStream(input, type, dict)) +TZLibDecompress::TZLibDecompress(IInputStream* input, ZLib::StreamType type, size_t buflen, TStringBuf dict) + : Impl_(new (buflen) TDecompressStream(input, type, dict)) { } diff --git a/util/stream/zlib.h b/util/stream/zlib.h index ead8c1e013..e7de7c81b7 100644 --- a/util/stream/zlib.h +++ b/util/stream/zlib.h @@ -47,9 +47,9 @@ namespace ZLib { */ class TZLibDecompress: public IInputStream { public: - TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type = ZLib::Auto, TStringBuf dict = {}); - TZLibDecompress(IInputStream* input, ZLib::StreamType type = ZLib::Auto, size_t buflen = ZLib::ZLIB_BUF_LEN, - TStringBuf dict = {}); + TZLibDecompress(IZeroCopyInput* input, ZLib::StreamType type = ZLib::Auto, TStringBuf dict = {}); + TZLibDecompress(IInputStream* input, ZLib::StreamType type = ZLib::Auto, size_t buflen = ZLib::ZLIB_BUF_LEN, + TStringBuf dict = {}); /** * Allows/disallows multiple sequential compressed streams. Allowed by default. diff --git a/util/stream/zlib_ut.cpp b/util/stream/zlib_ut.cpp index 15f77f3ce4..2290b4a9de 100644 --- a/util/stream/zlib_ut.cpp +++ b/util/stream/zlib_ut.cpp @@ -75,21 +75,21 @@ Y_UNIT_TEST_SUITE(TZLibTest) { } } - Y_UNIT_TEST(Dictionary) { + Y_UNIT_TEST(Dictionary) { static constexpr TStringBuf data = "<html><body></body></html>"; static constexpr TStringBuf dict = "</<html><body>"; - for (auto type : {ZLib::Raw, ZLib::ZLib}) { - TStringStream compressed; - { - TZLibCompress compressor(TZLibCompress::TParams(&compressed).SetDict(dict).SetType(type)); - compressor.Write(data); - } - - TZLibDecompress decompressor(&compressed, type, ZLib::ZLIB_BUF_LEN, dict); - UNIT_ASSERT_STRINGS_EQUAL(decompressor.ReadAll(), data); - } - } - + for (auto type : {ZLib::Raw, ZLib::ZLib}) { + TStringStream compressed; + { + TZLibCompress compressor(TZLibCompress::TParams(&compressed).SetDict(dict).SetType(type)); + compressor.Write(data); + } + + TZLibDecompress decompressor(&compressed, type, ZLib::ZLIB_BUF_LEN, dict); + UNIT_ASSERT_STRINGS_EQUAL(decompressor.ReadAll(), data); + } + } + Y_UNIT_TEST(DecompressTwoStreams) { // Check that Decompress(Compress(X) + Compress(Y)) == X + Y TTempFile tmpFile(ZDATA); diff --git a/util/system/file.cpp b/util/system/file.cpp index b468d2cfb4..4a261d020c 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -16,7 +16,7 @@ #include <util/random/random.h> -#include <util/generic/size_literals.h> +#include <util/generic/size_literals.h> #include <util/generic/string.h> #include <util/generic/ylimits.h> #include <util/generic/yexception.h> @@ -932,31 +932,31 @@ public: return res; } - // Maximum amount of bytes to be read via single system call. - // Some libraries fail when it is greater than max int. - // Syscalls can cause contention if they operate on very large data blocks. - static constexpr size_t MaxReadPortion = 1_GB; - + // Maximum amount of bytes to be read via single system call. + // Some libraries fail when it is greater than max int. + // Syscalls can cause contention if they operate on very large data blocks. + static constexpr size_t MaxReadPortion = 1_GB; + i32 RawRead(void* bufferIn, size_t numBytes) { - const size_t toRead = Min(MaxReadPortion, numBytes); - return Handle_.Read(bufferIn, toRead); - } - - size_t ReadOrFail(void* buf, size_t numBytes) { - const i32 reallyRead = RawRead(buf, numBytes); - - if (reallyRead < 0) { - ythrow TFileError() << "can not read data from " << FileName_.Quote(); - } - - return reallyRead; - } - + const size_t toRead = Min(MaxReadPortion, numBytes); + return Handle_.Read(bufferIn, toRead); + } + + size_t ReadOrFail(void* buf, size_t numBytes) { + const i32 reallyRead = RawRead(buf, numBytes); + + if (reallyRead < 0) { + ythrow TFileError() << "can not read data from " << FileName_.Quote(); + } + + return reallyRead; + } + size_t Read(void* bufferIn, size_t numBytes) { ui8* buf = (ui8*)bufferIn; while (numBytes) { - const size_t reallyRead = ReadOrFail(buf, numBytes); + const size_t reallyRead = ReadOrFail(buf, numBytes); if (reallyRead == 0) { // file exhausted @@ -976,16 +976,16 @@ public: } } - // Maximum amount of bytes to be written via single system call. - // Some libraries fail when it is greater than max int. - // Syscalls can cause contention if they operate on very large data blocks. - static constexpr size_t MaxWritePortion = 1_GB; - + // Maximum amount of bytes to be written via single system call. + // Some libraries fail when it is greater than max int. + // Syscalls can cause contention if they operate on very large data blocks. + static constexpr size_t MaxWritePortion = 1_GB; + void Write(const void* buffer, size_t numBytes) { const ui8* buf = (const ui8*)buffer; while (numBytes) { - const i32 toWrite = (i32)Min(MaxWritePortion, numBytes); + const i32 toWrite = (i32)Min(MaxWritePortion, numBytes); const i32 reallyWritten = Handle_.Write(buf, toWrite); if (reallyWritten < 0) { @@ -1001,7 +1001,7 @@ public: ui8* buf = (ui8*)bufferIn; while (numBytes) { - const i32 toRead = (i32)Min(MaxReadPortion, numBytes); + const i32 toRead = (i32)Min(MaxReadPortion, numBytes); const i32 reallyRead = RawPread(buf, toRead, offset); if (reallyRead < 0) { @@ -1035,7 +1035,7 @@ public: const ui8* buf = (const ui8*)buffer; while (numBytes) { - const i32 toWrite = (i32)Min(MaxWritePortion, numBytes); + const i32 toWrite = (i32)Min(MaxWritePortion, numBytes); const i32 reallyWritten = Handle_.Pwrite(buf, toWrite, offset); if (reallyWritten < 0) { @@ -1175,10 +1175,10 @@ i32 TFile::RawRead(void* buf, size_t len) { return Impl_->RawRead(buf, len); } -size_t TFile::ReadOrFail(void* buf, size_t len) { - return Impl_->ReadOrFail(buf, len); -} - +size_t TFile::ReadOrFail(void* buf, size_t len) { + return Impl_->ReadOrFail(buf, len); +} + void TFile::Load(void* buf, size_t len) { Impl_->Load(buf, len); } diff --git a/util/system/file.h b/util/system/file.h index 62622501c9..9502e159b6 100644 --- a/util/system/file.h +++ b/util/system/file.h @@ -172,28 +172,28 @@ public: void LinkTo(const TFile& f) const; TFile Duplicate() const; - // Reads up to 1 GB without retrying, returns -1 on error - i32 RawRead(void* buf, size_t len); - // Reads up to 1 GB without retrying, throws on error - size_t ReadOrFail(void* buf, size_t len); - // Retries incomplete reads until EOF, throws on error + // Reads up to 1 GB without retrying, returns -1 on error + i32 RawRead(void* buf, size_t len); + // Reads up to 1 GB without retrying, throws on error + size_t ReadOrFail(void* buf, size_t len); + // Retries incomplete reads until EOF, throws on error size_t Read(void* buf, size_t len); - // Reads exactly len bytes, throws on premature EOF or error + // Reads exactly len bytes, throws on premature EOF or error void Load(void* buf, size_t len); - - // Retries incomplete writes, will either write len bytes or throw + + // Retries incomplete writes, will either write len bytes or throw void Write(const void* buf, size_t len); - - // Retries incomplete reads until EOF, throws on error + + // Retries incomplete reads until EOF, throws on error size_t Pread(void* buf, size_t len, i64 offset) const; // Single pread call i32 RawPread(void* buf, ui32 len, i64 offset) const; - // Reads exactly len bytes, throws on premature EOF or error + // Reads exactly len bytes, throws on premature EOF or error void Pload(void* buf, size_t len, i64 offset) const; - - // Retries incomplete writes, will either write len bytes or throw + + // Retries incomplete writes, will either write len bytes or throw void Pwrite(const void* buf, size_t len, i64 offset) const; - + void Flock(int op); //do not use, their meaning very platform-dependant diff --git a/util/system/filemap.cpp b/util/system/filemap.cpp index 7cd3a9b42f..7454a4cb94 100644 --- a/util/system/filemap.cpp +++ b/util/system/filemap.cpp @@ -153,14 +153,14 @@ public: } void CheckFile() const { - if (!File_.IsOpen()) { + if (!File_.IsOpen()) { ythrow yexception() << "TMemoryMap: FILE '" << DbgName_ << "' is not open, " << LastSystemErrorText(); - } - if (Length_ < 0) { + } + if (Length_ < 0) { ythrow yexception() << "'" << DbgName_ << "' is not a regular file"; - } - } - + } + } + inline TImpl(FILE* f, EOpenMode om, TString dbgName) : File_(Duplicate(f)) , DbgName_(std::move(dbgName)) diff --git a/util/system/fstat.cpp b/util/system/fstat.cpp index cadd0f4540..81e98cbc6b 100644 --- a/util/system/fstat.cpp +++ b/util/system/fstat.cpp @@ -6,7 +6,7 @@ #include <util/folder/path.h> #include <cerrno> - + #if defined(_win_) #include "fs_win.h" @@ -174,11 +174,11 @@ i64 GetFileLength(FHANDLE fd) { if (::fstat(fd, &statbuf) != 0) { return -1L; } - if (!(statbuf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) { - // st_size only makes sense for regular files or devices - errno = EINVAL; - return -1L; - } + if (!(statbuf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) { + // st_size only makes sense for regular files or devices + errno = EINVAL; + return -1L; + } return statbuf.st_size; #else #error unsupported platform @@ -199,11 +199,11 @@ i64 GetFileLength(const char* name) { if (r == -1) { return -1; } - if (!(buf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) { - // st_size only makes sense for regular files or devices - errno = EINVAL; - return -1; - } + if (!(buf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) { + // st_size only makes sense for regular files or devices + errno = EINVAL; + return -1; + } return (i64)buf.st_size; #else #error unsupported platform diff --git a/util/system/tempfile.cpp b/util/system/tempfile.cpp index 74a64f854d..a2e9f49eb1 100644 --- a/util/system/tempfile.cpp +++ b/util/system/tempfile.cpp @@ -1,20 +1,20 @@ -#include "tempfile.h" - +#include "tempfile.h" + TTempFileHandle::TTempFileHandle() : TTempFile(MakeTempName()) , TFile(CreateFile()) { -} - +} + TTempFileHandle::TTempFileHandle(const TString& fname) - : TTempFile(fname) - , TFile(CreateFile()) -{ -} - + : TTempFile(fname) + , TFile(CreateFile()) +{ +} + TTempFileHandle TTempFileHandle::InCurrentDir(const TString& filePrefix, const TString& extension) { return TTempFileHandle(MakeTempName(".", filePrefix.c_str(), extension.c_str())); -} +} TTempFileHandle TTempFileHandle::InDir(const TFsPath& dirPath, const TString& filePrefix, const TString& extension) { return TTempFileHandle(MakeTempName(dirPath.c_str(), filePrefix.c_str(), extension.c_str())); diff --git a/util/system/tempfile.h b/util/system/tempfile.h index 9bde1c669f..de249c129d 100644 --- a/util/system/tempfile.h +++ b/util/system/tempfile.h @@ -27,14 +27,14 @@ private: class TTempFileHandle: public TTempFile, public TFile { public: - TTempFileHandle(); + TTempFileHandle(); TTempFileHandle(const TString& fname); - + static TTempFileHandle InCurrentDir(const TString& filePrefix = "yandex", const TString& extension = "tmp"); static TTempFileHandle InDir(const TFsPath& dirPath, const TString& filePrefix = "yandex", const TString& extension = "tmp"); -private: - TFile CreateFile() const; +private: + TFile CreateFile() const; }; /* diff --git a/util/system/tempfile_ut.cpp b/util/system/tempfile_ut.cpp index 6f749d4e0e..e4a0923d0b 100644 --- a/util/system/tempfile_ut.cpp +++ b/util/system/tempfile_ut.cpp @@ -1,25 +1,25 @@ -#include "tempfile.h" - +#include "tempfile.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/folder/dirut.h> + +#include <util/folder/dirut.h> #include <util/generic/yexception.h> -#include <util/stream/file.h> - +#include <util/stream/file.h> + #include <algorithm> Y_UNIT_TEST_SUITE(TTempFileHandle) { Y_UNIT_TEST(Create) { TString path; - { - TTempFileHandle tmp; - path = tmp.Name(); - tmp.Write("hello world\n", 12); - tmp.FlushData(); + { + TTempFileHandle tmp; + path = tmp.Name(); + tmp.Write("hello world\n", 12); + tmp.FlushData(); UNIT_ASSERT_STRINGS_EQUAL(TUnbufferedFileInput(tmp.Name()).ReadAll(), "hello world\n"); - } + } UNIT_ASSERT(!NFs::Exists(path)); - } + } Y_UNIT_TEST(InCurrentDir) { #ifndef _win32_ @@ -115,7 +115,7 @@ Y_UNIT_TEST_SUITE(TTempFileHandle) { Y_UNIT_TEST(NonExistingDirectory) { UNIT_ASSERT_EXCEPTION(TTempFileHandle::InDir("nonexsistingdirname"), TSystemError); } -} +} Y_UNIT_TEST_SUITE(MakeTempName) { Y_UNIT_TEST(Default) { |