diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/string/type.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/string/type.cpp')
-rw-r--r-- | util/string/type.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/util/string/type.cpp b/util/string/type.cpp new file mode 100644 index 0000000000..49671c02c2 --- /dev/null +++ b/util/string/type.cpp @@ -0,0 +1,86 @@ +#include "type.h" +#include "ascii.h" + +#include <array> + +bool IsSpace(const char* s, size_t len) noexcept { + if (len == 0) { + return false; + } + for (const char* p = s; p < s + len; ++p) { + if (!IsAsciiSpace(*p)) { + return false; + } + } + return true; +} + +template <typename TStringType> +static bool IsNumberT(const TStringType& s) noexcept { + if (s.empty()) { + return false; + } + + return std::all_of(s.begin(), s.end(), IsAsciiDigit<typename TStringType::value_type>); +} + +bool IsNumber(const TStringBuf s) noexcept { + return IsNumberT(s); +} + +bool IsNumber(const TWtringBuf s) noexcept { + return IsNumberT(s); +} + +template <typename TStringType> +static bool IsHexNumberT(const TStringType& s) noexcept { + if (s.empty()) { + return false; + } + + return std::all_of(s.begin(), s.end(), IsAsciiHex<typename TStringType::value_type>); +} + +bool IsHexNumber(const TStringBuf s) noexcept { + return IsHexNumberT(s); +} + +bool IsHexNumber(const TWtringBuf s) noexcept { + return IsHexNumberT(s); +} + +namespace { + template <size_t N> + bool IsCaseInsensitiveAnyOf(TStringBuf str, const std::array<TStringBuf, N>& options) { + for (auto option : options) { + if (str.size() == option.size() && ::strnicmp(str.data(), option.data(), str.size()) == 0) { + return true; + } + } + return false; + } +} //anonymous namespace + +bool IsTrue(const TStringBuf v) noexcept { + static constexpr std::array<TStringBuf, 7> trueOptions{ + "true", + "t", + "yes", + "y", + "on", + "1", + "da"}; + return IsCaseInsensitiveAnyOf(v, trueOptions); +} + +bool IsFalse(const TStringBuf v) noexcept { + static constexpr std::array<TStringBuf, 7> falseOptions{ + "false", + "f", + "no", + "n", + "off", + "0", + "net"}; + return IsCaseInsensitiveAnyOf(v, falseOptions); +} |