Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions include/iris/x4/char/char_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
==============================================================================*/

#include <iris/x4/char/char_parser.hpp>
#include <iris/x4/char/detail/cast_char.hpp>
#include <iris/x4/char/detail/basic_chset.hpp>
#include <iris/x4/string/case_compare.hpp>
#include <iris/x4/core/char_traits.hpp>

#include <iris/unicode/string.hpp>

#include <string_view>
#include <format>
#include <ranges>
#include <type_traits>
Expand Down Expand Up @@ -72,36 +73,29 @@ struct char_set : char_parser<Encoding, char_set<Encoding, Attr>>

static constexpr bool has_attribute = !std::is_same_v<unused_type, attribute_type>;

template<std::ranges::forward_range R>
constexpr explicit char_set(R const& str)
noexcept(detail::cast_char_noexcept<std::ranges::range_value_t<R>, char_type>)
constexpr explicit char_set(std::basic_string_view<char_type> const str)
// never noexcept; requires vector insertion
{
static_assert(detail::cast_char_viable<std::ranges::range_value_t<R>, char_type>);

using detail::cast_char; // ADL introduction

for (auto definition = std::ranges::begin(str); definition != std::ranges::end(str);) {
auto const ch = *definition;
auto next_definition = std::next(definition);
if (next_definition == std::ranges::end(str)) {
chset.set(cast_char<char_type>(ch));
chset_.set(ch);
break;
}

auto next_ch = *next_definition;
if (next_ch == '-') {
if (next_ch == detail::char_tokens<char_type>::hyphen) {
next_definition = std::next(next_definition);
if (next_definition == std::ranges::end(str)) {
chset.set(cast_char<char_type>(ch));
chset.set('-');
chset_.set(ch);
chset_.set(detail::char_tokens<char_type>::hyphen);
break;
}
chset.set(
cast_char<char_type>(ch),
cast_char<char_type>(*next_definition)
);
chset_.set(ch, *next_definition);

} else {
chset.set(cast_char<char_type>(ch));
chset_.set(ch);
}

definition = next_definition;
Expand All @@ -111,17 +105,18 @@ struct char_set : char_parser<Encoding, char_set<Encoding, Attr>>
template<class Char, class Context>
[[nodiscard]] constexpr bool test(Char ch_, Context const& ctx) const noexcept
{
static_assert(noexcept(x4::get_case_compare<encoding_type>(ctx).in_set(ch_, chset)));
return x4::get_case_compare<encoding_type>(ctx).in_set(ch_, chset);
static_assert(noexcept(x4::get_case_compare<encoding_type>(ctx).in_set(ch_, chset_)));
return x4::get_case_compare<encoding_type>(ctx).in_set(ch_, chset_);
}

detail::basic_chset<char_type> chset;

[[nodiscard]] std::string get_x4_info() const
{
// TODO: escape
return "char-set"; // TODO: make more user-friendly
}

private:
detail::basic_chset<char_type> chset_;
};

} // iris::x4
Expand Down
27 changes: 26 additions & 1 deletion include/iris/x4/core/char_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// This header is intended for inclusion by user-facing, non-parser headers.
// Do not add includes specific to X4's parser implementation here.

#include <iris/config.hpp>
#include <iris/config.hpp> // IWYU pragma: keep

#include <concepts>
#include <type_traits>
Expand Down Expand Up @@ -58,6 +58,31 @@ concept CharIncompatibleWith =
CharLike<T> &&
!std::same_as<T, ExpectedCharT>;

namespace detail {

template<class CharT>
struct char_tokens;

template<>
struct char_tokens<char>
{
static constexpr char hyphen = '-';
};

template<>
struct char_tokens<wchar_t>
{
static constexpr wchar_t hyphen = L'-';
};

template<>
struct char_tokens<char32_t>
{
static constexpr char32_t hyphen = U'-';
};

} // detail

} // iris::x4

#endif
Loading