diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/string_utils/parse_size | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/string_utils/parse_size')
-rw-r--r-- | library/cpp/string_utils/parse_size/parse_size.cpp | 120 | ||||
-rw-r--r-- | library/cpp/string_utils/parse_size/parse_size.h | 52 |
2 files changed, 86 insertions, 86 deletions
diff --git a/library/cpp/string_utils/parse_size/parse_size.cpp b/library/cpp/string_utils/parse_size/parse_size.cpp index 39188d560b..1195f89a51 100644 --- a/library/cpp/string_utils/parse_size/parse_size.cpp +++ b/library/cpp/string_utils/parse_size/parse_size.cpp @@ -2,89 +2,89 @@ #include <util/generic/yexception.h> #include <util/generic/ylimits.h> -#include <util/string/cast.h> +#include <util/string/cast.h> #include <util/stream/output.h> namespace { - enum ESuffixShifts { - ESS_KILO_BYTES = 10, - ESS_MEGA_BYTES = 20, - ESS_GIGA_BYTES = 30, - ESS_TERA_BYTES = 40, - }; - - bool TryShiftValue(ui64& value, ui64 shift) { - if (value > (Max<ui64>() >> shift)) { - return false; - } - - value <<= shift; - return true; + enum ESuffixShifts { + ESS_KILO_BYTES = 10, + ESS_MEGA_BYTES = 20, + ESS_GIGA_BYTES = 30, + ESS_TERA_BYTES = 40, + }; + + bool TryShiftValue(ui64& value, ui64 shift) { + if (value > (Max<ui64>() >> shift)) { + return false; + } + + value <<= shift; + return true; } - ui64 ShiftValue(ui64 value, ui64 shift) { - if (!TryShiftValue(value, shift)) { - ythrow yexception() << "value overflow '" << value << " << " << shift << "'"; - } else { - return value; - } + ui64 ShiftValue(ui64 value, ui64 shift) { + if (!TryShiftValue(value, shift)) { + ythrow yexception() << "value overflow '" << value << " << " << shift << "'"; + } else { + return value; + } } } namespace NSize { - ui64 ParseSize(TStringBuf str) { + ui64 ParseSize(TStringBuf str) { if (! str.size()) - ythrow yexception() << "Wrong size " << str; + ythrow yexception() << "Wrong size " << str; char suff = tolower(str[str.size() - 1]); - if (isdigit(suff)) - return FromString<ui64>(str); - ui64 shift = 1; - switch (suff) { - case 'k': - shift = ESS_KILO_BYTES; - break; - case 'm': - shift = ESS_MEGA_BYTES; - break; - case 'g': - shift = ESS_GIGA_BYTES; - break; - case 't': - shift = ESS_TERA_BYTES; - break; - default: - ythrow yexception() << "Unknown suffix " << str; - } + if (isdigit(suff)) + return FromString<ui64>(str); + ui64 shift = 1; + switch (suff) { + case 'k': + shift = ESS_KILO_BYTES; + break; + case 'm': + shift = ESS_MEGA_BYTES; + break; + case 'g': + shift = ESS_GIGA_BYTES; + break; + case 't': + shift = ESS_TERA_BYTES; + break; + default: + ythrow yexception() << "Unknown suffix " << str; + } ui64 value = FromString<ui64>(str.substr(0, str.size() - 1)); - - if (!TryShiftValue(value, shift)) { - ythrow yexception() << "Value overflow " << str; - } else { - return value; - } + + if (!TryShiftValue(value, shift)) { + ythrow yexception() << "Value overflow " << str; + } else { + return value; + } } - TSize FromKiloBytes(ui64 value) { - return TSize(ShiftValue(value, ESS_KILO_BYTES)); - } + TSize FromKiloBytes(ui64 value) { + return TSize(ShiftValue(value, ESS_KILO_BYTES)); + } - TSize FromMegaBytes(ui64 value) { - return TSize(ShiftValue(value, ESS_MEGA_BYTES)); + TSize FromMegaBytes(ui64 value) { + return TSize(ShiftValue(value, ESS_MEGA_BYTES)); } - TSize FromGigaBytes(ui64 value) { - return TSize(ShiftValue(value, ESS_GIGA_BYTES)); - } + TSize FromGigaBytes(ui64 value) { + return TSize(ShiftValue(value, ESS_GIGA_BYTES)); + } - TSize FromTeraBytes(ui64 value) { - return TSize(ShiftValue(value, ESS_TERA_BYTES)); - } + TSize FromTeraBytes(ui64 value) { + return TSize(ShiftValue(value, ESS_TERA_BYTES)); + } } -template <> +template <> NSize::TSize FromStringImpl<NSize::TSize>(const char* data, size_t len) { return NSize::TSize(NSize::ParseSize(TStringBuf(data, len))); } diff --git a/library/cpp/string_utils/parse_size/parse_size.h b/library/cpp/string_utils/parse_size/parse_size.h index ad235ef02f..c9fa92980b 100644 --- a/library/cpp/string_utils/parse_size/parse_size.h +++ b/library/cpp/string_utils/parse_size/parse_size.h @@ -3,31 +3,31 @@ #include <util/generic/strbuf.h> namespace NSize { - ui64 ParseSize(TStringBuf size); - - // Convenient disk size representation with string parsing and integer comparison - class TSize { - public: - TSize(ui64 value = 0) - : Value(value) - { - } - - ui64 GetValue() const { - return Value; - } - - operator ui64() const { - return Value; - } - - private: - ui64 Value; - }; - - TSize FromKiloBytes(ui64 value); - TSize FromMegaBytes(ui64 value); - TSize FromGigaBytes(ui64 value); - TSize FromTeraBytes(ui64 value); + ui64 ParseSize(TStringBuf size); + + // Convenient disk size representation with string parsing and integer comparison + class TSize { + public: + TSize(ui64 value = 0) + : Value(value) + { + } + + ui64 GetValue() const { + return Value; + } + + operator ui64() const { + return Value; + } + + private: + ui64 Value; + }; + + TSize FromKiloBytes(ui64 value); + TSize FromMegaBytes(ui64 value); + TSize FromGigaBytes(ui64 value); + TSize FromTeraBytes(ui64 value); } |