type.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "type.h"
  2. #include "ascii.h"
  3. #include <array>
  4. bool IsSpace(const char* s, size_t len) noexcept {
  5. if (len == 0) {
  6. return false;
  7. }
  8. for (const char* p = s; p < s + len; ++p) {
  9. if (!IsAsciiSpace(*p)) {
  10. return false;
  11. }
  12. }
  13. return true;
  14. }
  15. template <typename TStringType>
  16. static bool IsNumberT(const TStringType& s) noexcept {
  17. if (s.empty()) {
  18. return false;
  19. }
  20. return std::all_of(s.begin(), s.end(), IsAsciiDigit<typename TStringType::value_type>);
  21. }
  22. bool IsNumber(const TStringBuf s) noexcept {
  23. return IsNumberT(s);
  24. }
  25. bool IsNumber(const TWtringBuf s) noexcept {
  26. return IsNumberT(s);
  27. }
  28. template <typename TStringType>
  29. static bool IsHexNumberT(const TStringType& s) noexcept {
  30. if (s.empty()) {
  31. return false;
  32. }
  33. return std::all_of(s.begin(), s.end(), IsAsciiHex<typename TStringType::value_type>);
  34. }
  35. bool IsHexNumber(const TStringBuf s) noexcept {
  36. return IsHexNumberT(s);
  37. }
  38. bool IsHexNumber(const TWtringBuf s) noexcept {
  39. return IsHexNumberT(s);
  40. }
  41. namespace {
  42. template <size_t N>
  43. bool IsCaseInsensitiveAnyOf(TStringBuf str, const std::array<TStringBuf, N>& options) {
  44. for (auto option : options) {
  45. if (str.size() == option.size() && ::strnicmp(str.data(), option.data(), str.size()) == 0) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. } //anonymous namespace
  52. bool IsTrue(const TStringBuf v) noexcept {
  53. static constexpr std::array<TStringBuf, 7> trueOptions{
  54. "true",
  55. "t",
  56. "yes",
  57. "y",
  58. "on",
  59. "1",
  60. "da"};
  61. return IsCaseInsensitiveAnyOf(v, trueOptions);
  62. }
  63. bool IsFalse(const TStringBuf v) noexcept {
  64. static constexpr std::array<TStringBuf, 7> falseOptions{
  65. "false",
  66. "f",
  67. "no",
  68. "n",
  69. "off",
  70. "0",
  71. "net"};
  72. return IsCaseInsensitiveAnyOf(v, falseOptions);
  73. }