Skip to content

A utility that transcodes UTF string is needed #124

Description

@saki7

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

Activity

  1. saki7 commented on Oct 8, 2026

    @saki7
    MemberAuthor

    One important decision is whether this should be treated as X4's builtin semantic action or a dedicated parser class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions