mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-13 14:27:30 -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>
|
||||
T to(U&& s) {
|
||||
return tryTo<T>(std::forward<U>(s)).value();
|
||||
if constexpr (std::same_as<T, std::decay_t<U>>) {
|
||||
return std::forward<U>(s);
|
||||
} else {
|
||||
return tryTo<T>(std::forward<U>(s)).value();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dwarfs
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
@ -30,10 +31,14 @@
|
||||
#include <range/v3/view/map.hpp>
|
||||
#include <range/v3/view/split.hpp>
|
||||
|
||||
#include <dwarfs/conv.h>
|
||||
|
||||
namespace dwarfs {
|
||||
|
||||
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) |
|
||||
ranges::views::split(std::forward<Delim>(delim)) |
|
||||
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>
|
||||
auto split_view(char const* input, Delim&& delim) {
|
||||
return split_view<T>(std::string_view(input), std::forward<Delim>(delim));
|
||||
|
Loading…
x
Reference in New Issue
Block a user