diff options
author | nkmakarov <nkmakarov@yandex-team.ru> | 2022-02-10 16:49:06 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:06 +0300 |
commit | 324348a37ed08cf66897faefb0ec4bebfe7804e1 (patch) | |
tree | 8736a3afd6953763bf57544746bf1b8b5404dec6 /util/string | |
parent | 5eddcf9f19515e4be1e49ba1482d920e007a07d1 (diff) | |
download | ydb-324348a37ed08cf66897faefb0ec4bebfe7804e1.tar.gz |
Restoring authorship annotation for <nkmakarov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/string')
-rw-r--r-- | util/string/cast.h | 66 | ||||
-rw-r--r-- | util/string/cast_ut.cpp | 58 |
2 files changed, 62 insertions, 62 deletions
diff --git a/util/string/cast.h b/util/string/cast.h index 90e925c194..de44a87370 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -236,15 +236,15 @@ inline bool TryFromString(const TChar* data, T& result) { return TryFromString<T>(data, std::char_traits<TChar>::length(data), result); } -template <class T, class TChar> -inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) { - if (TryFromString<T>(data, len, result)) { - return true; - } - result = def; - return false; -} - +template <class T, class TChar> +inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) { + if (TryFromString<T>(data, len, result)) { + return true; + } + result = def; + return false; +} + template <class T> inline bool TryFromString(const TStringBuf& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); @@ -275,39 +275,39 @@ inline bool TryFromStringWithDefault(const TStringType& s, T& result, const T& d return TryFromString<T>(s.data(), s.size(), result, def); } -template <class T> -inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { - return TryFromStringWithDefault<T>(TStringBuf(s), result, def); -} - +template <class T> +inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { + return TryFromStringWithDefault<T>(TStringBuf(s), result, def); +} + template <class T, class TStringType> inline bool TryFromStringWithDefault(const TStringType& s, T& result) { - return TryFromStringWithDefault<T>(s, result, T()); -} - -// FromString methods with default value if data is invalid -template <class T, class TChar> -inline T FromString(const TChar* data, const size_t len, const T& def) { - T result; - TryFromString<T>(data, len, result, def); - return result; -} - + return TryFromStringWithDefault<T>(s, result, T()); +} + +// FromString methods with default value if data is invalid +template <class T, class TChar> +inline T FromString(const TChar* data, const size_t len, const T& def) { + T result; + TryFromString<T>(data, len, result, def); + return result; +} + template <class T, class TStringType> inline T FromStringWithDefault(const TStringType& s, const T& def) { return FromString<T>(s.data(), s.size(), def); } -template <class T> -inline T FromStringWithDefault(const char* s, const T& def) { - return FromStringWithDefault<T>(TStringBuf(s), def); -} - +template <class T> +inline T FromStringWithDefault(const char* s, const T& def) { + return FromStringWithDefault<T>(TStringBuf(s), def); +} + template <class T, class TStringType> inline T FromStringWithDefault(const TStringType& s) { - return FromStringWithDefault<T>(s, T()); -} - + return FromStringWithDefault<T>(s, T()); +} + double StrToD(const char* b, char** se); double StrToD(const char* b, const char* e, char** se); diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index 033450c38c..edb6df2771 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -2,7 +2,7 @@ #include <library/cpp/testing/unittest/registar.h> -#include <util/charset/wide.h> +#include <util/charset/wide.h> #include <util/system/defaults.h> #include <limits> @@ -372,71 +372,71 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestDefault) { - size_t res = 0; - const size_t def1 = 42; - + size_t res = 0; + const size_t def1 = 42; + TString s1("100500"); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res, def1), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100500", res, def1), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s1, def1), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s1, def1), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s1, def1), 100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s1), 100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault("100500", def1), 100500); - + TString s2("100q500"); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res, def1), false); UNIT_ASSERT_VALUES_EQUAL(res, def1); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100q500", res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100 500", res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s2, def1), yexception); - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault("100q500", def1), yexception); + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s2, def1), yexception); + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault("100q500", def1), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s2, def1), def1); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s2), size_t()); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>("100q500"), size_t()); - UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s2), TFromStringException); - - int res2 = 0; - const int def2 = -6; - + UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s2), TFromStringException); + + int res2 = 0; + const int def2 = -6; + TUtf16String s3 = u"-100500"; UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2, def2), true); UNIT_ASSERT_VALUES_EQUAL(res2, -100500); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2), true); UNIT_ASSERT_VALUES_EQUAL(res2, -100500); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s3, def1), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s3, def1), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s3, def2), -100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s3), size_t()); - + TUtf16String s4 = u"-f100500"; UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2, def2), false); UNIT_ASSERT_VALUES_EQUAL(res2, def2); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2), false); UNIT_ASSERT_VALUES_EQUAL(res2, size_t()); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s4, def2), def2); - UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); + UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s4), size_t()); - } - + } + Y_UNIT_TEST(TestBool) { // True cases UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("yes"), true); |