cast.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <type_traits>
  13. #include <utility>
  14. #include <variant>
  15. #include <vector>
  16. #include "any_of.h"
  17. #define SAFE_NUMBER_PARSE(func, ...) \
  18. [](char const *in, char **out) { return func(in, out, ##__VA_ARGS__); }
  19. namespace string_utils {
  20. // A helper object for providing partial specializations for casting
  21. template <typename, typename = void> struct cast_helper {};
  22. // The main parser
  23. template <typename T> std::pair<T, bool> cast(std::string const & str) noexcept;
  24. template <typename T> bool cast(std::string const & str, T & to) noexcept;
  25. // A section of multi-argument parsers
  26. template <typename... Ts>
  27. bool cast(std::vector<std::string> const & str, std::tuple<Ts...> & to) noexcept;
  28. template <typename K, typename V>
  29. bool cast(std::vector<std::string> const & str, std::pair<K, V> & to) noexcept;
  30. }
  31. namespace string_utils::detail {
  32. template <typename, typename = void> struct has_cast_helper : std::false_type {};
  33. template <typename T>
  34. struct has_cast_helper<T, std::void_t<std::result_of_t<cast_helper<T>(std::string, T)>>>
  35. : std::true_type {};
  36. template <typename T> constexpr bool has_cast_helper_v = has_cast_helper<T>{};
  37. }
  38. namespace string_utils::detail {
  39. template <typename Tuple>
  40. bool cast_tuple(std::vector<std::string> const & str, Tuple & to) noexcept;
  41. template <typename T, typename F>
  42. bool cast_number(std::string const &str, T & to, F func) noexcept;
  43. }
  44. namespace string_utils {
  45. template <typename T>
  46. bool cast(std::string const & str, T & to) noexcept {
  47. if constexpr (detail::has_cast_helper_v<T>) {
  48. return cast_helper<T>{}(str, to);
  49. } else if constexpr (std::is_same_v<T, std::string>) {
  50. to = T(str);
  51. } else {
  52. static_assert(!detail::has_cast_helper_v<T>, "no cast available");
  53. }
  54. return true;
  55. }
  56. template <typename V, typename T> bool maybe_cast(std::string const & str, T & to) noexcept {
  57. auto [rval, found] = cast<V>(str);
  58. if (found) { to = std::move(rval); }
  59. return found;
  60. }
  61. template <typename... Ts>
  62. struct cast_helper<std::variant<Ts...>> {
  63. bool operator()(std::string const &str, std::variant<Ts...> & to) const noexcept {
  64. return (maybe_cast<Ts>(str, to) || ...);
  65. }
  66. };
  67. template <typename T>
  68. struct cast_helper<std::optional<T>> {
  69. bool operator()(std::string const &str, std::optional<T> & to) const noexcept {
  70. return maybe_cast<T>(str, to) || true;
  71. }
  72. };
  73. }
  74. namespace string_utils {
  75. inline bool cast(std::string const &str, long & to) noexcept {
  76. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtol, 10));
  77. }
  78. inline bool cast(std::string const &str, long long & to) noexcept {
  79. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtoll, 10));
  80. }
  81. inline bool cast(std::string const &str, float & to) noexcept {
  82. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtof));
  83. }
  84. inline bool cast(std::string const &str, double & to) noexcept {
  85. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtod));
  86. }
  87. inline bool cast(std::string const &str, long double & to) noexcept {
  88. return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtold));
  89. }
  90. inline bool cast(std::string const & str, int & to) noexcept {
  91. long tmp;
  92. bool rval = cast(str, tmp);
  93. to = static_cast<int>(tmp);
  94. return rval && tmp == static_cast<long>(to);
  95. }
  96. inline bool cast(std::string const & str, bool & to) noexcept {
  97. if (any_of(str, "true", "TRUE", "YES", "1")) {
  98. to = true;
  99. return true;
  100. } else if (any_of(str, "false", "FALSE", "NO", "0")) {
  101. to = false;
  102. return true;
  103. }
  104. return false;
  105. }
  106. }
  107. namespace string_utils {
  108. template <typename... Ts>
  109. bool cast(std::vector<std::string> const & str, std::tuple<Ts...> & to) noexcept {
  110. return detail::cast_tuple(str, to);
  111. }
  112. template <typename K, typename V>
  113. bool cast(std::vector<std::string> const & str, std::pair<K, V> & to) noexcept {
  114. return detail::cast_tuple(str, to);
  115. }
  116. }
  117. namespace string_utils::detail {
  118. template <typename Tuple, size_t... Is>
  119. bool cast_tuple(std::vector<std::string> const & str, Tuple & to,
  120. std::index_sequence<Is...>) {
  121. return ((cast(str[Is], std::get<Is>(to))) && ...);
  122. }
  123. template <typename Tuple>
  124. bool cast_tuple(std::vector<std::string> const & str, Tuple & to) noexcept {
  125. constexpr size_t N = std::tuple_size_v<Tuple>;
  126. return str.size() == N && cast_tuple(str, to, std::make_index_sequence<N>{});
  127. }
  128. template <typename T, typename F>
  129. bool cast_number(std::string const &str, T & to, F func) noexcept {
  130. char *counter = nullptr;
  131. to = func(str.c_str(), &counter);
  132. return counter == str.c_str() + str.length();
  133. }
  134. }
  135. // This should be placed last in the file
  136. namespace string_utils {
  137. template <typename T> std::pair<T, bool> cast(std::string const & str) noexcept {
  138. using string_utils::cast;
  139. std::pair<T, bool> rval;
  140. rval.second = cast(str, rval.first);
  141. return rval;
  142. }
  143. }
  144. #undef CAST_NUMBER_IMPL