I currently have this in my application; this needs to be X4's utility.
Related: #98
#ifndef XP_PARSER_STRING_HPP
#define XP_PARSER_STRING_HPP
#include "xp/config.hpp"
#include <iris/x4/core/action_context.hpp>
#include <iris/x4/attribute/as.hpp>
#include <iris/unicode/string.hpp>
#include <concepts>
#include <string>
namespace xp::parser {
namespace detail {
struct as_native_string_fn
{
[[nodiscard]] static constexpr auto operator()(auto const& parser)
{
return x4::as<std::string>(
x4::as<std::u32string>(parser).on_match([](auto&& ctx) {
x4::_as_var(ctx) = iris::unicode::transcode<char>(x4::_attr(ctx));
})
);
}
};
template<class ToCharT = char32_t, class FromCharT = char32_t>
struct as_string_fn
{
[[nodiscard]] static constexpr auto operator()(auto const& parser)
{
if constexpr (std::same_as<FromCharT, ToCharT>) {
return x4::as<std::basic_string<ToCharT>>(
parser
);
} else {
return x4::as<std::basic_string<ToCharT>>(
x4::as<std::basic_string<FromCharT>>(parser).on_match([](auto&& ctx) {
x4::_as_var(ctx) = iris::unicode::transcode<ToCharT>(x4::_attr(ctx));
})
);
}
}
};
} // detail
[[maybe_unused]] inline constexpr detail::as_native_string_fn as_native_string{};
template<class ToCharT = char32_t, class FromCharT = char32_t>
[[maybe_unused]] inline constexpr detail::as_string_fn<ToCharT, FromCharT> as_string{};
} // xp::parser
#endif
I currently have this in my application; this needs to be X4's utility.
Related: #98