mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-14 06:48:39 -04:00
feat(string): add support for converting split_to
This commit is contained in:
parent
88371340d9
commit
4a7a993d95
@ -77,7 +77,11 @@ std::optional<T> tryTo(U&& s)
|
|||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
T to(U&& s) {
|
T to(U&& s) {
|
||||||
|
if constexpr (std::same_as<T, std::decay_t<U>>) {
|
||||||
|
return std::forward<U>(s);
|
||||||
|
} else {
|
||||||
return tryTo<T>(std::forward<U>(s)).value();
|
return tryTo<T>(std::forward<U>(s)).value();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <concepts>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@ -30,10 +31,14 @@
|
|||||||
#include <range/v3/view/map.hpp>
|
#include <range/v3/view/map.hpp>
|
||||||
#include <range/v3/view/split.hpp>
|
#include <range/v3/view/split.hpp>
|
||||||
|
|
||||||
|
#include <dwarfs/conv.h>
|
||||||
|
|
||||||
namespace dwarfs {
|
namespace dwarfs {
|
||||||
|
|
||||||
template <typename T, typename Input, typename Delim>
|
template <typename T, typename Input, typename Delim>
|
||||||
auto split_view(Input&& input, Delim&& delim) {
|
auto split_view(Input&& input, Delim&& delim)
|
||||||
|
requires(std::same_as<T, std::string> || std::same_as<T, std::string_view>)
|
||||||
|
{
|
||||||
return std::forward<Input>(input) |
|
return std::forward<Input>(input) |
|
||||||
ranges::views::split(std::forward<Delim>(delim)) |
|
ranges::views::split(std::forward<Delim>(delim)) |
|
||||||
ranges::views::transform([](auto&& rng) {
|
ranges::views::transform([](auto&& rng) {
|
||||||
@ -41,6 +46,17 @@ auto split_view(Input&& input, Delim&& delim) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Input, typename Delim>
|
||||||
|
auto split_view(Input&& input, Delim&& delim)
|
||||||
|
requires std::is_arithmetic_v<T>
|
||||||
|
{
|
||||||
|
return std::forward<Input>(input) |
|
||||||
|
ranges::views::split(std::forward<Delim>(delim)) |
|
||||||
|
ranges::views::transform([](auto&& rng) {
|
||||||
|
return to<T>(std::string_view(&*rng.begin(), ranges::distance(rng)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T, typename Delim>
|
template <typename T, typename Delim>
|
||||||
auto split_view(char const* input, Delim&& delim) {
|
auto split_view(char const* input, Delim&& delim) {
|
||||||
return split_view<T>(std::string_view(input), std::forward<Delim>(delim));
|
return split_view<T>(std::string_view(input), std::forward<Delim>(delim));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user