cast.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // cast.h
  3. // string-utils
  4. //
  5. // Created by Sam Jaffe on 2/13/21.
  6. // Copyright © 2021 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <cstdlib>
  10. #include <optional>
  11. #include <string>
  12. #include <string_view>
  13. #include <type_traits>
  14. #include <utility>
  15. #include <variant>
  16. #include <vector>
  17. #include "string_utils/any_of.h"
  18. #include "string_utils/forwards.h"
  19. #include "string_utils/traits.h"
  20. #define SAFE_NUMBER_PARSE(func, ...) \
  21. [](char const * in, char ** out) { return func(in, out, ##__VA_ARGS__); }
  22. namespace string_utils::detail {
  23. template <typename Actual, typename Out>
  24. bool ctor_cast(Out & out, std::string_view str) noexcept {
  25. auto [rval, found] = cast<Actual>(str);
  26. if (found) { out = Out(std::move(rval)); }
  27. return found;
  28. }
  29. template <typename Tuple, size_t... Is>
  30. bool cast_tuple(std::vector<std::string_view> const & str, Tuple & to,
  31. std::index_sequence<Is...>) noexcept {
  32. return ((cast(std::get<Is>(to), str[Is])) && ...);
  33. }
  34. template <typename T, typename F>
  35. bool cast_number(std::string_view str, T & to, F func) noexcept {
  36. char * counter = nullptr;
  37. to = func(str.data(), &counter);
  38. return counter == str.end();
  39. }
  40. inline std::vector<std::string_view> keyval(std::string_view input) noexcept {
  41. if (size_t const pos = input.find('='); pos < input.size()) {
  42. return {input.substr(0, pos), input.substr(pos + 1)};
  43. }
  44. return {input};
  45. }
  46. inline bool cast_bool(bool & out, std::string_view str) noexcept {
  47. if (any_of(str, "true", "TRUE", "YES", "1")) {
  48. out = true;
  49. return true;
  50. } else if (any_of(str, "false", "FALSE", "NO", "0")) {
  51. out = false;
  52. return true;
  53. }
  54. return false;
  55. }
  56. }
  57. // This should be placed last in the file
  58. namespace string_utils {
  59. template <typename Out> bool cast(Out & out, std::string_view str) noexcept {
  60. if constexpr (std::is_same_v<Out, long>) {
  61. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtol, 10));
  62. } else if constexpr (std::is_same_v<Out, unsigned long>) {
  63. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoul, 10));
  64. } else if constexpr (std::is_same_v<Out, long long>) {
  65. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoll, 10));
  66. } else if constexpr (std::is_same_v<Out, unsigned long long>) {
  67. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoull, 10));
  68. } else if constexpr (std::is_same_v<Out, float>) {
  69. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtof));
  70. } else if constexpr (std::is_same_v<Out, double>) {
  71. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtod));
  72. } else if constexpr (std::is_same_v<Out, long double>) {
  73. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtold));
  74. } else if constexpr (std::is_same_v<Out, bool>) {
  75. return detail::cast_bool(out, str);
  76. } else if constexpr (std::is_same_v<Out, char>) {
  77. out = str[0];
  78. return str.size() == 1;
  79. } else if constexpr (std::is_constructible_v<Out, std::string_view>) {
  80. out = Out(str);
  81. return true;
  82. } else if constexpr (std::is_integral_v<Out>) {
  83. using V = std::conditional_t<std::is_unsigned_v<Out>, unsigned long, long>;
  84. auto [tmp, success] = cast<V>(str);
  85. out = static_cast<Out>(tmp);
  86. return success && tmp == static_cast<V>(out);
  87. } else {
  88. static_assert(detail::always_false<Out>{}, "No match for cast(string)");
  89. }
  90. }
  91. template <typename... Ts>
  92. bool cast(std::variant<Ts...> & out, std::string_view str) noexcept {
  93. return (detail::ctor_cast<Ts>(out, str) || ...);
  94. }
  95. template <typename Out>
  96. bool cast(std::optional<Out> & out, std::string_view str) noexcept {
  97. return detail::ctor_cast<Out>(out, str) || true;
  98. }
  99. template <typename Out>
  100. bool cast(Out & out, std::vector<std::string_view> const & strs) noexcept {
  101. if constexpr (detail::is_associative_v<Out>) {
  102. for (auto elem : strs) {
  103. auto [tmp, success] =
  104. cast<typename Out::value_type>(detail::keyval(elem));
  105. if (!success) { return false; }
  106. out.insert(std::move(tmp));
  107. }
  108. return true;
  109. } else if constexpr (detail::is_container_v<Out>) {
  110. for (auto elem : strs) {
  111. auto [tmp, success] = cast<typename Out::value_type>(elem);
  112. if (!success) { return false; }
  113. out.insert(out.end(), std::move(tmp));
  114. }
  115. return true;
  116. } else if constexpr (detail::is_tuple_v<Out>) {
  117. constexpr size_t N = std::tuple_size_v<Out>;
  118. return strs.size() == N &&
  119. detail::cast_tuple(strs, out, std::make_index_sequence<N>{});
  120. } else {
  121. static_assert(detail::always_false<Out>{},
  122. "No match for cast(vector<string>)");
  123. }
  124. }
  125. template <typename T> std::pair<T, bool> cast(std::string_view str) noexcept {
  126. std::pair<detail::decay_t<T>, bool> rval;
  127. using ::string_utils::cast;
  128. rval.second = cast(rval.first, str);
  129. return rval;
  130. }
  131. template <typename T, typename S>
  132. std::pair<T, bool> cast(std::vector<S> const & strs) noexcept {
  133. std::vector<std::string_view> tmp{strs.begin(), strs.end()};
  134. std::pair<detail::decay_t<T>, bool> rval;
  135. using ::string_utils::cast;
  136. rval.second = cast(rval.first, tmp);
  137. return rval;
  138. }
  139. }
  140. #undef CAST_NUMBER_IMPL