cast.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. std::vector<std::string_view> keyval(std::string_view input) {
  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. }
  47. // This should be placed last in the file
  48. namespace string_utils {
  49. template <typename Out> bool cast(Out & out, std::string_view str) noexcept {
  50. if constexpr (std::is_same_v<Out, long>) {
  51. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtol, 10));
  52. } else if constexpr (std::is_same_v<Out, long>) {
  53. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtol, 10));
  54. } else if constexpr (std::is_same_v<Out, long long>) {
  55. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtoll, 10));
  56. } else if constexpr (std::is_same_v<Out, float>) {
  57. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtof));
  58. } else if constexpr (std::is_same_v<Out, double>) {
  59. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtod));
  60. } else if constexpr (std::is_same_v<Out, long double>) {
  61. return detail::cast_number(str, out, SAFE_NUMBER_PARSE(std::strtold));
  62. } else if constexpr (std::is_same_v<Out, int>) {
  63. auto [tmp, success] = cast<long>(str);
  64. out = static_cast<int>(tmp);
  65. return success && tmp == static_cast<long>(out);
  66. } else if constexpr (std::is_same_v<Out, bool>) {
  67. if (any_of(str, "true", "TRUE", "YES", "1")) {
  68. out = true;
  69. return true;
  70. } else if (any_of(str, "false", "FALSE", "NO", "0")) {
  71. out = false;
  72. return true;
  73. }
  74. return false;
  75. } else if constexpr (std::is_constructible_v<Out, std::string_view>) {
  76. out = Out(str);
  77. return true;
  78. } else {
  79. static_assert(detail::always_false<Out>{}, "No match for cast(string)");
  80. }
  81. }
  82. template <typename... Ts>
  83. bool cast(std::variant<Ts...> & out, std::string_view str) noexcept {
  84. return (detail::ctor_cast<Ts>(out, str) || ...);
  85. }
  86. template <typename Out>
  87. bool cast(std::optional<Out> & out, std::string_view str) noexcept {
  88. return detail::ctor_cast<Out>(out, str) || true;
  89. }
  90. template <typename Out>
  91. bool cast(Out & out, std::vector<std::string_view> const & strs) noexcept {
  92. if constexpr (detail::is_associative_v<Out>) {
  93. for (auto elem : strs) {
  94. auto [tmp, success] =
  95. cast<typename Out::value_type>(detail::keyval(elem));
  96. if (!success) { return false; }
  97. out.insert(std::move(tmp));
  98. }
  99. return true;
  100. } else if constexpr (detail::is_container_v<Out>) {
  101. for (auto elem : strs) {
  102. auto [tmp, success] = cast<typename Out::value_type>(elem);
  103. if (!success) { return false; }
  104. out.insert(out.end(), std::move(tmp));
  105. }
  106. return true;
  107. } else if constexpr (detail::is_tuple_v<Out>) {
  108. constexpr size_t N = std::tuple_size_v<Out>;
  109. return strs.size() == N &&
  110. detail::cast_tuple(strs, out, std::make_index_sequence<N>{});
  111. } else {
  112. static_assert(detail::always_false<Out>{},
  113. "No match for cast(vector<string>)");
  114. }
  115. }
  116. template <typename T> std::pair<T, bool> cast(std::string_view str) noexcept {
  117. std::pair<detail::decay_t<T>, bool> rval;
  118. using ::string_utils::cast;
  119. rval.second = cast(rval.first, str);
  120. return rval;
  121. }
  122. template <typename T, typename S>
  123. std::pair<T, bool> cast(std::vector<S> const & strs) noexcept {
  124. std::vector<std::string_view> tmp{strs.begin(), strs.end()};
  125. std::pair<detail::decay_t<T>, bool> rval;
  126. using ::string_utils::cast;
  127. rval.second = cast(rval.first, tmp);
  128. return rval;
  129. }
  130. }
  131. #undef CAST_NUMBER_IMPL