| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // cast.h
- // string-utils
- //
- // Created by Sam Jaffe on 2/13/21.
- // Copyright © 2021 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <cstdlib>
- #include <optional>
- #include <string>
- #include <string_view>
- #include <type_traits>
- #include <utility>
- #include <variant>
- #include <vector>
- #include "string_utils/any_of.h"
- #include "string_utils/forwards.h"
- #include "string_utils/traits.h"
- #define SAFE_NUMBER_PARSE(func, ...) \
- [](char const *in, char **out) { return func(in, out, ##__VA_ARGS__); }
- namespace string_utils::detail {
- template <typename T, typename F>
- bool cast_number(std::string_view str, T & to, F func) noexcept;
- template <typename S> std::vector<S> keyval(S const &input);
- }
- namespace string_utils {
- inline bool cast(std::string_view str, std::string & to) noexcept {
- to = std::string(str);
- return true;
- }
- inline bool cast(std::string_view str, long & to) noexcept {
- return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtol, 10));
- }
- inline bool cast(std::string_view str, long long & to) noexcept {
- return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtoll, 10));
- }
- inline bool cast(std::string_view str, float & to) noexcept {
- return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtof));
- }
- inline bool cast(std::string_view str, double & to) noexcept {
- return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtod));
- }
- inline bool cast(std::string_view str, long double & to) noexcept {
- return detail::cast_number(str, to, SAFE_NUMBER_PARSE(std::strtold));
- }
- inline bool cast(std::string_view str, int & to) noexcept {
- auto [tmp, success] = cast<long>(str);
- to = static_cast<int>(tmp);
- return success && tmp == static_cast<long>(to);
- }
- inline bool cast(std::string_view str, bool & to) noexcept {
- if (any_of(str, "true", "TRUE", "YES", "1")) {
- to = true;
- return true;
- } else if (any_of(str, "false", "FALSE", "NO", "0")) {
- to = false;
- return true;
- }
- return false;
- }
- }
- namespace string_utils {
- template <typename V, typename S, typename T> bool maybe_cast(S const & str, T & to) noexcept {
- auto [rval, found] = cast<V>(str);
- if (found) { to = std::move(rval); }
- return found;
- }
- template <typename T> struct cast_helper<T, std::enable_if_t<detail::is_container<T>{}>> {
- template <typename S>
- bool operator()(std::vector<S> const &strs, T & to) const noexcept {
- for (S const &elem : strs) {
- if constexpr (detail::is_associative<T>{}) {
- auto [tmp, success] = cast<typename T::value_type>(detail::keyval(elem));
- if (!success) { return false; }
- to.insert(std::move(tmp));
- } else {
- auto [tmp, success] = cast<typename T::value_type>(elem);
- if (!success) { return false; }
- to.insert(to.end(), std::move(tmp));
- }
- }
- return true;
- }
- };
- template <typename T> struct cast_helper<T, std::enable_if_t<detail::is_tuple<T>{}>> {
- template <typename S, size_t... Is>
- bool cast_tuple(S const & str, T & to, std::index_sequence<Is...>) const noexcept {
- return ((cast(str[Is], std::get<Is>(to))) && ...);
- }
-
- template <typename S> bool operator()(std::vector<S> const &strs, T & to) const noexcept {
- constexpr size_t N = std::tuple_size_v<T>;
- return strs.size() == N && cast_tuple(strs, to, std::make_index_sequence<N>{});
- }
- };
- template <typename... Ts>
- struct cast_helper<std::variant<Ts...>> {
- bool operator()(std::string_view str, std::variant<Ts...> & to) const noexcept {
- return (maybe_cast<Ts>(str, to) || ...);
- }
- };
- template <typename T>
- struct cast_helper<std::optional<T>> {
- bool operator()(std::string_view str, std::optional<T> & to) const noexcept {
- return maybe_cast<T>(str, to) || true;
- }
- };
- }
- namespace string_utils::detail {
- template <typename Tuple, size_t... Is>
- bool cast_tuple(std::vector<std::string> const & str, Tuple & to,
- std::index_sequence<Is...>) noexcept {
- return ((cast(str[Is], std::get<Is>(to))) && ...);
- }
- template <typename T, typename F>
- bool cast_number(std::string_view str, T & to, F func) noexcept {
- char *counter = nullptr;
- to = func(str.data(), &counter);
- return counter == str.end();
- }
- template <typename S> std::vector<S> keyval(S const &input) {
- size_t const pos = input.find('=');
- return pos == S::npos ? std::vector{input}
- : std::vector{input.substr(0, pos), input.substr(pos + 1)};
- }
- }
- // This should be placed last in the file
- namespace string_utils {
- template <typename T> std::pair<T, bool> cast(std::string_view str) noexcept {
- std::pair<detail::decay_t<T>, bool> rval;
- rval.second = cast(str, rval.first);
- return rval;
- }
- template <typename T, typename S>
- std::pair<T, bool> cast(std::vector<S> const & strs) noexcept {
- std::pair<detail::decay_t<T>, bool> rval;
- rval.second = cast(strs, rval.first);
- return rval;
- }
- }
- #undef CAST_NUMBER_IMPL
|