cast.h 5.9 KB

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