diff options
author | mvel <mvel@yandex-team.ru> | 2022-02-10 16:45:41 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:41 +0300 |
commit | 43f5a35593ebc9f6bcea619bb170394ea7ae468e (patch) | |
tree | e98df59de24d2ef7c77baed9f41e4875a2fef972 /util/string | |
parent | bd30392c4cc92487950adc375c07adf52da1d592 (diff) | |
download | ydb-43f5a35593ebc9f6bcea619bb170394ea7ae468e.tar.gz |
Restoring authorship annotation for <mvel@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/string')
-rw-r--r-- | util/string/ascii.cpp | 24 | ||||
-rw-r--r-- | util/string/ascii.h | 90 | ||||
-rw-r--r-- | util/string/ascii_ut.cpp | 24 | ||||
-rw-r--r-- | util/string/cast.cpp | 132 | ||||
-rw-r--r-- | util/string/cast.h | 70 | ||||
-rw-r--r-- | util/string/cast_ut.cpp | 216 | ||||
-rw-r--r-- | util/string/hex.h | 2 | ||||
-rw-r--r-- | util/string/strip.cpp | 4 | ||||
-rw-r--r-- | util/string/strip.h | 26 | ||||
-rw-r--r-- | util/string/strip_ut.cpp | 92 | ||||
-rw-r--r-- | util/string/subst.h | 4 | ||||
-rw-r--r-- | util/string/subst_ut.cpp | 12 | ||||
-rw-r--r-- | util/string/type_ut.cpp | 32 |
13 files changed, 364 insertions, 364 deletions
diff --git a/util/string/ascii.cpp b/util/string/ascii.cpp index 9f04f7ec4f..95edb95cc8 100644 --- a/util/string/ascii.cpp +++ b/util/string/ascii.cpp @@ -1,8 +1,8 @@ #include "ascii.h" -#include <util/system/yassert.h> -#include <util/system/compat.h> - +#include <util/system/yassert.h> +#include <util/system/compat.h> + // clang-format off extern const unsigned char NPrivate::ASCII_CLASS[256] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, @@ -22,7 +22,7 @@ extern const unsigned char NPrivate::ASCII_CLASS[256] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; - + extern const unsigned char NPrivate::ASCII_LOWER[256] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, @@ -46,14 +46,14 @@ extern const unsigned char NPrivate::ASCII_LOWER[256] = { int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { if (s1.size() <= s2.size()) { if (int cmp = strnicmp(s1.data(), s2.data(), s1.size())) { - return cmp; - } + return cmp; + } return (s1.size() < s2.size()) ? -1 : 0; - } - + } + Y_ASSERT(s1.size() > s2.size()); if (int cmp = strnicmp(s1.data(), s2.data(), s2.size())) { - return cmp; - } - return 1; -} + return cmp; + } + return 1; +} diff --git a/util/string/ascii.h b/util/string/ascii.h index 0bd70bc9ed..10344384d3 100644 --- a/util/string/ascii.h +++ b/util/string/ascii.h @@ -1,7 +1,7 @@ #pragma once #include <util/system/defaults.h> -#include <util/system/compat.h> +#include <util/system/compat.h> #include <util/generic/string.h> // ctype.h-like functions, locale-independent: @@ -156,72 +156,72 @@ template <class T> inline ::NPrivate::TDereferenced<T> AsciiToUpper(T c) noexcept { return IsAsciiLower(c) ? (c + ('A' - 'a')) : c; } - -/** - * ASCII case-insensitive string comparison (for proper UTF8 strings + +/** + * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). * - * BUGS: Currently will NOT work properly with strings that contain - * 0-terminator character inside. See IGNIETFERRO-1641 for details. - * + * BUGS: Currently will NOT work properly with strings that contain + * 0-terminator character inside. See IGNIETFERRO-1641 for details. + * * @return true iff @c s1 ans @c s2 are case-insensitively equal. */ -static inline bool AsciiEqualsIgnoreCase(const char* s1, const char* s2) noexcept { - return stricmp(s1, s2) == 0; -} - -/** - * ASCII case-insensitive string comparison (for proper UTF8 strings +static inline bool AsciiEqualsIgnoreCase(const char* s1, const char* s2) noexcept { + return stricmp(s1, s2) == 0; +} + +/** + * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). * - * BUGS: Currently will NOT work properly with strings that contain - * 0-terminator character inside. See IGNIETFERRO-1641 for details. - * + * BUGS: Currently will NOT work properly with strings that contain + * 0-terminator character inside. See IGNIETFERRO-1641 for details. + * * @return true iff @c s1 ans @c s2 are case-insensitively equal. */ static inline bool AsciiEqualsIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { return (s1.size() == s2.size()) && strnicmp(s1.data(), s2.data(), s1.size()) == 0; -} - -/** - * ASCII case-insensitive string comparison (for proper UTF8 strings +} + +/** + * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). * - * BUGS: Currently will NOT work properly with strings that contain - * 0-terminator character inside. See IGNIETFERRO-1641 for details. - * + * BUGS: Currently will NOT work properly with strings that contain + * 0-terminator character inside. See IGNIETFERRO-1641 for details. + * * @return 0 if strings are equal, negative if @c s1 < @c s2 * and positive otherwise. * (same value as @c stricmp does). */ -static inline int AsciiCompareIgnoreCase(const char* s1, const char* s2) noexcept { - return stricmp(s1, s2); -} - -/** - * ASCII case-insensitive string comparison (for proper UTF8 strings +static inline int AsciiCompareIgnoreCase(const char* s1, const char* s2) noexcept { + return stricmp(s1, s2); +} + +/** + * ASCII case-insensitive string comparison (for proper UTF8 strings * case-insensitive comparison consider using @c library/cpp/charset). * - * BUGS: Currently will NOT work properly with strings that contain - * 0-terminator character inside. See IGNIETFERRO-1641 for details. - * - * @return - * - zero if strings are equal - * - negative if @c s1 < @c s2 - * - positive otherwise, - * similar to stricmp. + * BUGS: Currently will NOT work properly with strings that contain + * 0-terminator character inside. See IGNIETFERRO-1641 for details. + * + * @return + * - zero if strings are equal + * - negative if @c s1 < @c s2 + * - positive otherwise, + * similar to stricmp. */ Y_PURE_FUNCTION int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept; /** - * ASCII case-sensitive string comparison (for proper UTF8 strings - * case-sensitive comparison consider using @c library/cpp/charset). - * - * BUGS: Currently will NOT work properly with strings that contain - * 0-terminator character inside. See IGNIETFERRO-1641 for details. - * - * @return true iff @c s2 are case-sensitively prefix of @c s1. - */ + * ASCII case-sensitive string comparison (for proper UTF8 strings + * case-sensitive comparison consider using @c library/cpp/charset). + * + * BUGS: Currently will NOT work properly with strings that contain + * 0-terminator character inside. See IGNIETFERRO-1641 for details. + * + * @return true iff @c s2 are case-sensitively prefix of @c s1. + */ static inline bool AsciiHasPrefix(const TStringBuf s1, const TStringBuf s2) noexcept { return (s1.size() >= s2.size()) && memcmp(s1.data(), s2.data(), s2.size()) == 0; } diff --git a/util/string/ascii_ut.cpp b/util/string/ascii_ut.cpp index 41732f6f4e..89069fee50 100644 --- a/util/string/ascii_ut.cpp +++ b/util/string/ascii_ut.cpp @@ -61,14 +61,14 @@ Y_UNIT_TEST_SUITE(TAsciiTest) { UNIT_ASSERT(!IsAsciiPunct(i)); } } - + Y_UNIT_TEST(CompareTest) { - UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", "qQq")); + UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", "qQq")); UNIT_ASSERT(AsciiEqualsIgnoreCase("qqq", TStringBuf("qQq"))); TString qq = "qq"; TString qQ = "qQ"; - UNIT_ASSERT(AsciiEqualsIgnoreCase(qq, qQ)); - + UNIT_ASSERT(AsciiEqualsIgnoreCase(qq, qQ)); + TString x = "qqqA"; TString y = "qQqB"; TString z = "qQnB"; @@ -77,13 +77,13 @@ Y_UNIT_TEST_SUITE(TAsciiTest) { TStringBuf xs = TStringBuf(x.data(), 3); TStringBuf ys = TStringBuf(y.data(), 3); TStringBuf zs = TStringBuf(z.data(), 3); - UNIT_ASSERT(AsciiCompareIgnoreCase(xs, ys) == 0); - UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zs) > 0); - UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zz) < 0); - UNIT_ASSERT(AsciiCompareIgnoreCase(zzz, zz) > 0); - - UNIT_ASSERT(AsciiCompareIgnoreCase("qqQ", "qq") > 0); - UNIT_ASSERT(AsciiCompareIgnoreCase("qq", "qq") == 0); + UNIT_ASSERT(AsciiCompareIgnoreCase(xs, ys) == 0); + UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zs) > 0); + UNIT_ASSERT(AsciiCompareIgnoreCase(xs, zz) < 0); + UNIT_ASSERT(AsciiCompareIgnoreCase(zzz, zz) > 0); + + UNIT_ASSERT(AsciiCompareIgnoreCase("qqQ", "qq") > 0); + UNIT_ASSERT(AsciiCompareIgnoreCase("qq", "qq") == 0); UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "qwe"), true); UNIT_ASSERT_EQUAL(AsciiHasPrefix("qweasd", "qWe"), false); @@ -94,5 +94,5 @@ Y_UNIT_TEST_SUITE(TAsciiTest) { UNIT_ASSERT_EQUAL(AsciiHasSuffixIgnoreCase("qweasd", "asD"), true); UNIT_ASSERT_EQUAL(AsciiHasSuffixIgnoreCase("qweasd", "ast"), false); - } + } } diff --git a/util/string/cast.cpp b/util/string/cast.cpp index 7443dbafb0..aa1e65a8e9 100644 --- a/util/string/cast.cpp +++ b/util/string/cast.cpp @@ -308,7 +308,7 @@ namespace { TUnsigned result; EParseStatus error = TBasicIntParser<TUnsigned, base, TChar>::Parse(&pos, end, max, &result); if (error != PS_OK) { - *ppos = pos; + *ppos = pos; return error; } @@ -484,38 +484,38 @@ size_t ToStringImpl<bool>(bool t, char* buf, size_t len) { */ template <> -bool TryFromStringImpl<bool>(const char* data, size_t len, bool& result) { +bool TryFromStringImpl<bool>(const char* data, size_t len, bool& result) { if (len == 1) { if (data[0] == '0') { - result = false; - return true; + result = false; + return true; } else if (data[0] == '1') { - result = true; + result = true; return true; } } TStringBuf buf(data, len); - if (IsTrue(buf)) { - result = true; + if (IsTrue(buf)) { + result = true; + return true; + } else if (IsFalse(buf)) { + result = false; return true; - } else if (IsFalse(buf)) { - result = false; - return true; } - return false; + return false; } -template <> -bool FromStringImpl<bool>(const char* data, size_t len) { - bool result; +template <> +bool FromStringImpl<bool>(const char* data, size_t len) { + bool result; if (!TryFromStringImpl<bool>(data, len, result)) { ythrow TFromStringException() << TStringBuf("Cannot parse bool(") << TStringBuf(data, len) << TStringBuf("). "); } - return result; -} - + return result; +} + template <> TString FromStringImpl<TString>(const char* data, size_t len) { return TString(data, len); @@ -541,8 +541,8 @@ TWtringBuf FromStringImpl<TWtringBuf>(const wchar16* data, size_t len) { return TWtringBuf(data, len); } -// Try-versions -template <> +// Try-versions +template <> bool TryFromStringImpl<TStringBuf>(const char* data, size_t len, TStringBuf& result) { result = {data, len}; return true; @@ -551,10 +551,10 @@ bool TryFromStringImpl<TStringBuf>(const char* data, size_t len, TStringBuf& res template <> bool TryFromStringImpl<TString>(const char* data, size_t len, TString& result) { result = TString(data, len); - return true; -} - -template <> + return true; +} + +template <> bool TryFromStringImpl<std::string>(const char* data, size_t len, std::string& result) { result.assign(data, len); return true; @@ -569,9 +569,9 @@ bool TryFromStringImpl<TWtringBuf>(const wchar16* data, size_t len, TWtringBuf& template <> bool TryFromStringImpl<TUtf16String>(const wchar16* data, size_t len, TUtf16String& result) { result = TUtf16String(data, len); - return true; -} - + return true; +} + #define DEF_INT_SPEC_III(CHAR, TYPE, ITYPE, BOUNDS, BASE) \ template <> \ TYPE IntFromString<TYPE, BASE>(const CHAR* data, size_t len) { \ @@ -641,48 +641,48 @@ DEF_FLT_SPEC(long double) #undef DEF_FLT_SPEC // Using StrToD for float and double because it is faster than sscanf. -// Exception-free, specialized for float types +// Exception-free, specialized for float types template <> -bool TryFromStringImpl<double>(const char* data, size_t len, double& result) { - if (!len) { - return false; - } - +bool TryFromStringImpl<double>(const char* data, size_t len, double& result) { + if (!len) { + return false; + } + char* se = nullptr; - double d = StrToD(data, data + len, &se); - - if (se != data + len) { - return false; - } - result = d; - return true; -} - -template <> -bool TryFromStringImpl<float>(const char* data, size_t len, float& result) { - double d; - if (TryFromStringImpl<double>(data, len, d)) { - result = static_cast<float>(d); - return true; - } - return false; -} - -template <> -bool TryFromStringImpl<long double>(const char* data, size_t len, long double& result) { - double d; - if (TryFromStringImpl<double>(data, len, d)) { - result = static_cast<long double>(d); - return true; - } - return false; -} - -// Exception-throwing, specialized for float types -template <> + double d = StrToD(data, data + len, &se); + + if (se != data + len) { + return false; + } + result = d; + return true; +} + +template <> +bool TryFromStringImpl<float>(const char* data, size_t len, float& result) { + double d; + if (TryFromStringImpl<double>(data, len, d)) { + result = static_cast<float>(d); + return true; + } + return false; +} + +template <> +bool TryFromStringImpl<long double>(const char* data, size_t len, long double& result) { + double d; + if (TryFromStringImpl<double>(data, len, d)) { + result = static_cast<long double>(d); + return true; + } + return false; +} + +// Exception-throwing, specialized for float types +template <> double FromStringImpl<double>(const char* data, size_t len) { - double d = 0.0; - if (!TryFromStringImpl(data, len, d)) { + double d = 0.0; + if (!TryFromStringImpl(data, len, d)) { ythrow TFromStringException() << TStringBuf("cannot parse float(") << TStringBuf(data, len) << TStringBuf(")"); } return d; @@ -690,7 +690,7 @@ double FromStringImpl<double>(const char* data, size_t len) { template <> float FromStringImpl<float>(const char* data, size_t len) { - return static_cast<float>(FromStringImpl<double>(data, len)); + return static_cast<float>(FromStringImpl<double>(data, len)); } double StrToD(const char* b, const char* e, char** se) { diff --git a/util/string/cast.h b/util/string/cast.h index 860af45dd6..90e925c194 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -14,7 +14,7 @@ template <class T> size_t ToStringImpl(T t, char* buf, size_t len); -/** +/** * Converts @c t to string writing not more than @c len bytes to output buffer @c buf. * No NULL terminator appended! Throws exception on buffer overflow. * @return number of bytes written @@ -215,27 +215,27 @@ inline ::NPrivate::TFromString<typename T::TChar> FromString(const T& s) { return ::NPrivate::TFromString<typename T::TChar>(s.data(), s.size()); } -// Conversion exception free versions -template <typename T, typename TChar> -bool TryFromStringImpl(const TChar* data, size_t len, T& result); - -/** - * @param data Source string buffer pointer - * @param len Source string length, in characters - * @param result Place to store conversion result value. - * If conversion error occurs, no value stored in @c result - * @return @c true in case of successful conversion, @c false otherwise - **/ -template <typename T, typename TChar> -inline bool TryFromString(const TChar* data, size_t len, T& result) { - return TryFromStringImpl<T>(data, len, result); -} - -template <typename T, typename TChar> -inline bool TryFromString(const TChar* data, T& result) { +// Conversion exception free versions +template <typename T, typename TChar> +bool TryFromStringImpl(const TChar* data, size_t len, T& result); + +/** + * @param data Source string buffer pointer + * @param len Source string length, in characters + * @param result Place to store conversion result value. + * If conversion error occurs, no value stored in @c result + * @return @c true in case of successful conversion, @c false otherwise + **/ +template <typename T, typename TChar> +inline bool TryFromString(const TChar* data, size_t len, T& result) { + return TryFromStringImpl<T>(data, len, result); +} + +template <typename T, typename TChar> +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)) { @@ -245,31 +245,31 @@ inline bool TryFromString(const TChar* data, const size_t len, T& result, const return false; } -template <class T> -inline bool TryFromString(const TStringBuf& s, T& result) { +template <class T> +inline bool TryFromString(const TStringBuf& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); -} - -template <class T> +} + +template <class T> inline bool TryFromString(const TString& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); -} - -template <class T> +} + +template <class T> inline bool TryFromString(const std::string& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); } template <class T> -inline bool TryFromString(const TWtringBuf& s, T& result) { +inline bool TryFromString(const TWtringBuf& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); -} - -template <class T> +} + +template <class T> inline bool TryFromString(const TUtf16String& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); -} - +} + template <class T, class TStringType> inline bool TryFromStringWithDefault(const TStringType& s, T& result, const T& def) { return TryFromString<T>(s.data(), s.size(), result, def); diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index 56daa2dfec..033450c38c 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -3,16 +3,16 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/charset/wide.h> -#include <util/system/defaults.h> +#include <util/system/defaults.h> #include <limits> -// positive test (return true or no exception) +// positive test (return true or no exception) #define test1(t, v) \ F<t>().CheckTryOK(v); \ F<t>().CheckOK(v) -// negative test (return false or exception) +// negative test (return false or exception) #define test2(t, v) \ F<t>().CheckTryFail(v); \ F<t>().CheckExc(v) @@ -22,7 +22,7 @@ #define HEX_MACROS_MAP(mac, type, val) mac(type, val, 2) mac(type, val, 8) mac(type, val, 10) mac(type, val, 16) #define OK_HEX_CHECK(type, val, base) UNIT_ASSERT_EQUAL((IntFromStringForCheck<base>(IntToString<base>(val))), val); -#define EXC_HEX_CHECK(type, val, base) UNIT_ASSERT_EXCEPTION((IntFromString<type, base>(IntToString<base>(val))), yexception); +#define EXC_HEX_CHECK(type, val, base) UNIT_ASSERT_EXCEPTION((IntFromString<type, base>(IntToString<base>(val))), yexception); #define TRY_HEX_MACROS_MAP(mac, type, val, result, def) \ mac(type, val, result, def, 2) \ @@ -53,33 +53,33 @@ struct TRet { } template <class B> - inline void CheckOK(B v) { + 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); + HEX_MACROS_MAP(OK_HEX_CHECK, A, v); } 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) { + 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) { static const A defaultV = 42; - A convV; + A convV; UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToString(v), convV), true); // char UNIT_ASSERT_VALUES_EQUAL(v, convV); UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToWtring(v), convV), true); // wide char UNIT_ASSERT_VALUES_EQUAL(v, convV); - + TRY_HEX_MACROS_MAP(TRY_OK_HEX_CHECK, A, v, convV, defaultV); - } - - template <class B> - inline void CheckTryFail(B v) { + } + + template <class B> + inline void CheckTryFail(B v) { static const A defaultV = 42; A convV = defaultV; // to check that original value is not trashed on bad cast UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToString(v), convV), false); // char @@ -88,35 +88,35 @@ struct TRet { UNIT_ASSERT_VALUES_EQUAL(defaultV, convV); TRY_HEX_MACROS_MAP(TRY_FAIL_HEX_CHECK, A, v, convV, defaultV); - } + } }; template <> struct TRet<bool> { template <class B> - inline void CheckOK(B v) { + inline void CheckOK(B v) { UNIT_ASSERT_VALUES_EQUAL(FromString<bool>(ToString(v)), v); } template <class B> - inline void CheckTryOK(B v) { - B convV; + inline void CheckTryOK(B v) { + B convV; UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), true); UNIT_ASSERT_VALUES_EQUAL(v, convV); - } - - template <class B> - inline void CheckExc(B v) { + } + + template <class B> + inline void CheckExc(B v) { UNIT_ASSERT_EXCEPTION(FromString<bool>(ToString(v)), yexception); } - - template <class B> - inline void CheckTryFail(B v) { + + template <class B> + inline void CheckTryFail(B v) { static const bool defaultV = false; bool convV = defaultV; UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), false); UNIT_ASSERT_VALUES_EQUAL(defaultV, convV); - } + } }; template <class A> @@ -125,30 +125,30 @@ inline TRet<A> F() { }; #if 0 -template <class T> +template <class T> inline void CheckConvertToBuffer(const T& value, const size_t size, const TString& canonValue) { - const size_t maxSize = 256; - char buffer[maxSize]; - const char magic = 0x7F; - memset(buffer, magic, maxSize); - size_t length = 0; - if (canonValue.size() > size) { // overflow will occur - UNIT_ASSERT_EXCEPTION(length = ToString(value, buffer, size), yexception); - // check that no bytes after size was trashed - for (size_t i = size; i < maxSize; ++i) + const size_t maxSize = 256; + char buffer[maxSize]; + const char magic = 0x7F; + memset(buffer, magic, maxSize); + size_t length = 0; + if (canonValue.size() > size) { // overflow will occur + UNIT_ASSERT_EXCEPTION(length = ToString(value, buffer, size), yexception); + // check that no bytes after size was trashed + for (size_t i = size; i < maxSize; ++i) UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); - } else { - length = ToString(value, buffer, size); - UNIT_ASSERT(length < maxSize); - // check that no bytes after length was trashed - for (size_t i = length; i < maxSize; ++i) + } else { + length = ToString(value, buffer, size); + UNIT_ASSERT(length < maxSize); + // check that no bytes after length was trashed + for (size_t i = length; i < maxSize; ++i) UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); - TStringBuf result(buffer, length); + TStringBuf result(buffer, length); UNIT_ASSERT_VALUES_EQUAL(result, TStringBuf(canonValue)); - } -} + } +} #endif - + Y_UNIT_TEST_SUITE(TCastTest) { template <class A> inline TRet<A> F() { @@ -332,14 +332,14 @@ Y_UNIT_TEST_SUITE(TCastTest) { CheckConvertToBuffer<float>(1.f, 5, "1"); CheckConvertToBuffer<float>(1.005f, 3, "1.005"); CheckConvertToBuffer<float>(1.00000000f, 3, "1"); - + CheckConvertToBuffer<double>(1.f, 5, "1"); CheckConvertToBuffer<double>(1.005f, 3, "1.005"); CheckConvertToBuffer<double>(1.00000000f, 3, "1"); - + CheckConvertToBuffer<int>(2, 5, "2"); CheckConvertToBuffer<int>(1005, 3, "1005"); - + CheckConvertToBuffer<size_t>(2, 5, "2"); CheckConvertToBuffer<ui64>(1005000000000000ull, 32, "1005000000000000"); CheckConvertToBuffer<ui64>(1005000000000000ull, 3, "1005000000000000"); @@ -348,29 +348,29 @@ Y_UNIT_TEST_SUITE(TCastTest) { // UNIT_ASSERT_EXCEPTION(FromString<double>(longNumber), yexception); } #endif - + Y_UNIT_TEST(TestWide) { TUtf16String iw = u"-100500"; int iv = 0; UNIT_ASSERT_VALUES_EQUAL(TryFromString(iw, iv), true); UNIT_ASSERT_VALUES_EQUAL(iv, -100500); - + ui64 uv = 0; TUtf16String uw = u"21474836470"; UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); - + TWtringBuf bw(uw.data(), uw.size()); uv = 0; UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); - + const wchar16* beg = uw.data(); uv = 0; UNIT_ASSERT_VALUES_EQUAL(TryFromString(beg, uw.size(), uv), true); UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); } - + Y_UNIT_TEST(TestDefault) { size_t res = 0; const size_t def1 = 42; @@ -438,17 +438,17 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestBool) { - // True cases - UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("yes"), true); - UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("1"), true); - // False cases - UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("no"), false); - UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("0"), false); - // Strange cases - UNIT_ASSERT_EXCEPTION(FromString<bool>(""), yexception); - UNIT_ASSERT_EXCEPTION(FromString<bool>("something"), yexception); - } - + // True cases + UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("yes"), true); + UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("1"), true); + // False cases + UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("no"), false); + UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("0"), false); + // Strange cases + UNIT_ASSERT_EXCEPTION(FromString<bool>(""), yexception); + UNIT_ASSERT_EXCEPTION(FromString<bool>("something"), yexception); + } + Y_UNIT_TEST(TestAutoDetectType) { UNIT_ASSERT_DOUBLES_EQUAL((float)FromString("0.0001"), 0.0001, EPS); UNIT_ASSERT_DOUBLES_EQUAL((double)FromString("0.0015", sizeof("0.0015") - 2), 0.001, EPS); @@ -463,52 +463,52 @@ Y_UNIT_TEST_SUITE(TCastTest) { ui16 wideCharacterCode = FromString(u"125"); UNIT_ASSERT_VALUES_EQUAL(integer, wideCharacterCode); } - + static void CheckMessage(TFromStringException& exc, const TString& phrase) { TString message = exc.what(); - if (!message.Contains(phrase)) { - Cerr << message << Endl; - UNIT_ASSERT(false); - } - } - + if (!message.Contains(phrase)) { + Cerr << message << Endl; + UNIT_ASSERT(false); + } + } + Y_UNIT_TEST(ErrorMessages) { - try { - FromString<ui32>(""); - UNIT_ASSERT(false); + try { + FromString<ui32>(""); + UNIT_ASSERT(false); } catch (TFromStringException& e) { - CheckMessage(e, "empty string as number"); - } - - try { - FromString<ui32>("-"); - UNIT_ASSERT(false); + CheckMessage(e, "empty string as number"); + } + + try { + FromString<ui32>("-"); + UNIT_ASSERT(false); } catch (TFromStringException& e) { - // Unsigned should have no sign at all, so - is not expected - CheckMessage(e, "Unexpected symbol \"-\" at pos 0 in string \"-\""); - } - - try { - FromString<i32>("-"); - UNIT_ASSERT(false); + // Unsigned should have no sign at all, so - is not expected + CheckMessage(e, "Unexpected symbol \"-\" at pos 0 in string \"-\""); + } + + try { + FromString<i32>("-"); + UNIT_ASSERT(false); } catch (TFromStringException& e) { - CheckMessage(e, "Cannot parse string \"-\" as number"); - } - - try { - FromString<i32>("+"); - UNIT_ASSERT(false); + CheckMessage(e, "Cannot parse string \"-\" as number"); + } + + try { + FromString<i32>("+"); + UNIT_ASSERT(false); } catch (TFromStringException& e) { - CheckMessage(e, "Cannot parse string \"+\" as number"); - } - - try { - FromString<ui32>("0.328413745072"); - UNIT_ASSERT(false); + CheckMessage(e, "Cannot parse string \"+\" as number"); + } + + try { + FromString<ui32>("0.328413745072"); + UNIT_ASSERT(false); } catch (TFromStringException& e) { - CheckMessage(e, "Unexpected symbol \".\" at pos 1 in string \"0.328413745072\""); - } - } + CheckMessage(e, "Unexpected symbol \".\" at pos 1 in string \"0.328413745072\""); + } + } Y_UNIT_TEST(TryStringBuf) { { diff --git a/util/string/hex.h b/util/string/hex.h index c4605c6087..af3d2d528f 100644 --- a/util/string/hex.h +++ b/util/string/hex.h @@ -1,7 +1,7 @@ #pragma once #include <util/generic/string.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/system/yassert.h> inline static char DigitToChar(unsigned char digit) { diff --git a/util/string/strip.cpp b/util/string/strip.cpp index bcc63f0e81..c921571cf0 100644 --- a/util/string/strip.cpp +++ b/util/string/strip.cpp @@ -8,10 +8,10 @@ bool Collapse(const TString& from, TString& to, size_t maxLen) { } void CollapseText(const TString& from, TString& to, size_t maxLen) { - Collapse(from, to, maxLen); + Collapse(from, to, maxLen); StripInPlace(to); if (to.size() >= maxLen) { - to.remove(maxLen - 5); // " ..." + to.remove(maxLen - 5); // " ..." ReverseInPlace(to); size_t pos = to.find_first_of(" .,;"); if (pos != TString::npos && pos < 32) { diff --git a/util/string/strip.h b/util/string/strip.h index eb9b684941..d5ef6da96d 100644 --- a/util/string/strip.h +++ b/util/string/strip.h @@ -170,29 +170,29 @@ static inline T StripStringRight(const T& from) { template <class T, class TStripCriterion> static inline T StripStringLeft(const T& from, TStripCriterion&& criterion) { return TStripImpl<true, false>::StripString(from, criterion); -} - +} + template <class T, class TStripCriterion> static inline T StripStringRight(const T& from, TStripCriterion&& criterion) { return TStripImpl<false, true>::StripString(from, criterion); -} - +} + /// Copies the given string removing leading and trailing spaces. static inline bool Strip(const TString& from, TString& to) { - return StripString(from, to); -} + return StripString(from, to); +} /// Removes leading and trailing spaces from the string. inline TString& StripInPlace(TString& s) { - Strip(s, s); - return s; -} + Strip(s, s); + return s; +} /// Returns a copy of the given string with removed leading and trailing spaces. inline TString Strip(const TString& s) Y_WARN_UNUSED_RESULT; inline TString Strip(const TString& s) { TString ret = s; - Strip(ret, ret); + Strip(ret, ret); return ret; } @@ -234,7 +234,7 @@ bool Collapse(const TString& from, TString& to, size_t maxLen = 0); /// Replaces several consequtive space symbols with one (processing is limited to maxLen bytes) inline TString& CollapseInPlace(TString& s, size_t maxLen = 0) { - Collapse(s, s, maxLen); + Collapse(s, s, maxLen); return s; } @@ -242,7 +242,7 @@ inline TString& CollapseInPlace(TString& s, size_t maxLen = 0) { inline TString Collapse(const TString& s, size_t maxLen = 0) Y_WARN_UNUSED_RESULT; inline TString Collapse(const TString& s, size_t maxLen) { TString ret; - Collapse(s, ret, maxLen); + Collapse(s, ret, maxLen); return ret; } @@ -252,6 +252,6 @@ void CollapseText(const TString& from, TString& to, size_t maxLen); /// @details An ellipsis is inserted at the end of the truncated line. inline void CollapseText(TString& s, size_t maxLen) { TString to; - CollapseText(s, to, maxLen); + CollapseText(s, to, maxLen); s = to; } diff --git a/util/string/strip_ut.cpp b/util/string/strip_ut.cpp index 0a6b8e362f..d1029d1498 100644 --- a/util/string/strip_ut.cpp +++ b/util/string/strip_ut.cpp @@ -6,25 +6,25 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { Y_UNIT_TEST(TestStrip) { - struct TTest { + struct TTest { const char* Str; const char* StripLeftRes; const char* StripRightRes; const char* StripRes; - }; - static const TTest tests[] = { - {" 012 ", "012 ", " 012", "012"}, - {" 012", "012", " 012", "012"}, - {"012\t\t", "012\t\t", "012", "012"}, - {"\t012\t", "012\t", "\t012", "012"}, - {"012", "012", "012", "012"}, - {"012\r\n", "012\r\n", "012", "012"}, - {"\n012\r", "012\r", "\n012", "012"}, - {"\n \t\r", "", "", ""}, - {"", "", "", ""}, - {"abc", "abc", "abc", "abc"}, - {"a c", "a c", "a c", "a c"}, - }; + }; + static const TTest tests[] = { + {" 012 ", "012 ", " 012", "012"}, + {" 012", "012", " 012", "012"}, + {"012\t\t", "012\t\t", "012", "012"}, + {"\t012\t", "012\t", "\t012", "012"}, + {"012", "012", "012", "012"}, + {"012\r\n", "012\r\n", "012", "012"}, + {"\n012\r", "012\r", "\n012", "012"}, + {"\n \t\r", "", "", ""}, + {"", "", "", ""}, + {"abc", "abc", "abc", "abc"}, + {"a c", "a c", "a c", "a c"}, + }; for (const auto& test : tests) { TString inputStr(test.Str); @@ -45,16 +45,16 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { } Y_UNIT_TEST(TestCustomStrip) { - struct TTest { + struct TTest { const char* Str; const char* Result; - }; - static const TTest tests[] = { - {"//012//", "012"}, - {"//012", "012"}, - {"012", "012"}, - {"012//", "012"}, - }; + }; + static const TTest tests[] = { + {"//012//", "012"}, + {"//012", "012"}, + {"012", "012"}, + {"012//", "012"}, + }; for (auto test : tests) { UNIT_ASSERT_EQUAL( @@ -64,28 +64,28 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { } Y_UNIT_TEST(TestCustomStripLeftRight) { - struct TTest { - const char* Str; - const char* ResultLeft; - const char* ResultRight; - }; - static const TTest tests[] = { - {"//012//", "012//", "//012"}, - {"//012", "012", "//012"}, - {"012", "012", "012"}, - {"012//", "012//", "012"}, - }; - + struct TTest { + const char* Str; + const char* ResultLeft; + const char* ResultRight; + }; + static const TTest tests[] = { + {"//012//", "012//", "//012"}, + {"//012", "012", "//012"}, + {"012", "012", "012"}, + {"012//", "012//", "012"}, + }; + for (const auto& test : tests) { - UNIT_ASSERT_EQUAL( + UNIT_ASSERT_EQUAL( StripStringLeft(TString(test.Str), EqualsStripAdapter('/')), test.ResultLeft); - UNIT_ASSERT_EQUAL( + UNIT_ASSERT_EQUAL( StripStringRight(TString(test.Str), EqualsStripAdapter('/')), test.ResultRight); - }; - } - + }; + } + Y_UNIT_TEST(TestNullStringStrip) { TStringBuf nullString(nullptr, nullptr); UNIT_ASSERT_EQUAL( @@ -128,11 +128,11 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { TString abs1("Very long description string written in unknown language."); TString abs2(abs1); TString abs3(abs1); - CollapseText(abs1, 204); - CollapseText(abs2, 54); - CollapseText(abs3, 49); - UNIT_ASSERT_EQUAL(abs1 == "Very long description string written in unknown language.", true); - UNIT_ASSERT_EQUAL(abs2 == "Very long description string written in unknown ...", true); - UNIT_ASSERT_EQUAL(abs3 == "Very long description string written in ...", true); + CollapseText(abs1, 204); + CollapseText(abs2, 54); + CollapseText(abs3, 49); + UNIT_ASSERT_EQUAL(abs1 == "Very long description string written in unknown language.", true); + UNIT_ASSERT_EQUAL(abs2 == "Very long description string written in unknown ...", true); + UNIT_ASSERT_EQUAL(abs3 == "Very long description string written in ...", true); } } diff --git a/util/string/subst.h b/util/string/subst.h index ab8ea56a56..45b622fbef 100644 --- a/util/string/subst.h +++ b/util/string/subst.h @@ -3,7 +3,7 @@ #include <util/generic/fwd.h> #include <stlfwd> - + /* Replace all occurences of substring `what` with string `with` starting from position `from`. * * @param text String to modify. @@ -18,7 +18,7 @@ size_t SubstGlobal(std::string& text, TStringBuf what, TStringBuf with, size_t f size_t SubstGlobal(TUtf16String& text, TWtringBuf what, TWtringBuf with, size_t from = 0); size_t SubstGlobal(std::u16string& text, TWtringBuf what, TWtringBuf with, size_t from = 0); size_t SubstGlobal(TUtf32String& text, TUtf32StringBuf what, TUtf32StringBuf with, size_t from = 0); - + /* Replace all occurences of character `what` with character `with` starting from position `from`. * * @param text String to modify. diff --git a/util/string/subst_ut.cpp b/util/string/subst_ut.cpp index 04ca5cdf60..21eccef779 100644 --- a/util/string/subst_ut.cpp +++ b/util/string/subst_ut.cpp @@ -1,6 +1,6 @@ #include "join.h" #include "subst.h" -#include <string> +#include <string> #include <library/cpp/testing/unittest/registar.h> @@ -169,7 +169,7 @@ Y_UNIT_TEST_SUITE(TStringSubst) { SubstGlobal(s, 'a', 'b', 1); UNIT_ASSERT_EQUAL(s, TString("abb")); } - + Y_UNIT_TEST(TestSubstCharGlobalRet) { const TUtf16String w1 = u"abcdabcd"; const TUtf16String w2 = SubstGlobalCopy(w1, wchar16('b'), wchar16('B'), 3); @@ -181,10 +181,10 @@ Y_UNIT_TEST_SUITE(TStringSubst) { } Y_UNIT_TEST(TestSubstStdString) { - std::string s = "aaa"; - SubstGlobal(s, "a", "b", 1); - UNIT_ASSERT_EQUAL(s, "abb"); - } + std::string s = "aaa"; + SubstGlobal(s, "a", "b", 1); + UNIT_ASSERT_EQUAL(s, "abb"); + } Y_UNIT_TEST(TestSubstStdStringRet) { const std::string s1 = "aaa"; diff --git a/util/string/type_ut.cpp b/util/string/type_ut.cpp index 3916914e23..03e7af62bd 100644 --- a/util/string/type_ut.cpp +++ b/util/string/type_ut.cpp @@ -13,31 +13,31 @@ Y_UNIT_TEST_SUITE(TStringClassify) { } Y_UNIT_TEST(TestIsTrue) { - UNIT_ASSERT(IsTrue("1")); - UNIT_ASSERT(IsTrue("yes")); - UNIT_ASSERT(IsTrue("YeS")); - UNIT_ASSERT(IsTrue("on")); - UNIT_ASSERT(IsTrue("true")); + UNIT_ASSERT(IsTrue("1")); + UNIT_ASSERT(IsTrue("yes")); + UNIT_ASSERT(IsTrue("YeS")); + UNIT_ASSERT(IsTrue("on")); + UNIT_ASSERT(IsTrue("true")); UNIT_ASSERT(IsTrue("t")); - UNIT_ASSERT(IsTrue("da")); + UNIT_ASSERT(IsTrue("da")); UNIT_ASSERT(!IsTrue("")); UNIT_ASSERT(!IsTrue("tr")); - UNIT_ASSERT(!IsTrue("foobar")); + UNIT_ASSERT(!IsTrue("foobar")); } - + Y_UNIT_TEST(TestIsFalse) { - UNIT_ASSERT(IsFalse("0")); - UNIT_ASSERT(IsFalse("no")); - UNIT_ASSERT(IsFalse("off")); - UNIT_ASSERT(IsFalse("false")); + UNIT_ASSERT(IsFalse("0")); + UNIT_ASSERT(IsFalse("no")); + UNIT_ASSERT(IsFalse("off")); + UNIT_ASSERT(IsFalse("false")); UNIT_ASSERT(IsFalse("f")); - UNIT_ASSERT(IsFalse("net")); - + UNIT_ASSERT(IsFalse("net")); + UNIT_ASSERT(!IsFalse("")); UNIT_ASSERT(!IsFalse("fa")); - UNIT_ASSERT(!IsFalse("foobar")); - } + UNIT_ASSERT(!IsFalse("foobar")); + } Y_UNIT_TEST(TestIsNumber) { UNIT_ASSERT(IsNumber("0")); |