| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //
- // 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 <type_traits>
- #include <utility>
- #include <variant>
- #include <vector>
- #include "any_of.h"
- #define CAST_NUMBER_IMPL(type, func, ...) \
- inline bool cast(std::string const & str, type & to) { \
- char * rval; \
- to = func(str.c_str(), &rval, ##__VA_ARGS__); \
- return rval == str.c_str() + str.length(); \
- }
- namespace string_utils { namespace traits {
- template <typename T, typename = void> struct is_stringy : std::false_type {};
- template <typename> struct is_variant : std::false_type {};
- template <typename... Ts>
- struct is_variant<std::variant<Ts...>> : std::true_type {};
- template <typename T>
- struct is_stringy<
- T, std::enable_if_t<std::is_constructible<T, std::string const &>{} &&
- !is_variant<T>{}>> : std::true_type {};
- }}
- namespace string_utils {
- template <typename T> std::pair<T, bool> cast(std::string const & str);
- template <typename T> bool cast(std::string const & str, std::optional<T> & to);
- template <typename... Ts>
- bool cast(std::string const & str, std::variant<Ts...> & to);
- template <typename... Ts>
- bool cast(std::vector<std::string> const & str, std::tuple<Ts...> & to);
- template <typename K, typename V>
- bool cast(std::vector<std::string> const & str, std::pair<K, V> & to);
- }
- namespace string_utils {
- template <typename T, typename = std::enable_if_t<traits::is_stringy<T>{}>>
- bool cast(std::string const & str, T & to) {
- to = T(str);
- return true;
- }
- CAST_NUMBER_IMPL(long, std::strtol, 10);
- CAST_NUMBER_IMPL(long long, std::strtoll, 10);
- CAST_NUMBER_IMPL(float, std::strtof);
- CAST_NUMBER_IMPL(double, std::strtod);
- CAST_NUMBER_IMPL(long double, std::strtold);
- inline bool cast(std::string const & str, int & to) {
- long tmp;
- bool rval = cast(str, tmp);
- to = static_cast<int>(tmp);
- return rval && tmp == static_cast<long>(to);
- }
- inline bool cast(std::string const & str, bool & to) {
- 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::detail {
- template <typename T, typename... Ts>
- bool cast_alternative(std::string const & str, std::variant<Ts...> & to) {
- auto [rval, found] = cast<T>(str);
- if (found) { to = std::move(rval); }
- return found;
- }
- template <typename Tuple, size_t... Is>
- bool cast_tuple(std::vector<std::string> const & str, Tuple & to,
- std::index_sequence<Is...>) {
- return ((cast(str[Is], std::get<Is>(to))) && ...);
- }
- template <typename Tuple>
- bool cast_tuple(std::vector<std::string> const & str, Tuple & to) {
- constexpr size_t N = std::tuple_size_v<Tuple>;
- return str.size() == N && cast_tuple(str, to, std::make_index_sequence<N>{});
- }
- }
- namespace string_utils {
- template <typename... Ts>
- bool cast(std::string const & str, std::variant<Ts...> & to) {
- return (detail::cast_alternative<Ts>(str, to) || ...);
- }
- template <typename T>
- bool cast(std::string const & str, std::optional<T> & to) {
- auto [value, success] = cast<T>(str);
- if (success) { to = std::move(value); }
- return true;
- }
- template <typename... Ts>
- bool cast(std::vector<std::string> const & str, std::tuple<Ts...> & to) {
- return detail::cast_tuple(str, to);
- }
- template <typename K, typename V>
- bool cast(std::vector<std::string> const & str, std::pair<K, V> & to) {
- return detail::cast_tuple(str, to);
- }
- }
- // This should be placed last in the file
- namespace string_utils {
- template <typename T> std::pair<T, bool> cast(std::string const & str) {
- using string_utils::cast;
- std::pair<T, bool> rval;
- rval.second = cast(str, rval.first);
- return rval;
- }
- }
- #undef CAST_NUMBER_IMPL
|