cast.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "any_of.h"
  18. #define SAFE_NUMBER_PARSE(func, ...) \
  19. [](char const *in, char **out) { return func(in, out, ##__VA_ARGS__); }
  20. namespace string_utils {
  21. // A helper object for providing partial specializations for casting
  22. template <typename, typename = void> struct cast_helper;
  23. // The main parser
  24. template <typename T, typename S> std::pair<T, bool> cast(S const &str) noexcept;
  25. template <typename T> bool cast(std::string_view str, T & to) noexcept;
  26. template <typename S, typename T> bool cast(std::vector<S> const &strs, T & to) noexcept;
  27. }
  28. namespace string_utils::detail {
  29. template <typename> struct always_false : std::false_type {};
  30. template <typename, typename, typename = void> struct has_cast_helper : std::false_type {};
  31. template <typename T, typename S>
  32. struct has_cast_helper<T, S, std::void_t<decltype(cast_helper<T>{}(S{}, T{}))>>
  33. : std::true_type {};
  34. template <typename, typename = void> struct is_tuple : std::false_type {};
  35. template <typename T>
  36. struct is_tuple<T, std::void_t<typename std::tuple_size<T>::type>> : std::true_type {};
  37. template <typename, typename = void> struct is_associative : std::false_type {};
  38. template <typename T>
  39. struct is_associative<T, std::void_t<typename T::mapped_type>> : std::true_type {};
  40. template <typename, typename = void> struct is_container : std::false_type {};
  41. template <typename T>
  42. struct is_container<T, std::void_t<typename T::value_type>> : std::true_type {};
  43. template <typename T> struct decay { using type = std::decay_t<T>; };
  44. template <template <typename...> class C, typename... Ts>
  45. struct decay<C<Ts...>> { using type = C<std::decay_t<Ts>...>; };
  46. template <typename T> using decay_t = typename decay<std::decay_t<T>>::type;
  47. }
  48. namespace string_utils::detail {
  49. template <typename Tuple, size_t... Is>
  50. bool cast_tuple(std::vector<std::string> const & str, Tuple & to,
  51. std::index_sequence<Is...>) noexcept;
  52. template <typename T, typename F>
  53. bool cast_number(std::string_view str, T & to, F func) noexcept;
  54. template <typename S> std::vector<S> keyval(S const &input);
  55. }
  56. namespace string_utils {
  57. template <typename T>
  58. bool cast(std::string_view str, T & to) noexcept { return cast_helper<T>{}(str, to); }
  59. template <typename S, typename T>
  60. bool cast(std::vector<S> const &strs, T & to) noexcept {
  61. if constexpr (detail::is_tuple<T>{}) {
  62. constexpr size_t N = std::tuple_size_v<T>;
  63. return strs.size() == N && detail::cast_tuple(strs, to, std::make_index_sequence<N>{});
  64. } else if constexpr (detail::is_associative<T>{}) {
  65. for (S const &elem : strs) {
  66. auto [tmp, success] = cast<typename T::value_type>(detail::keyval(elem));
  67. if (!success) { return false; }
  68. to.insert(std::move(tmp));
  69. }
  70. } else if constexpr (detail::is_container<T>{}) {
  71. for (S const &elem : strs) {
  72. auto [tmp, success] = cast<typename T::value_type>(elem);
  73. if (!success) { return false; }
  74. to.insert(to.end(), std::move(tmp));
  75. }
  76. } else {
  77. return cast_helper<T>{}(strs, to);
  78. }
  79. return true;
  80. }
  81. template <typename V, typename S, typename T> bool maybe_cast(S const & str, T & to) noexcept {
  82. auto [rval, found] = cast<V>(str);
  83. if (found) { to = std::move(rval); }
  84. return found;
  85. }
  86. template <typename... Ts>
  87. struct cast_helper<std::variant<Ts...>> {
  88. bool operator()(std::string_view str, std::variant<Ts...> & to) const noexcept {
  89. return (maybe_cast<Ts>(str, to) || ...);
  90. }
  91. };
  92. template <typename T>
  93. struct cast_helper<std::optional<T>> {
  94. bool operator()(std::string_view str, std::optional<T> & to) const noexcept {
  95. return maybe_cast<T>(str, to) || true;
  96. }
  97. };
  98. }
  99. namespace string_utils {
  100. inline bool cast(std::string_view str, std::string & to) noexcept {
  101. to = std::string(str);
  102. return true;
  103. }
  104. inline bool cast(std::string_view str, long & to) noexcept {
  105. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtol, 10));
  106. }
  107. inline bool cast(std::string_view str, long long & to) noexcept {
  108. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtoll, 10));
  109. }
  110. inline bool cast(std::string_view str, float & to) noexcept {
  111. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtof));
  112. }
  113. inline bool cast(std::string_view str, double & to) noexcept {
  114. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtod));
  115. }
  116. inline bool cast(std::string_view str, long double & to) noexcept {
  117. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtold));
  118. }
  119. inline bool cast(std::string_view str, int & to) noexcept {
  120. auto [tmp, success] = cast<long>(str);
  121. to = static_cast<int>(tmp);
  122. return success && tmp == static_cast<long>(to);
  123. }
  124. inline bool cast(std::string_view str, bool & to) noexcept {
  125. if (any_of(str, "true", "TRUE", "YES", "1")) {
  126. to = true;
  127. return true;
  128. } else if (any_of(str, "false", "FALSE", "NO", "0")) {
  129. to = false;
  130. return true;
  131. }
  132. return false;
  133. }
  134. }
  135. namespace string_utils::detail {
  136. template <typename Tuple, size_t... Is>
  137. bool cast_tuple(std::vector<std::string> const & str, Tuple & to,
  138. std::index_sequence<Is...>) noexcept {
  139. return ((cast(str[Is], std::get<Is>(to))) && ...);
  140. }
  141. template <typename T, typename F>
  142. bool cast_number(std::string_view str, T & to, F func) noexcept {
  143. char *counter = nullptr;
  144. to = func(str.data(), &counter);
  145. return counter == str.end();
  146. }
  147. template <typename S> std::vector<S> keyval(S const &input) {
  148. size_t const pos = input.find('=');
  149. return pos == S::npos ? std::vector{input}
  150. : std::vector{input.substr(0, pos), input.substr(pos + 1)};
  151. }
  152. }
  153. // This should be placed last in the file
  154. namespace string_utils {
  155. template <typename T, typename S> std::pair<T, bool> cast(S const & str) noexcept {
  156. std::pair<detail::decay_t<T>, bool> rval;
  157. rval.second = cast(str, rval.first);
  158. return rval;
  159. }
  160. }
  161. #undef CAST_NUMBER_IMPL