diff options
author | akhropov <akhropov@yandex-team.ru> | 2022-02-10 16:46:32 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:32 +0300 |
commit | 298c6da79f1d8f35089a67f463f0b541bec36d9b (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util | |
parent | 00afc96e9c0298054b7386fa7fb9e3cc3d67b974 (diff) | |
download | ydb-298c6da79f1d8f35089a67f463f0b541bec36d9b.tar.gz |
Restoring authorship annotation for <akhropov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util')
57 files changed, 888 insertions, 888 deletions
diff --git a/util/charset/wide.h b/util/charset/wide.h index 721b9c0513..04e6928aab 100644 --- a/util/charset/wide.h +++ b/util/charset/wide.h @@ -26,8 +26,8 @@ using TCharTemp = TTempArray<wchar16>; namespace NDetail { inline TString InStringMsg(const char* s, size_t len) { return (len <= 50) ? " in string " + TString(s, len).Quote() : TString(); - } - + } + template <bool isPointer> struct TSelector; diff --git a/util/charset/wide_ut.cpp b/util/charset/wide_ut.cpp index 9eebbf5387..d8f3233e73 100644 --- a/util/charset/wide_ut.cpp +++ b/util/charset/wide_ut.cpp @@ -500,12 +500,12 @@ void TConversionTest::TestUTF8ToWide() { UNIT_ASSERT(w == wideLatinAlphabet); w = UTF8ToWide(utf8CyrillicAlphabet, strlen(utf8CyrillicAlphabet)); UNIT_ASSERT(w == wideCyrillicAlphabet); - - const char* utf8NonBMP = "\xf4\x80\x89\x84\xf4\x80\x89\x87\xf4\x80\x88\xba"; + + const char* utf8NonBMP = "\xf4\x80\x89\x84\xf4\x80\x89\x87\xf4\x80\x88\xba"; wchar16 wNonBMPDummy[] = {0xDBC0, 0xDE44, 0xDBC0, 0xDE47, 0xDBC0, 0xDE3A}; TestSurrogates(utf8NonBMP, wNonBMPDummy, Y_ARRAY_SIZE(wNonBMPDummy)); - - const char* utf8NonBMP2 = "ab\xf4\x80\x89\x87n"; + + const char* utf8NonBMP2 = "ab\xf4\x80\x89\x87n"; wchar16 wNonBMPDummy2[] = {'a', 'b', 0xDBC0, 0xDE47, 'n'}; TestSurrogates(utf8NonBMP2, wNonBMPDummy2, Y_ARRAY_SIZE(wNonBMPDummy2)); diff --git a/util/datetime/constants.h b/util/datetime/constants.h index 63022d73a8..352403270e 100644 --- a/util/datetime/constants.h +++ b/util/datetime/constants.h @@ -1,7 +1,7 @@ -#pragma once - +#pragma once + #include <time.h> - -const time_t SECONDS_IN_DAY = 86400; -const time_t SECONDS_IN_HOUR = 3600; + +const time_t SECONDS_IN_DAY = 86400; +const time_t SECONDS_IN_HOUR = 3600; const time_t SECONDS_IN_HALFHOUR = 1800; diff --git a/util/draft/date.cpp b/util/draft/date.cpp index 314b1ccd9a..a290c46050 100644 --- a/util/draft/date.cpp +++ b/util/draft/date.cpp @@ -3,18 +3,18 @@ #include <util/string/cast.h> #include <util/generic/yexception.h> #include <util/datetime/base.h> - + time_t GetDateStart(time_t ts) { - tm dateTm; - memset(&dateTm, 0, sizeof(tm)); - localtime_r(&ts, &dateTm); + tm dateTm; + memset(&dateTm, 0, sizeof(tm)); + localtime_r(&ts, &dateTm); dateTm.tm_isdst = -1; - dateTm.tm_sec = 0; - dateTm.tm_min = 0; - dateTm.tm_hour = 0; - return mktime(&dateTm); + dateTm.tm_sec = 0; + dateTm.tm_min = 0; + dateTm.tm_hour = 0; + return mktime(&dateTm); } static time_t ParseDate(const char* date, const char* format) { @@ -34,11 +34,11 @@ static time_t ParseDate(const char* dateStr) { return ParseDate(dateStr, "%Y%m%d"); } -template <> -TDate FromStringImpl<TDate>(const char* data, size_t len) { +template <> +TDate FromStringImpl<TDate>(const char* data, size_t len) { return TDate(ParseDate(TString(data, len).data())); -} - +} + TDate::TDate(const char* yyyymmdd) : Timestamp(GetDateStart(ParseDate(yyyymmdd))) { @@ -60,18 +60,18 @@ TDate::TDate(const TString& date, const TString& format) } TDate::TDate(unsigned year, unsigned month, unsigned monthDay) { - tm dateTm; - Zero(dateTm); + tm dateTm; + Zero(dateTm); dateTm.tm_year = year - 1900; dateTm.tm_mon = month - 1; dateTm.tm_mday = monthDay; - dateTm.tm_isdst = -1; - Timestamp = mktime(&dateTm); + dateTm.tm_isdst = -1; + Timestamp = mktime(&dateTm); if (Timestamp == (time_t)-1) { - ythrow yexception() << "Invalid TDate args:(" << year << ',' << month << ',' << monthDay << ')'; + ythrow yexception() << "Invalid TDate args:(" << year << ',' << month << ',' << monthDay << ')'; } -} - +} + time_t TDate::GetStartUTC() const { tm dateTm; localtime_r(&Timestamp, &dateTm); @@ -83,31 +83,31 @@ time_t TDate::GetStartUTC() const { } TString TDate::ToStroka(const char* format) const { - tm dateTm; - localtime_r(&Timestamp, &dateTm); + tm dateTm; + localtime_r(&Timestamp, &dateTm); return Strftime(format, &dateTm); } unsigned TDate::GetWeekDay() const { - tm dateTm; - localtime_r(&Timestamp, &dateTm); - return (unsigned)dateTm.tm_wday; -} - + tm dateTm; + localtime_r(&Timestamp, &dateTm); + return (unsigned)dateTm.tm_wday; +} + unsigned TDate::GetYear() const { - tm dateTm; - localtime_r(&Timestamp, &dateTm); - return ((unsigned)dateTm.tm_year) + 1900; -} - + tm dateTm; + localtime_r(&Timestamp, &dateTm); + return ((unsigned)dateTm.tm_year) + 1900; +} + unsigned TDate::GetMonth() const { - tm dateTm; - localtime_r(&Timestamp, &dateTm); - return ((unsigned)dateTm.tm_mon) + 1; -} - + tm dateTm; + localtime_r(&Timestamp, &dateTm); + return ((unsigned)dateTm.tm_mon) + 1; +} + unsigned TDate::GetMonthDay() const { - tm dateTm; - localtime_r(&Timestamp, &dateTm); - return (unsigned)dateTm.tm_mday; -} + tm dateTm; + localtime_r(&Timestamp, &dateTm); + return (unsigned)dateTm.tm_mday; +} diff --git a/util/draft/date.h b/util/draft/date.h index 0e0b1d7a36..e3eb616fe5 100644 --- a/util/draft/date.h +++ b/util/draft/date.h @@ -29,10 +29,10 @@ public: TDate(const char* yyyymmdd); TDate(const TString& yyyymmdd); - TDate(unsigned year, unsigned month, unsigned monthDay); // month from 01, monthDay from 01 + TDate(unsigned year, unsigned month, unsigned monthDay); // month from 01, monthDay from 01 TDate(const TString& date, const TString& format); - - explicit TDate(time_t t); + + explicit TDate(time_t t); time_t GetStart() const { return Timestamp; @@ -54,28 +54,28 @@ public: TDate& operator+=(unsigned days) { Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); - return *this; - } - + return *this; + } + TDate& operator-=(unsigned days) { Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); - return *this; - } - + return *this; + } + TDate operator+(unsigned days) const { return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); - } - + } + TDate operator-(unsigned days) const { return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); - } - - unsigned GetWeekDay() const; // days since Sunday - - unsigned GetYear() const; + } + + unsigned GetWeekDay() const; // days since Sunday + + unsigned GetYear() const; unsigned GetMonth() const; // from 01 - unsigned GetMonthDay() const; // from 01 - + unsigned GetMonthDay() const; // from 01 + friend bool operator<(const TDate& left, const TDate& right); friend bool operator>(const TDate& left, const TDate& right); friend bool operator<=(const TDate& left, const TDate& right); diff --git a/util/draft/date_ut.cpp b/util/draft/date_ut.cpp index 5320087abf..8c33a6c1cf 100644 --- a/util/draft/date_ut.cpp +++ b/util/draft/date_ut.cpp @@ -1,29 +1,29 @@ -#include "date.h" - +#include "date.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TDateTest) { Y_UNIT_TEST(ComponentsTest) { - { - TDate d("20110215"); - UNIT_ASSERT_EQUAL(d.GetYear(), 2011); - UNIT_ASSERT_EQUAL(d.GetMonth(), 2); - UNIT_ASSERT_EQUAL(d.GetMonthDay(), 15); + { + TDate d("20110215"); + UNIT_ASSERT_EQUAL(d.GetYear(), 2011); + UNIT_ASSERT_EQUAL(d.GetMonth(), 2); + UNIT_ASSERT_EQUAL(d.GetMonthDay(), 15); UNIT_ASSERT_EQUAL(d.ToStroka("%Y%m%d"), "20110215"); UNIT_ASSERT_EQUAL(d.ToStroka(), "20110215"); UNIT_ASSERT_EQUAL(d.ToStroka("%Y--%m--%d"), "2011--02--15"); UNIT_ASSERT_EQUAL(d.ToStroka("%U"), "07"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1297728000); - } - { + } + { TDate d(2005, 6, 3); - UNIT_ASSERT_EQUAL(d.GetYear(), 2005); - UNIT_ASSERT_EQUAL(d.GetMonth(), 6); - UNIT_ASSERT_EQUAL(d.GetMonthDay(), 3); + UNIT_ASSERT_EQUAL(d.GetYear(), 2005); + UNIT_ASSERT_EQUAL(d.GetMonth(), 6); + UNIT_ASSERT_EQUAL(d.GetMonthDay(), 3); UNIT_ASSERT_EQUAL(d.ToStroka(), "20050603"); UNIT_ASSERT_EQUAL(d.ToStroka("____%Y__%m____%d"), "____2005__06____03"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1117756800); - } + } { TDate d("2011-02-15", "%Y-%m-%d"); UNIT_ASSERT_EQUAL(d.GetYear(), 2011); @@ -32,5 +32,5 @@ Y_UNIT_TEST_SUITE(TDateTest) { UNIT_ASSERT_EQUAL(d.ToStroka("%Y%m%d"), "20110215"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1297728000); } - } -} + } +} diff --git a/util/draft/datetime.cpp b/util/draft/datetime.cpp index cd6a2462cc..5cbe7d8847 100644 --- a/util/draft/datetime.cpp +++ b/util/draft/datetime.cpp @@ -1,15 +1,15 @@ #include "datetime.h" -#include <util/ysaveload.h> - +#include <util/ysaveload.h> + #include <util/system/atomic.h> #include <util/system/fasttime.h> #include <util/datetime/base.h> #include <util/datetime/systime.h> #include <util/stream/output.h> -#include <util/stream/mem.h> -#include <util/string/cast.h> -#include <util/string/printf.h> +#include <util/stream/mem.h> +#include <util/string/cast.h> +#include <util/string/printf.h> namespace NDatetime { const ui32 MonthDays[2][12] = { @@ -213,25 +213,25 @@ namespace NDatetime { return t; } } - + template <> void In<TMonth>(IInputStream& in, TMonth& t) { - char buf[4]; - LoadPodArray(&in, buf, 4); + char buf[4]; + LoadPodArray(&in, buf, 4); t.Year = FromString<ui16>(buf, 4); - LoadPodArray(&in, buf, 2); + LoadPodArray(&in, buf, 2); t.Month = ui8(FromString<ui16>(buf, 2)) - 1; -} - +} + template <> void Out<TMonth>(IOutputStream& o, const TMonth& t) { - o << t.Year << Sprintf("%.2hu", (ui16)(t.Month + 1)); -} - + o << t.Year << Sprintf("%.2hu", (ui16)(t.Month + 1)); +} + template <> TMonth FromStringImpl<TMonth, char>(const char* s, size_t len) { - TMonth res; + TMonth res; TMemoryInput in(s, len); - in >> res; - return res; -} + in >> res; + return res; +} diff --git a/util/draft/datetime.h b/util/draft/datetime.h index 125adff6ab..8a387ea6f1 100644 --- a/util/draft/datetime.h +++ b/util/draft/datetime.h @@ -8,8 +8,8 @@ #include <cstdlib> -#include <time.h> - +#include <time.h> + namespace NDatetime { extern const ui32 MonthDays[2][12]; // !leapYear; !!leapYear extern const ui32 MonthDaysNewYear[2][13]; // !leapYear; !!leapYear @@ -132,53 +132,53 @@ namespace NDatetime { } }; } - + inline TString date2str(const time_t date) { struct tm dateTm; memset(&dateTm, 0, sizeof(dateTm)); localtime_r(&date, &dateTm); - char buf[9]; + char buf[9]; strftime(buf, sizeof(buf), "%Y%m%d", &dateTm); return TString(buf); -} - +} + inline time_t str2date(const TString& dateStr) { struct tm dateTm; - memset(&dateTm, 0, sizeof(tm)); + memset(&dateTm, 0, sizeof(tm)); strptime(dateStr.data(), "%Y%m%d", &dateTm); - return mktime(&dateTm); -} - -// checks whether time2 > time1 and close enough to it + return mktime(&dateTm); +} + +// checks whether time2 > time1 and close enough to it inline bool AreTimesSeqAndClose(time_t time1, time_t time2, time_t closeInterval = 10) { - return (time2 - time1) <= closeInterval; -} - -// checks whether time2 and time1 are close enough + return (time2 - time1) <= closeInterval; +} + +// checks whether time2 and time1 are close enough inline bool AreTimesClose(time_t time1, time_t time2, time_t closeInterval = 10) { return std::abs(time2 - time1) <= closeInterval; -} - -//////////////////////////////// - +} + +//////////////////////////////// + struct TMonth { - ui16 Year; + ui16 Year; ui8 Month; - - TMonth(ui16 year = 0, ui8 month = 0) - : Year(year) - , Month(month) + + TMonth(ui16 year = 0, ui8 month = 0) + : Year(year) + , Month(month) { } - + TMonth operator-(ui16 n) { - if (n <= Month) { + if (n <= Month) { return TMonth(Year, Month - (ui8)n); - } else { - n -= Month; - return (n % 12) ? TMonth(Year - 1 - (n / 12), 12 - (n % 12)) : TMonth(Year - (n / 12), 0); - } - } -}; + } else { + n -= Month; + return (n % 12) ? TMonth(Year - 1 - (n / 12), 12 - (n % 12)) : TMonth(Year - (n / 12), 0); + } + } +}; Y_DECLARE_PODTYPE(NDatetime::TSimpleTM); diff --git a/util/draft/enum.h b/util/draft/enum.h index 2939099b5e..18002b7df2 100644 --- a/util/draft/enum.h +++ b/util/draft/enum.h @@ -1,16 +1,16 @@ #pragma once - + #include <bitset> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <util/stream/str.h> -#include <util/string/cast.h> +#include <util/string/cast.h> #include <util/string/split.h> #include <utility> - + class TEnumNotFoundException: public yexception { -}; - +}; + #define EnumFromString(key, entries) EnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) #define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size) #define FindEnumFromString(key, entries) FindEnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) @@ -18,24 +18,24 @@ class TEnumNotFoundException: public yexception { #define EnumToString(key, entries) EnumToStringImpl(key, entries, Y_ARRAY_SIZE(entries)) #define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size) #define PrintEnumItems(entries) PrintEnumItemsImpl(entries, Y_ARRAY_SIZE(entries)) - + template <class K1, class K2, class V> const V* FindEnumFromStringImpl(K1 key, const std::pair<K2, V>* entries, size_t arraySize) { - for (size_t i = 0; i < arraySize; i++) - if (entries[i].first == key) - return &entries[i].second; + for (size_t i = 0; i < arraySize; i++) + if (entries[i].first == key) + return &entries[i].second; return nullptr; -} - +} + // special version for const char* template <class V> const V* FindEnumFromStringImpl(const char* key, const std::pair<const char*, V>* entries, size_t arraySize) { - for (size_t i = 0; i < arraySize; i++) - if (entries[i].first && key && !strcmp(entries[i].first, key)) - return &entries[i].second; + for (size_t i = 0; i < arraySize; i++) + if (entries[i].first && key && !strcmp(entries[i].first, key)) + return &entries[i].second; return nullptr; -} - +} + template <class K, class V> TString PrintEnumItemsImpl(const std::pair<K, V>* entries, size_t arraySize) { TString result; @@ -57,18 +57,18 @@ TString PrintEnumItemsImpl(const std::pair<const char*, V>* entries, size_t arra template <class K1, class K2, class V> const V* EnumFromStringImpl(K1 key, const std::pair<K2, V>* entries, size_t arraySize) { - const V* res = FindEnumFromStringImpl(key, entries, arraySize); - if (res) - return res; + const V* res = FindEnumFromStringImpl(key, entries, arraySize); + if (res) + return res; ythrow TEnumNotFoundException() << "Key '" << key << "' not found in enum. Valid options are: " << PrintEnumItemsImpl(entries, arraySize) << ". "; -} - +} + template <class K, class V> const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arraySize) { - for (size_t i = 0; i < arraySize; i++) - if (entries[i].second == value) - return &entries[i].first; + for (size_t i = 0; i < arraySize; i++) + if (entries[i].second == value) + return &entries[i].first; TEnumNotFoundException exc; exc << "Value '" << int(value) << "' not found in enum. Valid values are: "; @@ -76,10 +76,10 @@ const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arrayS exc << (i ? ", " : "") << int(entries[i].second); exc << ". "; ythrow exc; -} - -/////////////////////////////////// - +} + +/////////////////////////////////// + template <class B> inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { if (allIfEmpty) { @@ -89,18 +89,18 @@ inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { } } -// all set by default +// all set by default template <class E, size_t N, size_t B> inline void SetEnumFlags(const std::pair<const char*, E> (&str2Enum)[N], TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) { - if (optSpec.empty()) { + if (optSpec.empty()) { SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { flags.reset(); for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, N); flags.set(e); - } + } } } @@ -110,14 +110,14 @@ inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf o bool allIfEmpty = true) { if (optSpec.empty()) { SetEnumFlagsForEmptySpec(flags, allIfEmpty); - } else { + } else { flags.reset(); for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, size); flags.set(e); - } - } -} + } + } +} // for enums generated with GENERATE_ENUM_SERIALIZATION template <class E, size_t B> @@ -128,7 +128,7 @@ inline void SetEnumFlags(TStringBuf optSpec, std::bitset<B>& flags, bool allIfEm flags.reset(); for (const auto& it : StringSplitter(optSpec).Split(',')) { E e; - if (!TryFromString(it.Token(), e)) + if (!TryFromString(it.Token(), e)) ythrow yexception() << "Unknown enum value '" << it.Token() << "'"; flags.set((size_t)e); } diff --git a/util/draft/holder_vector.h b/util/draft/holder_vector.h index 0aff81648b..1c62055bd9 100644 --- a/util/draft/holder_vector.h +++ b/util/draft/holder_vector.h @@ -1,46 +1,46 @@ #pragma once - + #include <util/generic/ptr.h> #include <util/generic/vector.h> #include <util/generic/noncopyable.h> - + template <class T, class D = TDelete> class THolderVector: public TVector<T*>, public TNonCopyable { using TBase = TVector<T*>; -public: +public: explicit THolderVector(size_t n = 0) : TBase(n) { } - ~THolderVector() { + ~THolderVector() { Clear(); } void Clear() { - for (typename TBase::iterator it = TBase::begin(); it != TBase::end(); ++it) { + for (typename TBase::iterator it = TBase::begin(); it != TBase::end(); ++it) { if (*it) D::Destroy(*it); - } + } TBase::clear(); - } - - size_t Size() const { - return TBase::size(); - } - + } + + size_t Size() const { + return TBase::size(); + } + // TVector takes ownership of T - void PushBack(T* t) { - try { + void PushBack(T* t) { + try { TBase::push_back(t); } catch (...) { if (t) D::Destroy(t); throw; - } - } - + } + } + void PushBack(std::unique_ptr<T> t) { PushBack(t.release()); } @@ -82,9 +82,9 @@ public: TBase::swap(other); } - using TBase::operator[]; + using TBase::operator[]; using TBase::operator bool; - using TBase::at; + using TBase::at; using TBase::back; using TBase::begin; using TBase::capacity; @@ -99,4 +99,4 @@ public: using typename TBase::iterator; using typename TBase::reverse_iterator; using typename TBase::value_type; -}; +}; diff --git a/util/draft/ip.h b/util/draft/ip.h index 04db85ae24..eb947cd2cd 100644 --- a/util/draft/ip.h +++ b/util/draft/ip.h @@ -1,48 +1,48 @@ -#pragma once - -#include <util/digest/murmur.h> - -#include <util/network/ip.h> - -#include <util/str_stl.h> +#pragma once + +#include <util/digest/murmur.h> + +#include <util/network/ip.h> + +#include <util/str_stl.h> #include <util/generic/maybe.h> #include <util/generic/variant.h> - -#ifdef _unix_ + +#ifdef _unix_ #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> -#endif // _unix_ - -#include <string.h> - -#ifndef INET6_ADDRSTRLEN +#endif // _unix_ + +#include <string.h> + +#ifndef INET6_ADDRSTRLEN #define INET6_ADDRSTRLEN 46 -#endif - -// Network (big-endian) byte order +#endif + +// Network (big-endian) byte order using TIp4 = TIpHost; - -// Network (big-endian) byte order + +// Network (big-endian) byte order struct TIp6 { - char Data[16]; - - bool operator==(const TIp6& rhs) const { - return memcmp(Data, rhs.Data, sizeof(Data)) == 0; - } + char Data[16]; + + bool operator==(const TIp6& rhs) const { + return memcmp(Data, rhs.Data, sizeof(Data)) == 0; + } bool operator<(const TIp6& rhs) const { return memcmp(Data, rhs.Data, sizeof(Data)) < 0; } -}; - -template <> -struct THash<TIp6> { - inline size_t operator()(const TIp6& ip) const { - return MurmurHash<size_t>((const void*)ip.Data, 16); - } -}; - +}; + +template <> +struct THash<TIp6> { + inline size_t operator()(const TIp6& ip) const { + return MurmurHash<size_t>((const void*)ip.Data, 16); + } +}; + static inline TIp6 Ip6FromIp4(TIp4 addr) { TIp6 res; memset(res.Data, 0, sizeof(res.Data)); @@ -52,16 +52,16 @@ static inline TIp6 Ip6FromIp4(TIp4 addr) { return res; } -static inline TIp6 Ip6FromString(const char* ipStr) { - TIp6 res; - - if (inet_pton(AF_INET6, ipStr, &res.Data) == 0) { +static inline TIp6 Ip6FromString(const char* ipStr) { + TIp6 res; + + if (inet_pton(AF_INET6, ipStr, &res.Data) == 0) { ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ipv6 address"; - } - - return res; -} - + } + + return res; +} + static inline TMaybe<TIp6> TryParseIp6FromString(const char* ipStr) { TIp6 res; @@ -72,60 +72,60 @@ static inline TMaybe<TIp6> TryParseIp6FromString(const char* ipStr) { return res; } -static inline char* Ip6ToString(const TIp6& ip, char* buf, size_t len) { - if (!inet_ntop(AF_INET6, (void*)&ip.Data, buf, (socklen_t)len)) { - ythrow TSystemError() << "Failed to get ipv6 address string"; - } - - return buf; -} - +static inline char* Ip6ToString(const TIp6& ip, char* buf, size_t len) { + if (!inet_ntop(AF_INET6, (void*)&ip.Data, buf, (socklen_t)len)) { + ythrow TSystemError() << "Failed to get ipv6 address string"; + } + + return buf; +} + static inline TString Ip6ToString(const TIp6& ip) { - char buf[INET6_ADDRSTRLEN]; - + char buf[INET6_ADDRSTRLEN]; + return TString(Ip6ToString(ip, buf, sizeof(buf))); -} - +} + template <> inline void Out<TIp6>(IOutputStream& os, const TIp6& a) { os << Ip6ToString(a); } using TIp4Or6 = std::variant<TIp4, TIp6>; - -static inline TIp4Or6 Ip4Or6FromString(const char* ipStr) { - const char* c = ipStr; - for (; *c; ++c) { - if (*c == '.') { - return IpFromString(ipStr); - } - if (*c == ':') { - return Ip6FromString(ipStr); - } - } + +static inline TIp4Or6 Ip4Or6FromString(const char* ipStr) { + const char* c = ipStr; + for (; *c; ++c) { + if (*c == '.') { + return IpFromString(ipStr); + } + if (*c == ':') { + return Ip6FromString(ipStr); + } + } ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ipv4 or ipv6 address"; -} - +} + static inline TString Ip4Or6ToString(const TIp4Or6& ip) { if (std::holds_alternative<TIp6>(ip)) { return Ip6ToString(std::get<TIp6>(ip)); - } else { + } else { return IpToString(std::get<TIp4>(ip)); - } -} - -// for TIp4 or TIp6, not TIp4Or6 + } +} + +// for TIp4 or TIp6, not TIp4Or6 template <class TIp> struct TIpCompare { - bool Less(const TIp& l, const TIp& r) const { - return memcmp(&l, &r, sizeof(TIp)) < 0; - } - - bool LessEqual(const TIp& l, const TIp& r) const { - return memcmp(&l, &r, sizeof(TIp)) <= 0; - } - - bool operator()(const TIp& l, const TIp& r) const { - return Less(l, r); - } -}; + bool Less(const TIp& l, const TIp& r) const { + return memcmp(&l, &r, sizeof(TIp)) < 0; + } + + bool LessEqual(const TIp& l, const TIp& r) const { + return memcmp(&l, &r, sizeof(TIp)) <= 0; + } + + bool operator()(const TIp& l, const TIp& r) const { + return Less(l, r); + } +}; diff --git a/util/draft/memory.h b/util/draft/memory.h index 1f7298d7ec..0a9722bb36 100644 --- a/util/draft/memory.h +++ b/util/draft/memory.h @@ -1,40 +1,40 @@ -#pragma once - +#pragma once + #include <util/system/defaults.h> -#include <algorithm> -#include <functional> -#include <utility> - +#include <algorithm> +#include <functional> +#include <utility> + template <class T> inline bool IsZero(const T* begin, const T* end) { return std::find_if(begin, end, [](const T& other) { return other != T(0); }) == end; -} - +} + template <size_t Size> inline bool IsZero(const char* p) { size_t sizeInUI64 = Size / 8; const char* pEndUi64 = p + sizeInUI64 * 8; - if (sizeInUI64 && !IsZero<ui64>((const ui64*)p, (const ui64*)pEndUi64)) - return false; - return IsZero(pEndUi64, p + Size); -} - + if (sizeInUI64 && !IsZero<ui64>((const ui64*)p, (const ui64*)pEndUi64)) + return false; + return IsZero(pEndUi64, p + Size); +} + #define IS_ZERO_INTSZ(INT) \ template <> \ inline bool IsZero<sizeof(INT)>(const char* p) { \ return (*(INT*)p) == INT(0); \ } - -IS_ZERO_INTSZ(ui8) -IS_ZERO_INTSZ(ui16) -IS_ZERO_INTSZ(ui32) -IS_ZERO_INTSZ(ui64) - -#undef IS_ZERO_INTSZ - -// If you want to use this to check all fields in a struct make sure it's w/o holes or #pragma pack(1) + +IS_ZERO_INTSZ(ui8) +IS_ZERO_INTSZ(ui16) +IS_ZERO_INTSZ(ui32) +IS_ZERO_INTSZ(ui64) + +#undef IS_ZERO_INTSZ + +// If you want to use this to check all fields in a struct make sure it's w/o holes or #pragma pack(1) template <class T> bool IsZero(const T& t) { - return IsZero<sizeof(T)>((const char*)&t); -} + return IsZero<sizeof(T)>((const char*)&t); +} diff --git a/util/draft/memory_ut.cpp b/util/draft/memory_ut.cpp index d683c870db..76bee30549 100644 --- a/util/draft/memory_ut.cpp +++ b/util/draft/memory_ut.cpp @@ -1,28 +1,28 @@ #include "memory.h" #include <library/cpp/testing/unittest/registar.h> - -#pragma pack(1) + +#pragma pack(1) struct Y_PACKED TSampleStruct1 { - ui8 A; - ui8 B; -}; - -#pragma pack(1) + ui8 A; + ui8 B; +}; + +#pragma pack(1) struct Y_PACKED TSampleStruct2 { - ui8 A; - ui16 B; - i32 C; -}; - -#pragma pack(1) + ui8 A; + ui16 B; + i32 C; +}; + +#pragma pack(1) struct Y_PACKED TSampleStruct3 { - TSampleStruct2 A; - ui64 B; -}; - -#pragma pack() - + TSampleStruct2 A; + ui64 B; +}; + +#pragma pack() + Y_UNIT_TEST_SUITE(TUtilDraftMemoryTest) { Y_UNIT_TEST(IsZeroTest) { ui8 a1 = 0; @@ -34,7 +34,7 @@ Y_UNIT_TEST_SUITE(TUtilDraftMemoryTest) { UNIT_ASSERT(!IsZero(a2)); a2 = 0; UNIT_ASSERT(IsZero(a2)); - + double a3 = 0.0; UNIT_ASSERT(IsZero(a3)); a3 = 1.e-13; @@ -47,7 +47,7 @@ Y_UNIT_TEST_SUITE(TUtilDraftMemoryTest) { ss1.A = 0; ss1.B = 12; UNIT_ASSERT(!IsZero(ss1)); - + TSampleStruct2 ss2; ss2.A = 0; ss2.B = 100; @@ -55,7 +55,7 @@ Y_UNIT_TEST_SUITE(TUtilDraftMemoryTest) { UNIT_ASSERT(!IsZero(ss2)); ss2.B = 0; UNIT_ASSERT(IsZero(ss2)); - + TSampleStruct3 ss3; ss3.A = ss2; ss3.B = 0; @@ -66,4 +66,4 @@ Y_UNIT_TEST_SUITE(TUtilDraftMemoryTest) { ss3.A.C = -789; UNIT_ASSERT(!IsZero(ss3)); } -} +} diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index 8c03084069..ffc9b09f96 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -375,12 +375,12 @@ char GetDirectorySeparator() { const char* GetDirectorySeparatorS() { return LOCSLASH_S; } - + void RemoveDirWithContents(TString dirName) { - SlashFolderLocal(dirName); - + SlashFolderLocal(dirName); + TDirIterator dir(dirName, TDirIterator::TOptions(FTS_NOSTAT)); - + for (auto it = dir.begin(); it != dir.end(); ++it) { switch (it->fts_info) { case FTS_F: @@ -392,8 +392,8 @@ void RemoveDirWithContents(TString dirName) { ythrow TSystemError() << "error while removing " << it->fts_path; break; } - } -} + } +} int mkpath(char* path, int mode) { return NFs::MakeDirectoryRecursive(path, NFs::EFilePermission(mode)) ? 0 : -1; diff --git a/util/folder/dirut.h b/util/folder/dirut.h index 456f1f6ebb..2537027b12 100644 --- a/util/folder/dirut.h +++ b/util/folder/dirut.h @@ -18,15 +18,15 @@ #include <time.h> #include <io.h> #include "dirent_win.h" - + // these live in mktemp_system.cpp extern "C" int mkstemps(char* path, int slen); char* mkdtemp(char* path); - + #else #ifdef _sun_ #include <alloca.h> - + char* mkdtemp(char* path); #endif #include <unistd.h> @@ -44,7 +44,7 @@ int mkpath(char* path, int mode = 0777); TString GetHomeDir(); void MakeDirIfNotExist(const char* path, int mode = 0777); - + inline void MakeDirIfNotExist(const TString& path, int mode = 0777) { MakeDirIfNotExist(path.data(), mode); } @@ -60,7 +60,7 @@ char GetDirectorySeparator(); const char* GetDirectorySeparatorS(); void RemoveDirWithContents(TString dirName); - + const char* GetFileNameComponent(const char* f); inline TString GetFileNameComponent(const TString& f) { diff --git a/util/folder/filelist.cpp b/util/folder/filelist.cpp index 48ecc2d51f..b21fcdbf20 100644 --- a/util/folder/filelist.cpp +++ b/util/folder/filelist.cpp @@ -27,7 +27,7 @@ void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringB TStringBuf filename = file->fts_path + dirNameLength + 1; if (filename.empty() || !filename.StartsWith(prefix) || !filename.EndsWith(suffix)) { - continue; + continue; } if (((Mask & EM_FILES) && file->fts_info == FTS_F) || ((Mask & EM_DIRS) && file->fts_info == FTS_D) || ((Mask & EM_SLINKS) && file->fts_info == FTS_SL)) { diff --git a/util/generic/guid.cpp b/util/generic/guid.cpp index 51c17970a5..8b907457bc 100644 --- a/util/generic/guid.cpp +++ b/util/generic/guid.cpp @@ -71,8 +71,8 @@ TString GetGuidAsString(const TGUID& g) { TString CreateGuidAsString() { return TGUID::Create().AsGuidString(); -} - +} + static bool GetDigit(const char c, ui32& digit) { digit = 0; if ('0' <= c && c <= '9') { diff --git a/util/generic/maybe.h b/util/generic/maybe.h index 54c43aaf59..34d21aebcd 100644 --- a/util/generic/maybe.h +++ b/util/generic/maybe.h @@ -378,8 +378,8 @@ public: } template <typename U> - TMaybe<U, Policy> Cast() const { - return Defined() ? TMaybe<U, Policy>(*Data()) : TMaybe<U, Policy>(); + TMaybe<U, Policy> Cast() const { + return Defined() ? TMaybe<U, Policy>(*Data()) : TMaybe<U, Policy>(); } constexpr explicit operator bool() const noexcept { @@ -451,9 +451,9 @@ private: template <class T> using TMaybeFail = TMaybe<T, NMaybe::TPolicyUndefinedFail>; -template <class T, class TPolicy = ::NMaybe::TPolicyUndefinedExcept> -constexpr TMaybe<std::decay_t<T>, TPolicy> MakeMaybe(T&& value) { - return TMaybe<std::decay_t<T>, TPolicy>(std::forward<T>(value)); +template <class T, class TPolicy = ::NMaybe::TPolicyUndefinedExcept> +constexpr TMaybe<std::decay_t<T>, TPolicy> MakeMaybe(T&& value) { + return TMaybe<std::decay_t<T>, TPolicy>(std::forward<T>(value)); } template <class T, class... TArgs> @@ -466,26 +466,26 @@ constexpr TMaybe<T> MakeMaybe(std::initializer_list<U> il, TArgs&&... args) { return TMaybe<T>(typename TMaybe<T>::TInPlace{}, il, std::forward<TArgs>(args)...); } -template <class T, class TPolicy> -void Swap(TMaybe<T, TPolicy>& lhs, TMaybe<T, TPolicy>& rhs) { +template <class T, class TPolicy> +void Swap(TMaybe<T, TPolicy>& lhs, TMaybe<T, TPolicy>& rhs) { lhs.Swap(rhs); } -template <class T, class TPolicy> -void swap(TMaybe<T, TPolicy>& lhs, TMaybe<T, TPolicy>& rhs) { +template <class T, class TPolicy> +void swap(TMaybe<T, TPolicy>& lhs, TMaybe<T, TPolicy>& rhs) { lhs.Swap(rhs); } -template <typename T, class TPolicy> -struct THash<TMaybe<T, TPolicy>> { - constexpr size_t operator()(const TMaybe<T, TPolicy>& data) const { +template <typename T, class TPolicy> +struct THash<TMaybe<T, TPolicy>> { + constexpr size_t operator()(const TMaybe<T, TPolicy>& data) const { return (data.Defined()) ? THash<T>()(data.GetRef()) : 42; } }; // Comparisons between TMaybe -template <class T, class TPolicy> -constexpr bool operator==(const ::TMaybe<T, TPolicy>& left, const ::TMaybe<T, TPolicy>& right) { +template <class T, class TPolicy> +constexpr bool operator==(const ::TMaybe<T, TPolicy>& left, const ::TMaybe<T, TPolicy>& right) { return (static_cast<bool>(left) != static_cast<bool>(right)) ? false : ( @@ -494,13 +494,13 @@ constexpr bool operator==(const ::TMaybe<T, TPolicy>& left, const ::TMaybe<T, TP : *left == *right); } -template <class T, class TPolicy> -constexpr bool operator!=(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { +template <class T, class TPolicy> +constexpr bool operator!=(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { return !(left == right); } -template <class T, class TPolicy> -constexpr bool operator<(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { +template <class T, class TPolicy> +constexpr bool operator<(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { return (!static_cast<bool>(right)) ? false : ( @@ -509,203 +509,203 @@ constexpr bool operator<(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy : (*left < *right)); } -template <class T, class TPolicy> -constexpr bool operator>(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { +template <class T, class TPolicy> +constexpr bool operator>(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { return right < left; } -template <class T, class TPolicy> -constexpr bool operator<=(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { +template <class T, class TPolicy> +constexpr bool operator<=(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { return !(right < left); } -template <class T, class TPolicy> -constexpr bool operator>=(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { +template <class T, class TPolicy> +constexpr bool operator>=(const TMaybe<T, TPolicy>& left, const TMaybe<T, TPolicy>& right) { return !(left < right); } // Comparisons with TNothing -template <class T, class TPolicy> -constexpr bool operator==(const TMaybe<T, TPolicy>& left, TNothing) noexcept { +template <class T, class TPolicy> +constexpr bool operator==(const TMaybe<T, TPolicy>& left, TNothing) noexcept { return !static_cast<bool>(left); } -template <class T, class TPolicy> -constexpr bool operator==(TNothing, const TMaybe<T, TPolicy>& right) noexcept { +template <class T, class TPolicy> +constexpr bool operator==(TNothing, const TMaybe<T, TPolicy>& right) noexcept { return !static_cast<bool>(right); } -template <class T, class TPolicy> -constexpr bool operator!=(const TMaybe<T, TPolicy>& left, TNothing) noexcept { +template <class T, class TPolicy> +constexpr bool operator!=(const TMaybe<T, TPolicy>& left, TNothing) noexcept { return static_cast<bool>(left); } -template <class T, class TPolicy> -constexpr bool operator!=(TNothing, const TMaybe<T, TPolicy>& right) noexcept { +template <class T, class TPolicy> +constexpr bool operator!=(TNothing, const TMaybe<T, TPolicy>& right) noexcept { return static_cast<bool>(right); } -template <class T, class TPolicy> -constexpr bool operator<(const TMaybe<T, TPolicy>&, TNothing) noexcept { +template <class T, class TPolicy> +constexpr bool operator<(const TMaybe<T, TPolicy>&, TNothing) noexcept { return false; } -template <class T, class TPolicy> -constexpr bool operator<(TNothing, const TMaybe<T, TPolicy>& right) noexcept { +template <class T, class TPolicy> +constexpr bool operator<(TNothing, const TMaybe<T, TPolicy>& right) noexcept { return static_cast<bool>(right); } -template <class T, class TPolicy> -constexpr bool operator<=(const TMaybe<T, TPolicy>& left, TNothing) noexcept { +template <class T, class TPolicy> +constexpr bool operator<=(const TMaybe<T, TPolicy>& left, TNothing) noexcept { return !static_cast<bool>(left); } -template <class T, class TPolicy> -constexpr bool operator<=(TNothing, const TMaybe<T, TPolicy>&) noexcept { +template <class T, class TPolicy> +constexpr bool operator<=(TNothing, const TMaybe<T, TPolicy>&) noexcept { return true; } -template <class T, class TPolicy> -constexpr bool operator>(const TMaybe<T, TPolicy>& left, TNothing) noexcept { +template <class T, class TPolicy> +constexpr bool operator>(const TMaybe<T, TPolicy>& left, TNothing) noexcept { return static_cast<bool>(left); } -template <class T, class TPolicy> -constexpr bool operator>(TNothing, const TMaybe<T, TPolicy>&) noexcept { +template <class T, class TPolicy> +constexpr bool operator>(TNothing, const TMaybe<T, TPolicy>&) noexcept { return false; } -template <class T, class TPolicy> -constexpr bool operator>=(const TMaybe<T, TPolicy>&, TNothing) noexcept { +template <class T, class TPolicy> +constexpr bool operator>=(const TMaybe<T, TPolicy>&, TNothing) noexcept { return true; } -template <class T, class TPolicy> -constexpr bool operator>=(TNothing, const TMaybe<T, TPolicy>& right) noexcept { +template <class T, class TPolicy> +constexpr bool operator>=(TNothing, const TMaybe<T, TPolicy>& right) noexcept { return !static_cast<bool>(right); } // Comparisons with T -template <class T, class TPolicy> -constexpr bool operator==(const TMaybe<T, TPolicy>& maybe, const T& value) { +template <class T, class TPolicy> +constexpr bool operator==(const TMaybe<T, TPolicy>& maybe, const T& value) { return static_cast<bool>(maybe) ? *maybe == value : false; } -template <class T, class TPolicy> -constexpr bool operator==(const T& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy> +constexpr bool operator==(const T& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? *maybe == value : false; } -template <class T, class TPolicy> -constexpr bool operator!=(const TMaybe<T, TPolicy>& maybe, const T& value) { +template <class T, class TPolicy> +constexpr bool operator!=(const TMaybe<T, TPolicy>& maybe, const T& value) { return static_cast<bool>(maybe) ? !(*maybe == value) : true; } -template <class T, class TPolicy> -constexpr bool operator!=(const T& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy> +constexpr bool operator!=(const T& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? !(*maybe == value) : true; } -template <class T, class TPolicy> -constexpr bool operator<(const TMaybe<T, TPolicy>& maybe, const T& value) { +template <class T, class TPolicy> +constexpr bool operator<(const TMaybe<T, TPolicy>& maybe, const T& value) { return static_cast<bool>(maybe) ? std::less<T>{}(*maybe, value) : true; } -template <class T, class TPolicy> -constexpr bool operator<(const T& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy> +constexpr bool operator<(const T& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? std::less<T>{}(value, *maybe) : false; } -template <class T, class TPolicy> -constexpr bool operator<=(const TMaybe<T, TPolicy>& maybe, const T& value) { +template <class T, class TPolicy> +constexpr bool operator<=(const TMaybe<T, TPolicy>& maybe, const T& value) { return !(maybe > value); } -template <class T, class TPolicy> -constexpr bool operator<=(const T& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy> +constexpr bool operator<=(const T& value, const TMaybe<T, TPolicy>& maybe) { return !(value > maybe); } -template <class T, class TPolicy> -constexpr bool operator>(const TMaybe<T, TPolicy>& maybe, const T& value) { +template <class T, class TPolicy> +constexpr bool operator>(const TMaybe<T, TPolicy>& maybe, const T& value) { return static_cast<bool>(maybe) ? value < maybe : false; } -template <class T, class TPolicy> -constexpr bool operator>(const T& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy> +constexpr bool operator>(const T& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? maybe < value : true; } -template <class T, class TPolicy> -constexpr bool operator>=(const TMaybe<T, TPolicy>& maybe, const T& value) { +template <class T, class TPolicy> +constexpr bool operator>=(const TMaybe<T, TPolicy>& maybe, const T& value) { return !(maybe < value); } -template <class T, class TPolicy> -constexpr bool operator>=(const T& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy> +constexpr bool operator>=(const T& value, const TMaybe<T, TPolicy>& maybe) { return !(value < maybe); } // Comparison with values convertible to T -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator==(const ::TMaybe<T, TPolicy>& maybe, const U& value) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator==(const ::TMaybe<T, TPolicy>& maybe, const U& value) { return static_cast<bool>(maybe) ? *maybe == value : false; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator==(const U& value, const ::TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator==(const U& value, const ::TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? *maybe == value : false; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator!=(const TMaybe<T, TPolicy>& maybe, const U& value) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator!=(const TMaybe<T, TPolicy>& maybe, const U& value) { return static_cast<bool>(maybe) ? !(*maybe == value) : true; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator!=(const U& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator!=(const U& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? !(*maybe == value) : true; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator<(const TMaybe<T, TPolicy>& maybe, const U& value) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator<(const TMaybe<T, TPolicy>& maybe, const U& value) { return static_cast<bool>(maybe) ? std::less<T>{}(*maybe, value) : true; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator<(const U& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator<(const U& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? std::less<T>{}(value, *maybe) : false; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator<=(const TMaybe<T, TPolicy>& maybe, const U& value) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator<=(const TMaybe<T, TPolicy>& maybe, const U& value) { return !(maybe > value); } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator<=(const U& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator<=(const U& value, const TMaybe<T, TPolicy>& maybe) { return !(value > maybe); } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator>(const TMaybe<T, TPolicy>& maybe, const U& value) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator>(const TMaybe<T, TPolicy>& maybe, const U& value) { return static_cast<bool>(maybe) ? value < maybe : false; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator>(const U& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator>(const U& value, const TMaybe<T, TPolicy>& maybe) { return static_cast<bool>(maybe) ? maybe < value : true; } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator>=(const TMaybe<T, TPolicy>& maybe, const U& value) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator>=(const TMaybe<T, TPolicy>& maybe, const U& value) { return !(maybe < value); } -template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> -constexpr bool operator>=(const U& value, const TMaybe<T, TPolicy>& maybe) { +template <class T, class TPolicy, class U, std::enable_if_t<std::is_convertible<U, T>::value, int> = 0> +constexpr bool operator>=(const U& value, const TMaybe<T, TPolicy>& maybe) { return !(value < maybe); } diff --git a/util/generic/variant.h b/util/generic/variant.h index 16b36cbfad..749fc75090 100644 --- a/util/generic/variant.h +++ b/util/generic/variant.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include "hash.h" #include <variant> diff --git a/util/generic/ymath.h b/util/generic/ymath.h index 4857b28566..9ff9ae2abe 100644 --- a/util/generic/ymath.h +++ b/util/generic/ymath.h @@ -172,35 +172,35 @@ inline bool FuzzyEquals(double p1, double p2, double eps = 1.0e-13) { inline bool FuzzyEquals(float p1, float p2, float eps = 1.0e-6) { return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); } - -namespace NUtilMathPrivate { - template <bool IsSigned> - struct TCeilDivImpl {}; - - template <> - struct TCeilDivImpl<true> { + +namespace NUtilMathPrivate { + template <bool IsSigned> + struct TCeilDivImpl {}; + + template <> + struct TCeilDivImpl<true> { template <class T> - static inline T Do(T x, T y) noexcept { - return x / y + (((x < 0) ^ (y > 0)) && (x % y)); - } - }; - - template <> - struct TCeilDivImpl<false> { + static inline T Do(T x, T y) noexcept { + return x / y + (((x < 0) ^ (y > 0)) && (x % y)); + } + }; + + template <> + struct TCeilDivImpl<false> { template <class T> - static inline T Do(T x, T y) noexcept { - auto quot = x / y; - return (x % y) ? (quot + 1) : quot; - } - }; -} - -/** - * @returns Equivalent to ceil((double) x / (double) y) but using only integer arithmetic operations - */ -template <class T> -inline T CeilDiv(T x, T y) noexcept { - static_assert(std::is_integral<T>::value, "Integral type required."); - Y_ASSERT(y != 0); - return ::NUtilMathPrivate::TCeilDivImpl<std::is_signed<T>::value>::Do(x, y); -} + static inline T Do(T x, T y) noexcept { + auto quot = x / y; + return (x % y) ? (quot + 1) : quot; + } + }; +} + +/** + * @returns Equivalent to ceil((double) x / (double) y) but using only integer arithmetic operations + */ +template <class T> +inline T CeilDiv(T x, T y) noexcept { + static_assert(std::is_integral<T>::value, "Integral type required."); + Y_ASSERT(y != 0); + return ::NUtilMathPrivate::TCeilDivImpl<std::is_signed<T>::value>::Do(x, y); +} diff --git a/util/generic/ymath_ut.cpp b/util/generic/ymath_ut.cpp index 15514e4bb3..29190b55eb 100644 --- a/util/generic/ymath_ut.cpp +++ b/util/generic/ymath_ut.cpp @@ -34,7 +34,7 @@ class TMathTest: public TTestBase { UNIT_TEST(TestAbs); UNIT_TEST(TestPower); UNIT_TEST(TestSigmoid); - UNIT_TEST(TestCeilDiv); + UNIT_TEST(TestCeilDiv); UNIT_TEST_SUITE_END(); private: @@ -45,7 +45,7 @@ private: void TestAbs(); void TestPower(); void TestSigmoid(); - void TestCeilDiv(); + void TestCeilDiv(); inline void TestIsValidFloat() { UNIT_ASSERT(IsValidFloat(-Max<double>() / 2.)); @@ -191,30 +191,30 @@ void TMathTest::TestSigmoid() { UNIT_ASSERT_EQUAL(Sigmoid(-5000.), 0.0); UNIT_ASSERT_EQUAL(Sigmoid(5000.), 1.0); } - -void TMathTest::TestCeilDiv() { - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui8>(2, 3), 1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui8>(3, 3), 1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui32>(12, 2), 6); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui64>(10, 3), 4); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui64>(0, 10), 0); - - // negative numbers - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(0, -10), 0); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-1, 2), 0); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-1, -2), 1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(10, -5), -2); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-3, -4), 1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-6, -4), 2); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-6, 4), -1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-13, 4), -3); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-14, -4), 4); - - // check values close to overflow - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui8>(255, 10), 26); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui32>(std::numeric_limits<ui32>::max() - 3, std::numeric_limits<ui32>::max()), 1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i32>(std::numeric_limits<i32>::max() - 3, std::numeric_limits<i32>::max()), 1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i32>(std::numeric_limits<i32>::min(), std::numeric_limits<i32>::max()), -1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i8>(std::numeric_limits<i8>::max(), std::numeric_limits<i8>::min() + 1), -1); - UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i64>(std::numeric_limits<i64>::max() - 2, -(std::numeric_limits<i64>::min() + 1)), 1); -} + +void TMathTest::TestCeilDiv() { + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui8>(2, 3), 1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui8>(3, 3), 1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui32>(12, 2), 6); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui64>(10, 3), 4); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui64>(0, 10), 0); + + // negative numbers + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(0, -10), 0); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-1, 2), 0); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-1, -2), 1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(10, -5), -2); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-3, -4), 1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-6, -4), 2); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-6, 4), -1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-13, 4), -3); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv(-14, -4), 4); + + // check values close to overflow + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui8>(255, 10), 26); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<ui32>(std::numeric_limits<ui32>::max() - 3, std::numeric_limits<ui32>::max()), 1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i32>(std::numeric_limits<i32>::max() - 3, std::numeric_limits<i32>::max()), 1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i32>(std::numeric_limits<i32>::min(), std::numeric_limits<i32>::max()), -1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i8>(std::numeric_limits<i8>::max(), std::numeric_limits<i8>::min() + 1), -1); + UNIT_ASSERT_VALUES_EQUAL(CeilDiv<i64>(std::numeric_limits<i64>::max() - 2, -(std::numeric_limits<i64>::min() + 1)), 1); +} diff --git a/util/memory/segpool_alloc.h b/util/memory/segpool_alloc.h index d38c000774..1a83b7a543 100644 --- a/util/memory/segpool_alloc.h +++ b/util/memory/segpool_alloc.h @@ -77,10 +77,10 @@ struct segpool_alloc { if (!pool) { Y_IF_DEBUG(malloc_free_count++); free(__p); - } else { + } else { Y_IF_DEBUG(pool_free_count++); - ; - } + ; + } } ~segpool_alloc() { //assert(pool_count == pool_free_count && malloc_count == malloc_free_count); <- uncomment when swap() problem is solved diff --git a/util/network/ip.h b/util/network/ip.h index 7fd0b63ae1..dc7c2d24a0 100644 --- a/util/network/ip.h +++ b/util/network/ip.h @@ -14,28 +14,28 @@ using TIpHost = ui32; /// Port number in host format using TIpPort = ui16; -/* +/* * ipStr is in 'ddd.ddd.ddd.ddd' format * returns IPv4 address in inet format */ -static inline TIpHost IpFromString(const char* ipStr) { - in_addr ia; +static inline TIpHost IpFromString(const char* ipStr) { + in_addr ia; - if (inet_aton(ipStr, &ia) == 0) { + if (inet_aton(ipStr, &ia) == 0) { ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ip address"; - } + } + + return (ui32)ia.s_addr; +} - return (ui32)ia.s_addr; -} - static inline char* IpToString(TIpHost ip, char* buf, size_t len) { if (!inet_ntop(AF_INET, (void*)&ip, buf, (socklen_t)len)) { ythrow TSystemError() << "Failed to get ip address string"; - } - + } + return buf; -} - +} + static inline TString IpToString(TIpHost ip) { char buf[INET_ADDRSTRLEN]; diff --git a/util/network/ip_ut.cpp b/util/network/ip_ut.cpp index 753d62cd0e..6716c6a699 100644 --- a/util/network/ip_ut.cpp +++ b/util/network/ip_ut.cpp @@ -1,10 +1,10 @@ #include "ip.h" #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/yexception.h> - -class TSysIpTest: public TTestBase { + +class TSysIpTest: public TTestBase { UNIT_TEST_SUITE(TSysIpTest); UNIT_TEST(TestIpFromString); UNIT_TEST_EXCEPTION(TestIpFromString2, yexception); @@ -21,43 +21,43 @@ private: void TestIpFromString4(); void TestIpFromString5(); void TestIpToString(); -}; - -UNIT_TEST_SUITE_REGISTRATION(TSysIpTest); - +}; + +UNIT_TEST_SUITE_REGISTRATION(TSysIpTest); + void TSysIpTest::TestIpFromString() { - const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"}; - ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}}; - + const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"}; + ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}}; + for (size_t i = 0; i < Y_ARRAY_SIZE(ipStr); ++i) { const ui32 ip = IpFromString(ipStr[i]); - UNIT_ASSERT(memcmp(&ip, ipArr[i], sizeof(ui32)) == 0); - } -} - + UNIT_ASSERT(memcmp(&ip, ipArr[i], sizeof(ui32)) == 0); + } +} + void TSysIpTest::TestIpFromString2() { IpFromString("XXXXXXWXW"); -} - +} + void TSysIpTest::TestIpFromString3() { IpFromString("986.0.37.255"); -} - +} + void TSysIpTest::TestIpFromString4() { IpFromString("256.0.22.365"); -} - +} + void TSysIpTest::TestIpFromString5() { IpFromString("245.12..0"); -} - -void TSysIpTest::TestIpToString() { - ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}}; - - const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"}; - +} + +void TSysIpTest::TestIpToString() { + ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}}; + + const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"}; + for (size_t i = 0; i < Y_ARRAY_SIZE(ipStr); ++i) { UNIT_ASSERT(IpToString(*reinterpret_cast<TIpHost*>(&(ipArr[i]))) == ipStr[i]); - } -} + } +} diff --git a/util/network/socket.cpp b/util/network/socket.cpp index 192656f57a..4f6e804346 100644 --- a/util/network/socket.cpp +++ b/util/network/socket.cpp @@ -49,7 +49,7 @@ using namespace NAddr; #if defined(_win_) - + int inet_aton(const char* cp, struct in_addr* inp) { sockaddr_in addr; addr.sin_family = AF_INET; diff --git a/util/network/socket.h b/util/network/socket.h index 4fb3926110..40c8648b40 100644 --- a/util/network/socket.h +++ b/util/network/socket.h @@ -14,10 +14,10 @@ #include <cerrno> -#ifndef INET_ADDRSTRLEN +#ifndef INET_ADDRSTRLEN #define INET_ADDRSTRLEN 16 -#endif - +#endif + #if defined(_unix_) #define get_host_error() h_errno #elif defined(_win_) @@ -46,15 +46,15 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout) noexcept; #else #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) #endif - + int inet_aton(const char* cp, struct in_addr* inp); - + #define get_host_error() WSAGetLastError() #define SHUT_RD SD_RECEIVE #define SHUT_WR SD_SEND #define SHUT_RDWR SD_BOTH - + #define INFTIM (-1) #endif diff --git a/util/random/entropy.cpp b/util/random/entropy.cpp index 77e9ba3170..3617edb83d 100644 --- a/util/random/entropy.cpp +++ b/util/random/entropy.cpp @@ -14,7 +14,7 @@ #include <util/system/info.h> #include <util/system/spinlock.h> #include <util/system/thread.h> -#include <util/system/execpath.h> +#include <util/system/execpath.h> #include <util/system/datetime.h> #include <util/system/hostname.h> #include <util/system/getpid.h> diff --git a/util/stream/debug.cpp b/util/stream/debug.cpp index 69ce49155c..afd5b3e1c7 100644 --- a/util/stream/debug.cpp +++ b/util/stream/debug.cpp @@ -30,7 +30,7 @@ namespace { Level = 0; } } - + IOutputStream* Out; int Level; }; diff --git a/util/stream/input.cpp b/util/stream/input.cpp index 5a01c2f1ae..6e8170f2f9 100644 --- a/util/stream/input.cpp +++ b/util/stream/input.cpp @@ -258,19 +258,19 @@ IInputStream& NPrivate::StdInStream() noexcept { // helper functions -static inline bool IsStdDelimiter(char c) { +static inline bool IsStdDelimiter(char c) { return (c == '\0') || (c == ' ') || (c == '\r') || (c == '\n') || (c == '\t'); } static void ReadUpToDelimiter(IInputStream& i, TString& s) { char c; while (i.ReadChar(c)) { // skip delimiters - if (!IsStdDelimiter(c)) { + if (!IsStdDelimiter(c)) { s += c; break; } } - while (i.ReadChar(c) && !IsStdDelimiter(c)) { // read data (with trailing delimiter) + while (i.ReadChar(c) && !IsStdDelimiter(c)) { // read data (with trailing delimiter) s += c; } } @@ -279,7 +279,7 @@ static void ReadUpToDelimiter(IInputStream& i, TString& s) { template <> void In<TString>(IInputStream& i, TString& s) { - s.resize(0); + s.resize(0); ReadUpToDelimiter(i, s); } diff --git a/util/stream/mem.h b/util/stream/mem.h index 878a2ff194..18a5d46772 100644 --- a/util/stream/mem.h +++ b/util/stream/mem.h @@ -82,7 +82,7 @@ public: const char* Buf() const noexcept { return Buf_; } - + /** * Initializes this stream with a next chunk extracted from the given zero * copy stream. diff --git a/util/stream/null.cpp b/util/stream/null.cpp index 66519fb796..4e8b298145 100644 --- a/util/stream/null.cpp +++ b/util/stream/null.cpp @@ -7,10 +7,10 @@ TNullIO& NPrivate::StdNullStream() noexcept { } TNullInput::TNullInput() noexcept { -} - +} + TNullInput::~TNullInput() = default; - + size_t TNullInput::DoRead(void*, size_t) { return 0; } @@ -24,7 +24,7 @@ size_t TNullInput::DoNext(const void**, size_t) { } TNullOutput::TNullOutput() noexcept = default; - + TNullOutput::~TNullOutput() = default; void TNullOutput::DoWrite(const void* /*buf*/, size_t /*len*/) { diff --git a/util/stream/pipe.cpp b/util/stream/pipe.cpp index d0e1a78b57..51be1934a7 100644 --- a/util/stream/pipe.cpp +++ b/util/stream/pipe.cpp @@ -33,17 +33,17 @@ public: TPipeBase::TPipeBase(const TString& command, const char* mode) : Impl_(new TImpl(command, mode)) -{ -} - +{ +} + TPipeBase::~TPipeBase() = default; - + TPipeInput::TPipeInput(const TString& command) : TPipeBase(command, "r") { } -size_t TPipeInput::DoRead(void* buf, size_t len) { +size_t TPipeInput::DoRead(void* buf, size_t len) { if (Impl_->Pipe_ == nullptr) { return 0; } @@ -59,8 +59,8 @@ size_t TPipeInput::DoRead(void* buf, size_t len) { } } return bytesRead; -} - +} + TPipeOutput::TPipeOutput(const TString& command) : TPipeBase(command, "w") { diff --git a/util/stream/pipe.h b/util/stream/pipe.h index bbd98a6fbd..18525b9517 100644 --- a/util/stream/pipe.h +++ b/util/stream/pipe.h @@ -46,11 +46,11 @@ public: * @param command Command line to start a process with. */ TPipeInput(const TString& command); - + private: size_t DoRead(void* buf, size_t len) override; }; - + /** * Output stream that binds to a standard input stream of a newly started process. * @@ -74,8 +74,8 @@ public: private: void DoWrite(const void* buf, size_t len) override; -}; - +}; + class TPipedBase { protected: TPipedBase(PIPEHANDLE fd); diff --git a/util/stream/tee.cpp b/util/stream/tee.cpp index d059c367d0..99873b95ba 100644 --- a/util/stream/tee.cpp +++ b/util/stream/tee.cpp @@ -4,21 +4,21 @@ TTeeOutput::TTeeOutput(IOutputStream* l, IOutputStream* r) noexcept : L_(l) , R_(r) { -} - +} + TTeeOutput::~TTeeOutput() = default; - -void TTeeOutput::DoWrite(const void* buf, size_t len) { + +void TTeeOutput::DoWrite(const void* buf, size_t len) { L_->Write(buf, len); R_->Write(buf, len); -} - -void TTeeOutput::DoFlush() { +} + +void TTeeOutput::DoFlush() { L_->Flush(); R_->Flush(); -} - -void TTeeOutput::DoFinish() { +} + +void TTeeOutput::DoFinish() { L_->Finish(); R_->Finish(); -} +} diff --git a/util/stream/tee.h b/util/stream/tee.h index 623d44be32..c69e232fb9 100644 --- a/util/stream/tee.h +++ b/util/stream/tee.h @@ -14,15 +14,15 @@ class TTeeOutput: public IOutputStream { public: TTeeOutput(IOutputStream* l, IOutputStream* r) noexcept; ~TTeeOutput() override; - + private: void DoWrite(const void* buf, size_t len) override; void DoFlush() override; void DoFinish() override; - + private: IOutputStream* L_; IOutputStream* R_; -}; - +}; + /** @} */ diff --git a/util/stream/zlib.cpp b/util/stream/zlib.cpp index eb60ac9dc4..60f4e9439f 100644 --- a/util/stream/zlib.cpp +++ b/util/stream/zlib.cpp @@ -348,11 +348,11 @@ void TZLibCompress::TDestruct::Destroy(TImpl* impl) { } TZLibCompress::~TZLibCompress() { - try { - Finish(); - } catch (...) { + try { + Finish(); + } catch (...) { // ¯\_(ツ)_/¯ - } + } } void TZLibCompress::DoWrite(const void* buf, size_t size) { diff --git a/util/string/cast.cpp b/util/string/cast.cpp index 75741efe21..aa1e65a8e9 100644 --- a/util/string/cast.cpp +++ b/util/string/cast.cpp @@ -54,7 +54,7 @@ namespace { template <class T> std::enable_if_t<std::is_signed<T>::value, std::make_unsigned_t<T>> NegateNegativeSigned(T value) noexcept { return std::make_unsigned_t<T>(-(value + 1)) + std::make_unsigned_t<T>(1); - } + } template <class T> std::enable_if_t<std::is_unsigned<T>::value, std::make_unsigned_t<T>> NegateNegativeSigned(T) noexcept { @@ -482,29 +482,29 @@ size_t ToStringImpl<bool>(bool t, char* buf, size_t len) { /* * ------------------------------ parsers ------------------------------ */ - -template <> + +template <> bool TryFromStringImpl<bool>(const char* data, size_t len, bool& result) { - if (len == 1) { - if (data[0] == '0') { + if (len == 1) { + if (data[0] == '0') { result = false; return true; - } else if (data[0] == '1') { + } else if (data[0] == '1') { result = true; - return true; - } - } - TStringBuf buf(data, len); + return true; + } + } + TStringBuf buf(data, len); if (IsTrue(buf)) { result = true; - return true; + return true; } else if (IsFalse(buf)) { result = false; return true; - } + } return false; -} - +} + template <> bool FromStringImpl<bool>(const char* data, size_t len) { bool result; diff --git a/util/string/cast.h b/util/string/cast.h index 7a176a5c77..90e925c194 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -124,7 +124,7 @@ struct TFromStringException: public TBadCastException { /* * specialized for: - * bool + * bool * short * unsigned short * int diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index e7be19c96a..033450c38c 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -40,31 +40,31 @@ UNIT_ASSERT_VALUES_EQUAL(TryIntFromStringForCheck<base>(IntToString<base>(val), result), false); \ UNIT_ASSERT_VALUES_EQUAL(result, def); -template <class A> -struct TRet { +template <class A> +struct TRet { template <int base> inline A IntFromStringForCheck(const TString& str) { - return IntFromString<A, base>(str); - } + return IntFromString<A, base>(str); + } template <int base> inline bool TryIntFromStringForCheck(const TString& str, A& result) { return TryIntFromString<base>(str, result); } - template <class B> + template <class B> inline void CheckOK(B v) { UNIT_ASSERT_VALUES_EQUAL(FromString<A>(ToString(v)), v); // char UNIT_ASSERT_VALUES_EQUAL(FromString<A>(ToWtring(v)), v); // wide char HEX_MACROS_MAP(OK_HEX_CHECK, A, v); - } + } - template <class B> + template <class B> inline void CheckExc(B v) { UNIT_ASSERT_EXCEPTION(FromString<A>(ToString(v)), yexception); // char UNIT_ASSERT_EXCEPTION(FromString<A>(ToWtring(v)), yexception); // wide char HEX_MACROS_MAP(EXC_HEX_CHECK, A, v); - } + } template <class B> inline void CheckTryOK(B v) { @@ -89,16 +89,16 @@ struct TRet { TRY_HEX_MACROS_MAP(TRY_FAIL_HEX_CHECK, A, v, convV, defaultV); } -}; - -template <> -struct TRet<bool> { - template <class B> +}; + +template <> +struct TRet<bool> { + template <class B> inline void CheckOK(B v) { UNIT_ASSERT_VALUES_EQUAL(FromString<bool>(ToString(v)), v); - } - - template <class B> + } + + template <class B> inline void CheckTryOK(B v) { B convV; UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), true); @@ -107,8 +107,8 @@ struct TRet<bool> { template <class B> inline void CheckExc(B v) { - UNIT_ASSERT_EXCEPTION(FromString<bool>(ToString(v)), yexception); - } + UNIT_ASSERT_EXCEPTION(FromString<bool>(ToString(v)), yexception); + } template <class B> inline void CheckTryFail(B v) { @@ -117,13 +117,13 @@ struct TRet<bool> { UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), false); UNIT_ASSERT_VALUES_EQUAL(defaultV, convV); } -}; - -template <class A> -inline TRet<A> F() { - return TRet<A>(); -}; - +}; + +template <class A> +inline TRet<A> F() { + return TRet<A>(); +}; + #if 0 template <class T> inline void CheckConvertToBuffer(const T& value, const size_t size, const TString& canonValue) { @@ -181,7 +181,7 @@ Y_UNIT_TEST_SUITE(TCastTest) { test1(bool, false); test2(bool, ""); test2(bool, "a"); - + test2(ui8, -1); test1(i8, -1); test1(i8, SCHAR_MAX); diff --git a/util/string/type.cpp b/util/string/type.cpp index c6b6f7aea5..49671c02c2 100644 --- a/util/string/type.cpp +++ b/util/string/type.cpp @@ -72,7 +72,7 @@ bool IsTrue(const TStringBuf v) noexcept { "da"}; return IsCaseInsensitiveAnyOf(v, trueOptions); } - + bool IsFalse(const TStringBuf v) noexcept { static constexpr std::array<TStringBuf, 7> falseOptions{ "false", @@ -83,4 +83,4 @@ bool IsFalse(const TStringBuf v) noexcept { "0", "net"}; return IsCaseInsensitiveAnyOf(v, falseOptions); -} +} diff --git a/util/system/compat.cpp b/util/system/compat.cpp index 1c8c9b5289..18fbfa296a 100644 --- a/util/system/compat.cpp +++ b/util/system/compat.cpp @@ -17,11 +17,11 @@ #endif #ifndef HAVE_NATIVE_GETPROGNAME -const char* getprogname() { +const char* getprogname() { return GetProgramName().data(); -} +} #endif - + #ifdef _win_ void sleep(i64 len) { diff --git a/util/system/compat.h b/util/system/compat.h index 205e79c8f6..c53dbcca17 100644 --- a/util/system/compat.h +++ b/util/system/compat.h @@ -12,8 +12,8 @@ #if defined(_win_) #include <process.h> -#endif - +#endif + extern "C" { #if defined(_win_) using pid_t = int; @@ -34,8 +34,8 @@ extern "C" { #ifndef HAVE_NATIVE_GETPROGNAME const char* getprogname(); -#endif - +#endif + #if defined(_MSC_VER) void err(int e, const char* m, ...); void errx(int e, const char* m, ...); diff --git a/util/system/execpath.cpp b/util/system/execpath.cpp index 7cff2d5973..33198af58b 100644 --- a/util/system/execpath.cpp +++ b/util/system/execpath.cpp @@ -9,9 +9,9 @@ #elif defined(_win_) #include "winint.h" #include <io.h> -#elif defined(_linux_) +#elif defined(_linux_) #include <unistd.h> -#elif defined(_freebsd_) +#elif defined(_freebsd_) #include <string.h> #include <sys/types.h> // for u_int not defined in sysctl.h #include <sys/sysctl.h> @@ -28,7 +28,7 @@ #include <util/string/cast.h> #include "filemap.h" -#include "execpath.h" +#include "execpath.h" #include "fs.h" #if defined(_freebsd_) @@ -36,28 +36,28 @@ static inline bool GoodPath(const TString& path) { return path.find('/') != TString::npos; } -static inline int FreeBSDSysCtl(int* mib, size_t mibSize, TTempBuf& res) { - for (size_t i = 0; i < 2; ++i) { - size_t cb = res.Size(); +static inline int FreeBSDSysCtl(int* mib, size_t mibSize, TTempBuf& res) { + for (size_t i = 0; i < 2; ++i) { + size_t cb = res.Size(); if (sysctl(mib, mibSize, res.Data(), &cb, nullptr, 0) == 0) { - res.Proceed(cb); - return 0; - } else if (errno == ENOMEM) { - res = TTempBuf(cb); - } else { - return errno; - } + res.Proceed(cb); + return 0; + } else if (errno == ENOMEM) { + res = TTempBuf(cb); + } else { + return errno; + } } - return errno; + return errno; } static inline TString FreeBSDGetExecPath() { int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; - TTempBuf buf; + TTempBuf buf; int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf); - if (r == 0) { + if (r == 0) { return TString(buf.Data(), buf.Filled() - 1); - } else if (r == ENOTSUP) { // older FreeBSD version + } else if (r == ENOTSUP) { // older FreeBSD version /* * BSD analogue for /proc/self is /proc/curproc. * See: @@ -65,7 +65,7 @@ static inline TString FreeBSDGetExecPath() { */ TString path("/proc/curproc/file"); return NFs::ReadLink(path); - } else { + } else { return TString(); } } @@ -74,56 +74,56 @@ static inline TString FreeBSDGetArgv0() { int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, getpid()}; TTempBuf buf; int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf); - if (r == 0) { + if (r == 0) { return TString(buf.Data()); - } else if (r == ENOTSUP) { + } else if (r == ENOTSUP) { return TString(); - } else { - ythrow yexception() << "FreeBSDGetArgv0() failed: " << LastSystemErrorText(); - } + } else { + ythrow yexception() << "FreeBSDGetArgv0() failed: " << LastSystemErrorText(); + } } static inline bool FreeBSDGuessExecPath(const TString& guessPath, TString& execPath) { if (NFs::Exists(guessPath)) { - // now it should work for real - execPath = FreeBSDGetExecPath(); - if (RealPath(execPath) == RealPath(guessPath)) { - return true; - } - } - return false; -} - + // now it should work for real + execPath = FreeBSDGetExecPath(); + if (RealPath(execPath) == RealPath(guessPath)) { + return true; + } + } + return false; +} + static inline bool FreeBSDGuessExecBasePath(const TString& guessBasePath, TString& execPath) { return FreeBSDGuessExecPath(TString(guessBasePath) + "/" + getprogname(), execPath); -} - +} + #endif static TString GetExecPathImpl() { #if defined(_solaris_) return execname(); #elif defined(_darwin_) - TTempBuf execNameBuf; - for (size_t i = 0; i < 2; ++i) { + TTempBuf execNameBuf; + for (size_t i = 0; i < 2; ++i) { std::remove_pointer_t<TFunctionArg<decltype(_NSGetExecutablePath), 1>> bufsize = execNameBuf.Size(); int r = _NSGetExecutablePath(execNameBuf.Data(), &bufsize); if (r == 0) { return execNameBuf.Data(); - } else if (r == -1) { - execNameBuf = TTempBuf(bufsize); + } else if (r == -1) { + execNameBuf = TTempBuf(bufsize); } } - ythrow yexception() << "GetExecPathImpl() failed"; + ythrow yexception() << "GetExecPathImpl() failed"; #elif defined(_win_) - TTempBuf execNameBuf; + TTempBuf execNameBuf; for (;;) { DWORD r = GetModuleFileName(nullptr, execNameBuf.Data(), execNameBuf.Size()); - if (r == execNameBuf.Size()) { - execNameBuf = TTempBuf(execNameBuf.Size() * 2); - } else if (r == 0) { - ythrow yexception() << "GetExecPathImpl() failed: " << LastSystemErrorText(); - } else { + if (r == execNameBuf.Size()) { + execNameBuf = TTempBuf(execNameBuf.Size() * 2); + } else if (r == 0) { + ythrow yexception() << "GetExecPathImpl() failed: " << LastSystemErrorText(); + } else { return execNameBuf.Data(); } } @@ -133,23 +133,23 @@ static TString GetExecPathImpl() { // TODO(yoda): check if the filename ends with " (deleted)" #elif defined(_freebsd_) TString execPath = FreeBSDGetExecPath(); - if (GoodPath(execPath)) { - return execPath; + if (GoodPath(execPath)) { + return execPath; + } + if (FreeBSDGuessExecPath(FreeBSDGetArgv0(), execPath)) { + return execPath; } - if (FreeBSDGuessExecPath(FreeBSDGetArgv0(), execPath)) { - return execPath; + if (FreeBSDGuessExecPath(getenv("_"), execPath)) { + return execPath; } - if (FreeBSDGuessExecPath(getenv("_"), execPath)) { - return execPath; + if (FreeBSDGuessExecBasePath(getenv("PWD"), execPath)) { + return execPath; } - if (FreeBSDGuessExecBasePath(getenv("PWD"), execPath)) { - return execPath; - } if (FreeBSDGuessExecBasePath(NFs::CurrentWorkingDirectory(), execPath)) { - return execPath; - } + return execPath; + } - ythrow yexception() << "can not resolve exec path"; + ythrow yexception() << "can not resolve exec path"; #else #error dont know how to implement GetExecPath on this platform #endif diff --git a/util/system/execpath_ut.cpp b/util/system/execpath_ut.cpp index d83523de15..16b01466f5 100644 --- a/util/system/execpath_ut.cpp +++ b/util/system/execpath_ut.cpp @@ -14,7 +14,7 @@ Y_UNIT_TEST_SUITE(TExecPathTest) { UNIT_ASSERT(NFs::Exists(execPath)); UNIT_ASSERT(NFs::Exists(persistentExecPath)); } catch (...) { - Cerr << execPath << Endl; + Cerr << execPath << Endl; throw; } diff --git a/util/system/info.cpp b/util/system/info.cpp index dacad12282..cf6681e89a 100644 --- a/util/system/info.cpp +++ b/util/system/info.cpp @@ -1,7 +1,7 @@ #include "info.h" -#include "error.h" - +#include "error.h" + #include <cstdlib> #if defined(_linux_) || defined(_cygwin_) @@ -26,7 +26,7 @@ static int getloadavg(double* loadavg, int nelem) { return nelem; } -#elif defined(_unix_) || defined(_darwin_) +#elif defined(_unix_) || defined(_darwin_) #include <sys/types.h> #endif @@ -38,7 +38,7 @@ static int getloadavg(double* loadavg, int nelem) { #include <util/string/cast.h> #include <util/string/strip.h> #include <util/stream/file.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #if defined(_linux_) static inline size_t CgroupCpus() { @@ -204,25 +204,25 @@ size_t NSystemInfo::TotalMemorySize() { sysinfo(&info); return info.totalram; #elif defined(_darwin_) - int mib[2]; - int64_t memSize; - size_t length; - - // Get the Physical memory size - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - length = sizeof(int64_t); - if (sysctl(mib, 2, &memSize, &length, NULL, 0) != 0) { - ythrow yexception() << "sysctl failed: " << LastSystemErrorText(); - } - return (size_t)memSize; + int mib[2]; + int64_t memSize; + size_t length; + + // Get the Physical memory size + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + length = sizeof(int64_t); + if (sysctl(mib, 2, &memSize, &length, NULL, 0) != 0) { + ythrow yexception() << "sysctl failed: " << LastSystemErrorText(); + } + return (size_t)memSize; #elif defined(_win_) - MEMORYSTATUSEX memoryStatusEx; - memoryStatusEx.dwLength = sizeof(memoryStatusEx); - if (!GlobalMemoryStatusEx(&memoryStatusEx)) { - ythrow yexception() << "GlobalMemoryStatusEx failed: " << LastSystemErrorText(); - } - return (size_t)memoryStatusEx.ullTotalPhys; + MEMORYSTATUSEX memoryStatusEx; + memoryStatusEx.dwLength = sizeof(memoryStatusEx); + if (!GlobalMemoryStatusEx(&memoryStatusEx)) { + ythrow yexception() << "GlobalMemoryStatusEx failed: " << LastSystemErrorText(); + } + return (size_t)memoryStatusEx.ullTotalPhys; #else return 0; #endif diff --git a/util/system/interrupt_signals.cpp b/util/system/interrupt_signals.cpp index 61ff8dd720..96b723f2b8 100644 --- a/util/system/interrupt_signals.cpp +++ b/util/system/interrupt_signals.cpp @@ -1,60 +1,60 @@ -#include "interrupt_signals.h" - -#include "compiler.h" -#include "error.h" - -#include <util/generic/yexception.h> - -#include <csignal> - -static void (*InterruptSignalsHandler)(int signum) = nullptr; - -#ifdef _win_ - +#include "interrupt_signals.h" + +#include "compiler.h" +#include "error.h" + +#include <util/generic/yexception.h> + +#include <csignal> + +static void (*InterruptSignalsHandler)(int signum) = nullptr; + +#ifdef _win_ + #include <windows.h> - -static BOOL WINAPI WindowsSignalsHandler(_In_ DWORD dwCtrlType) { - if (!InterruptSignalsHandler) { - return FALSE; - } - - switch (dwCtrlType) { - case CTRL_C_EVENT: - InterruptSignalsHandler(SIGINT); - return TRUE; - case CTRL_BREAK_EVENT: - InterruptSignalsHandler(SIGTERM); - return TRUE; - case CTRL_CLOSE_EVENT: - InterruptSignalsHandler(SIGHUP); - return TRUE; - default: - return FALSE; - } - Y_UNREACHABLE(); -} - -#endif - -// separate function is to enforce 'extern "C"' linkage -extern "C" void CppSignalsHandler(int signum) { - if (InterruptSignalsHandler) { - InterruptSignalsHandler(signum); - } -} - -void SetInterruptSignalsHandler(void (*handler)(int signum)) { - InterruptSignalsHandler = handler; -#ifdef _win_ - if (!SetConsoleCtrlHandler(WindowsSignalsHandler, TRUE)) { - ythrow TSystemError() << "SetConsoleCtrlHandler failed: " << LastSystemErrorText(); - } - for (int signum : {SIGINT, SIGTERM}) { -#else - for (int signum : {SIGINT, SIGTERM, SIGHUP}) { -#endif - if (std::signal(signum, CppSignalsHandler) == SIG_ERR) { - ythrow TSystemError() << "std::signal failed to set handler for signal with id " << signum; - } - } -} + +static BOOL WINAPI WindowsSignalsHandler(_In_ DWORD dwCtrlType) { + if (!InterruptSignalsHandler) { + return FALSE; + } + + switch (dwCtrlType) { + case CTRL_C_EVENT: + InterruptSignalsHandler(SIGINT); + return TRUE; + case CTRL_BREAK_EVENT: + InterruptSignalsHandler(SIGTERM); + return TRUE; + case CTRL_CLOSE_EVENT: + InterruptSignalsHandler(SIGHUP); + return TRUE; + default: + return FALSE; + } + Y_UNREACHABLE(); +} + +#endif + +// separate function is to enforce 'extern "C"' linkage +extern "C" void CppSignalsHandler(int signum) { + if (InterruptSignalsHandler) { + InterruptSignalsHandler(signum); + } +} + +void SetInterruptSignalsHandler(void (*handler)(int signum)) { + InterruptSignalsHandler = handler; +#ifdef _win_ + if (!SetConsoleCtrlHandler(WindowsSignalsHandler, TRUE)) { + ythrow TSystemError() << "SetConsoleCtrlHandler failed: " << LastSystemErrorText(); + } + for (int signum : {SIGINT, SIGTERM}) { +#else + for (int signum : {SIGINT, SIGTERM, SIGHUP}) { +#endif + if (std::signal(signum, CppSignalsHandler) == SIG_ERR) { + ythrow TSystemError() << "std::signal failed to set handler for signal with id " << signum; + } + } +} diff --git a/util/system/interrupt_signals.h b/util/system/interrupt_signals.h index e38f95908d..9f9c2427f1 100644 --- a/util/system/interrupt_signals.h +++ b/util/system/interrupt_signals.h @@ -1,22 +1,22 @@ -#pragma once - -#include "platform.h" - -#include <csignal> - -#ifdef _win_ - #ifndef SIGHUP +#pragma once + +#include "platform.h" + +#include <csignal> + +#ifdef _win_ + #ifndef SIGHUP #define SIGHUP 1 /* Hangup (POSIX). */ - #endif -#endif - + #endif +#endif + /** - * Set handler for interrupt signals. - * - * All OSes: SIGINT, SIGTERM (defined by C++ standard) - * UNIX variants: Also SIGHUP - * Windows: CTRL_C_EVENT handled as SIGINT, CTRL_BREAK_EVENT as SIGTERM, CTRL_CLOSE_EVENT as SIGHUP + * Set handler for interrupt signals. + * + * All OSes: SIGINT, SIGTERM (defined by C++ standard) + * UNIX variants: Also SIGHUP + * Windows: CTRL_C_EVENT handled as SIGINT, CTRL_BREAK_EVENT as SIGTERM, CTRL_CLOSE_EVENT as SIGHUP * * \param handler Signal handler to use. Pass nullptr to clear currently set handler. - */ -void SetInterruptSignalsHandler(void (*handler)(int signum)); + */ +void SetInterruptSignalsHandler(void (*handler)(int signum)); diff --git a/util/system/interrupt_signals_ut.cpp b/util/system/interrupt_signals_ut.cpp index 822c290e82..bec54fec61 100644 --- a/util/system/interrupt_signals_ut.cpp +++ b/util/system/interrupt_signals_ut.cpp @@ -1,46 +1,46 @@ -#include "interrupt_signals.h" - -#include "atomic.h" - -#include <util/datetime/base.h> - +#include "interrupt_signals.h" + +#include "atomic.h" + +#include <util/datetime/base.h> + #include <library/cpp/testing/unittest/registar.h> - -#ifdef _win_ - #include <windows.h> -#endif - + +#ifdef _win_ + #include <windows.h> +#endif + Y_UNIT_TEST_SUITE(TTestInterruptSignals) { - static TAtomic HandledSigNum = 0; - - static void Handler(int signum) { - AtomicSet(HandledSigNum, signum); - } - + static TAtomic HandledSigNum = 0; + + static void Handler(int signum) { + AtomicSet(HandledSigNum, signum); + } + Y_UNIT_TEST(Test1) { - SetInterruptSignalsHandler(Handler); -#ifdef _win_ - // TODO: unfortunately GenerateConsoleCtrlEvent fails under Wine - /* - for (auto [winEvent, posixSigNum] : { - std::make_pair(CTRL_C_EVENT, SIGINT), - std::make_pair(CTRL_BREAK_EVENT, SIGTERM) - }) - { - if (!GenerateConsoleCtrlEvent(winEvent, 0)) { - UNIT_FAIL("GenerateConsoleCtrlEvent failed: " << LastSystemErrorText()); - } - Sleep(TDuration::MilliSeconds(100)); - UNIT_ASSERT_VALUES_EQUAL(HandledSigNum, posixSigNum); - } - */ - for (int signum : {SIGINT, SIGTERM}) { -#else - for (int signum : {SIGINT, SIGTERM, SIGHUP}) { -#endif - std::raise(signum); - Sleep(TDuration::MilliSeconds(100)); // give it time to handle an async signal - UNIT_ASSERT_VALUES_EQUAL(HandledSigNum, signum); - } - } -} + SetInterruptSignalsHandler(Handler); +#ifdef _win_ + // TODO: unfortunately GenerateConsoleCtrlEvent fails under Wine + /* + for (auto [winEvent, posixSigNum] : { + std::make_pair(CTRL_C_EVENT, SIGINT), + std::make_pair(CTRL_BREAK_EVENT, SIGTERM) + }) + { + if (!GenerateConsoleCtrlEvent(winEvent, 0)) { + UNIT_FAIL("GenerateConsoleCtrlEvent failed: " << LastSystemErrorText()); + } + Sleep(TDuration::MilliSeconds(100)); + UNIT_ASSERT_VALUES_EQUAL(HandledSigNum, posixSigNum); + } + */ + for (int signum : {SIGINT, SIGTERM}) { +#else + for (int signum : {SIGINT, SIGTERM, SIGHUP}) { +#endif + std::raise(signum); + Sleep(TDuration::MilliSeconds(100)); // give it time to handle an async signal + UNIT_ASSERT_VALUES_EQUAL(HandledSigNum, signum); + } + } +} diff --git a/util/system/mktemp_system.cpp b/util/system/mktemp_system.cpp index 03a16254a4..32bea2987c 100644 --- a/util/system/mktemp_system.cpp +++ b/util/system/mktemp_system.cpp @@ -41,13 +41,13 @@ #include <string.h> #include <ctype.h> -#ifdef _win32_ +#ifdef _win32_ #include "winint.h" #include <util/folder/dirut.h> #else - #include <unistd.h> -#endif - + #include <unistd.h> +#endif + #include <util/random/random.h> #include "sysstat.h" @@ -59,10 +59,10 @@ GetTemp(char* path, int* doopen, int domkdir, int slen) { char *start, *trv, *suffp; char* pad; -#ifndef _win32_ +#ifndef _win32_ struct stat sbuf; int rval; -#endif +#endif ui32 rand; if (doopen != nullptr && domkdir) { @@ -95,7 +95,7 @@ GetTemp(char* path, int* doopen, int domkdir, int slen) for (; trv > path; --trv) { if (*trv == '/') { *trv = '\0'; -#ifdef _win32_ +#ifdef _win32_ ui32 attr = ::GetFileAttributesA(path); *trv = '/'; if (attr == 0xFFFFFFFF) @@ -104,7 +104,7 @@ GetTemp(char* path, int* doopen, int domkdir, int slen) errno = ENOTDIR; return (0); } -#else +#else rval = stat(path, &sbuf); *trv = '/'; if (rval != 0) { @@ -114,7 +114,7 @@ GetTemp(char* path, int* doopen, int domkdir, int slen) errno = ENOTDIR; return (0); } -#endif +#endif break; } } @@ -137,14 +137,14 @@ GetTemp(char* path, int* doopen, int domkdir, int slen) return (0); } } else -#ifdef _win32_ +#ifdef _win32_ if (::GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES) return (errno == ENOENT); -#else +#else if (lstat(path, &sbuf)) { return (errno == ENOENT); } -#endif +#endif /* If we have a collision, cycle through the space of filenames */ for (trv = start;;) { if (*trv == '\0' || trv == suffp) { diff --git a/util/system/platform.h b/util/system/platform.h index 5c4abea622..58f310ab34 100644 --- a/util/system/platform.h +++ b/util/system/platform.h @@ -138,10 +138,10 @@ #define _sse2_ #endif -#if defined(__SSE3__) || defined(SSE3_ENABLED) +#if defined(__SSE3__) || defined(SSE3_ENABLED) #define _sse3_ -#endif - +#endif + #if defined(__SSSE3__) || defined(SSSE3_ENABLED) #define _ssse3_ #endif diff --git a/util/system/progname.cpp b/util/system/progname.cpp index 4e3a173a64..2c29119320 100644 --- a/util/system/progname.cpp +++ b/util/system/progname.cpp @@ -1,4 +1,4 @@ -#include "execpath.h" +#include "execpath.h" #include "progname.h" #include <util/folder/dirut.h> diff --git a/util/system/rusage.cpp b/util/system/rusage.cpp index 800a685cb9..2befeca875 100644 --- a/util/system/rusage.cpp +++ b/util/system/rusage.cpp @@ -109,7 +109,7 @@ void TRusage::Fill() { } #if defined(_darwin_) - // see https://lists.apple.com/archives/darwin-kernel/2009/Mar/msg00005.html + // see https://lists.apple.com/archives/darwin-kernel/2009/Mar/msg00005.html MaxRss = ru.ru_maxrss; #else MaxRss = ru.ru_maxrss * 1024LL; diff --git a/util/system/shellcommand_ut.cpp b/util/system/shellcommand_ut.cpp index 458ecd0025..9d849279d2 100644 --- a/util/system/shellcommand_ut.cpp +++ b/util/system/shellcommand_ut.cpp @@ -298,7 +298,7 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { // this ut is unix-only, port to win using %TEMP% Y_UNIT_TEST(TestInterrupt) { TString tmpfile = TString("shellcommand_ut.interrupt.") + ToString(RandomNumber<ui32>()); - + TShellCommandOptions options; options.SetAsync(true); options.SetQuoteArguments(false); diff --git a/util/system/user.cpp b/util/system/user.cpp index 8c66a8ce95..83e89ea0a8 100644 --- a/util/system/user.cpp +++ b/util/system/user.cpp @@ -1,18 +1,18 @@ -#include "user.h" -#include "platform.h" -#include "defaults.h" +#include "user.h" +#include "platform.h" +#include "defaults.h" #include "env.h" - -#include <util/generic/yexception.h> - -#ifdef _win_ + +#include <util/generic/yexception.h> + +#ifdef _win_ #include "winint.h" -#else +#else #include <errno.h> #include <pwd.h> #include <unistd.h> -#endif - +#endif + TString GetUsername() { for (const auto& var : {"LOGNAME", "USER", "LNAME", "USERNAME"}) { TString val = GetEnv(var); @@ -21,19 +21,19 @@ TString GetUsername() { } } - TTempBuf nameBuf; - for (;;) { -#if defined(_win_) - DWORD len = (DWORD)Min(nameBuf.Size(), size_t(32767)); + TTempBuf nameBuf; + for (;;) { +#if defined(_win_) + DWORD len = (DWORD)Min(nameBuf.Size(), size_t(32767)); if (!GetUserNameA(nameBuf.Data(), &len)) { - DWORD err = GetLastError(); - if ((err == ERROR_INSUFFICIENT_BUFFER) && (nameBuf.Size() <= 32767)) - nameBuf = TTempBuf((size_t)len); - else - ythrow TSystemError(err) << " GetUserName failed"; - } else { + DWORD err = GetLastError(); + if ((err == ERROR_INSUFFICIENT_BUFFER) && (nameBuf.Size() <= 32767)) + nameBuf = TTempBuf((size_t)len); + else + ythrow TSystemError(err) << " GetUserName failed"; + } else { return TString(nameBuf.Data(), (size_t)(len - 1)); - } + } #elif defined(_bionic_) const passwd* pwd = getpwuid(geteuid()); @@ -43,16 +43,16 @@ TString GetUsername() { ythrow TSystemError() << TStringBuf(" getpwuid failed"); #else - passwd pwd; - passwd* tmpPwd; - int err = getpwuid_r(geteuid(), &pwd, nameBuf.Data(), nameBuf.Size(), &tmpPwd); + passwd pwd; + passwd* tmpPwd; + int err = getpwuid_r(geteuid(), &pwd, nameBuf.Data(), nameBuf.Size(), &tmpPwd); if (err == 0 && tmpPwd) { return TString(pwd.pw_name); } else if (err == ERANGE) { nameBuf = TTempBuf(nameBuf.Size() * 2); - } else { + } else { ythrow TSystemError(err) << " getpwuid_r failed"; - } -#endif - } -} + } +#endif + } +} diff --git a/util/system/user.h b/util/system/user.h index e426a79a9e..be348d1cee 100644 --- a/util/system/user.h +++ b/util/system/user.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include <util/generic/fwd.h> - + TString GetUsername(); diff --git a/util/system/ut/ya.make b/util/system/ut/ya.make index 76e8db9225..127e7c261e 100644 --- a/util/system/ut/ya.make +++ b/util/system/ut/ya.make @@ -51,7 +51,7 @@ SRCS( system/hi_lo_ut.cpp system/hostname_ut.cpp system/info_ut.cpp - system/interrupt_signals_ut.cpp + system/interrupt_signals_ut.cpp system/mem_info_ut.cpp system/mincore_ut.cpp system/mutex_ut.cpp diff --git a/util/ya.make b/util/ya.make index 9d22bff2d9..6ebe7e40cf 100644 --- a/util/ya.make +++ b/util/ya.make @@ -288,7 +288,7 @@ JOIN_SRCS( system/cpu_id.cpp system/fhandle.cpp system/guard.cpp - system/interrupt_signals.cpp + system/interrupt_signals.cpp system/madvise.cpp system/maxlen.cpp system/mem_info.cpp |