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/draft | |
parent | 00afc96e9c0298054b7386fa7fb9e3cc3d67b974 (diff) | |
download | ydb-298c6da79f1d8f35089a67f463f0b541bec36d9b.tar.gz |
Restoring authorship annotation for <akhropov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/draft')
-rw-r--r-- | util/draft/date.cpp | 80 | ||||
-rw-r--r-- | util/draft/date.h | 36 | ||||
-rw-r--r-- | util/draft/date_ut.cpp | 32 | ||||
-rw-r--r-- | util/draft/datetime.cpp | 36 | ||||
-rw-r--r-- | util/draft/datetime.h | 64 | ||||
-rw-r--r-- | util/draft/enum.h | 74 | ||||
-rw-r--r-- | util/draft/holder_vector.h | 40 | ||||
-rw-r--r-- | util/draft/ip.h | 168 | ||||
-rw-r--r-- | util/draft/memory.h | 48 | ||||
-rw-r--r-- | util/draft/memory_ut.cpp | 46 |
10 files changed, 312 insertions, 312 deletions
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)); } -} +} |