cast.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #if __has_include(<magic_enum/magic_enum.hpp>)
  18. #include <magic_enum/magic_enum.hpp>
  19. #include <magic_enum/magic_enum_flags.hpp>
  20. #endif
  21. #include "string_utils/any_of.h"
  22. #include "string_utils/forwards.h"
  23. #include "string_utils/traits.h"
  24. #define SAFE_NUMBER_PARSE(func, ...) \
  25. [](char const * in, char ** out) { return func(in, out, ##__VA_ARGS__); }
  26. namespace string_utils::detail {
  27. template <typename Actual, typename Out>
  28. bool ctor_cast(Out & out, std::string_view str) noexcept {
  29. auto [rval, found] = cast<Actual>(str);
  30. if (found) { out = Out(std::move(rval)); }
  31. return found;
  32. }
  33. template <typename Tuple, size_t... Is>
  34. bool cast_tuple(std::vector<std::string_view> const & str, Tuple & to,
  35. std::index_sequence<Is...>) noexcept {
  36. return ((cast(std::get<Is>(to), str[Is])) && ...);
  37. }
  38. template <typename T, typename F>
  39. bool cast_number(std::string_view str, T & to, F func) noexcept {
  40. char * counter = nullptr;
  41. to = func(str.data(), &counter);
  42. return counter == str.end();
  43. }
  44. inline std::vector<std::string_view> keyval(std::string_view input) noexcept {
  45. if (size_t const pos = input.find('='); pos < input.size()) {
  46. return {input.substr(0, pos), input.substr(pos + 1)};
  47. }
  48. return {input};
  49. }
  50. inline bool cast_bool(bool & out, std::string_view str) noexcept {
  51. if (any_of(str, "true", "TRUE", "YES", "1")) {
  52. out = true;
  53. return true;
  54. } else if (any_of(str, "false", "FALSE", "NO", "0")) {
  55. out = false;
  56. return true;
  57. }
  58. return false;
  59. }
  60. }
  61. // This should be placed last in the file
  62. namespace string_utils {
  63. template <typename Out> bool cast(Out & out, std::string_view str) noexcept {
  64. if constexpr (std::is_same_v<Out, long>) {
  65. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtol, 10));
  66. } else if constexpr (std::is_same_v<Out, unsigned long>) {
  67. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoul, 10));
  68. } else if constexpr (std::is_same_v<Out, long long>) {
  69. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoll, 10));
  70. } else if constexpr (std::is_same_v<Out, unsigned long long>) {
  71. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoull, 10));
  72. } else if constexpr (std::is_same_v<Out, float>) {
  73. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtof));
  74. } else if constexpr (std::is_same_v<Out, double>) {
  75. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtod));
  76. } else if constexpr (std::is_same_v<Out, long double>) {
  77. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtold));
  78. } else if constexpr (std::is_same_v<Out, bool>) {
  79. return detail::cast_bool(out, str);
  80. } else if constexpr (std::is_same_v<Out, char>) {
  81. out = str[0];
  82. return str.size() == 1;
  83. } else if constexpr (std::is_constructible_v<Out, std::string_view>) {
  84. out = Out(str);
  85. return true;
  86. #if __has_include(<magic_enum/magic_enum.hpp>)
  87. } else if constexpr (std::is_enum_v<Out>) {
  88. std::optional<Out> result;
  89. if constexpr (magic_enum::detail::subtype_v<Out> ==
  90. magic_enum::detail::enum_subtype::flags) {
  91. result = magic_enum::enum_flags_cast<Out>(str);
  92. } else {
  93. result = magic_enum::enum_cast<Out>(str);
  94. }
  95. out = result.value_or(out);
  96. return result.has_value();
  97. #endif
  98. } else if constexpr (std::is_integral_v<Out>) {
  99. using V = std::conditional_t<std::is_unsigned_v<Out>, unsigned long, long>;
  100. auto [tmp, success] = cast<V>(str);
  101. out = static_cast<Out>(tmp);
  102. return success && tmp == static_cast<V>(out);
  103. } else {
  104. static_assert(detail::always_false<Out>{}, "No match for cast(string)");
  105. }
  106. }
  107. template <typename... Ts>
  108. bool cast(std::variant<Ts...> & out, std::string_view str) noexcept {
  109. return (detail::ctor_cast<Ts>(out, str) || ...);
  110. }
  111. template <typename Out>
  112. bool cast(std::optional<Out> & out, std::string_view str) noexcept {
  113. return detail::ctor_cast<Out>(out, str) || true;
  114. }
  115. template <typename Out>
  116. bool cast(Out & out, std::vector<std::string_view> const & strs) noexcept {
  117. if constexpr (detail::is_associative_v<Out>) {
  118. for (auto elem : strs) {
  119. auto [tmp, success] =
  120. cast<typename Out::value_type>(detail::keyval(elem));
  121. if (!success) { return false; }
  122. out.insert(std::move(tmp));
  123. }
  124. return true;
  125. } else if constexpr (detail::is_container_v<Out>) {
  126. for (auto elem : strs) {
  127. auto [tmp, success] = cast<typename Out::value_type>(elem);
  128. if (!success) { return false; }
  129. out.insert(out.end(), std::move(tmp));
  130. }
  131. return true;
  132. } else if constexpr (detail::is_tuple_v<Out>) {
  133. constexpr size_t N = std::tuple_size_v<Out>;
  134. return strs.size() == N &&
  135. detail::cast_tuple(strs, out, std::make_index_sequence<N>{});
  136. } else {
  137. static_assert(detail::always_false<Out>{},
  138. "No match for cast(vector<string>)");
  139. }
  140. }
  141. template <typename Out, typename S>
  142. bool cast(Out & out, std::vector<S> const & strs) noexcept {
  143. return cast(out, std::vector<std::string_view>(strs.begin(), strs.end()));
  144. }
  145. template <typename T> std::pair<T, bool> cast(std::string_view str) noexcept {
  146. std::pair<detail::decay_t<T>, bool> rval;
  147. using ::string_utils::cast;
  148. rval.second = cast(rval.first, str);
  149. return rval;
  150. }
  151. template <typename T, typename S>
  152. std::pair<T, bool> cast(std::vector<S> const & strs) noexcept {
  153. std::vector<std::string_view> tmp(strs.begin(), strs.end());
  154. std::pair<detail::decay_t<T>, bool> rval;
  155. using ::string_utils::cast;
  156. rval.second = cast(rval.first, tmp);
  157. return rval;
  158. }
  159. }
  160. #undef CAST_NUMBER_IMPL