Skip to content

Commit

Permalink
update single header
Browse files Browse the repository at this point in the history
  • Loading branch information
hanickadot committed Jul 27, 2023
1 parent 9aaf1bc commit ac79656
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 8 deletions.
76 changes: 72 additions & 4 deletions single-header/ctre-unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ Software.
#include <utility>
#include <cstddef>
#include <string_view>
#include <array>
#include <cstdint>

namespace ctll {
Expand Down Expand Up @@ -271,11 +272,16 @@ constexpr length_value_t length_and_value_of_utf16_code_point(uint16_t first_uni
else return {first_unit, 1};
}

struct construct_from_pointer_t { };

constexpr auto construct_from_pointer = construct_from_pointer_t{};

template <size_t N> struct fixed_string {
char32_t content[N] = {};
size_t real_size{0};
bool correct_flag{true};
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept {

template <typename T> constexpr fixed_string(construct_from_pointer_t, const T * input) noexcept {
if constexpr (std::is_same_v<T, char>) {
#ifdef CTRE_STRING_IS_UTF8
size_t out{0};
Expand Down Expand Up @@ -373,6 +379,10 @@ template <size_t N> struct fixed_string {
}
}
}

template <typename T> constexpr fixed_string(const std::array<T, N> & in) noexcept: fixed_string{construct_from_pointer, in.data()} { }
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept: fixed_string{construct_from_pointer, input} { }

constexpr fixed_string(const fixed_string & other) noexcept {
for (size_t i{0}; i < N; ++i) {
content[i] = other.content[i];
Expand Down Expand Up @@ -440,6 +450,8 @@ template <> class fixed_string<0> {
};

template <typename CharT, size_t N> fixed_string(const CharT (&)[N]) -> fixed_string<N-1>;
template <typename CharT, size_t N> fixed_string(const std::array<CharT,N> &) -> fixed_string<N>;

template <size_t N> fixed_string(fixed_string<N>) -> fixed_string<N>;

}
Expand Down Expand Up @@ -3051,7 +3063,7 @@ struct utf8_iterator {

struct sentinel {
// this is here only because I want to support std::make_reverse_iterator
using self_type = utf8_iterator;
using self_type = sentinel;
using value_type = char8_t;
using reference = char8_t &;
using pointer = const char8_t *;
Expand All @@ -3069,6 +3081,20 @@ struct utf8_iterator {
friend constexpr auto operator==(self_type, const char8_t * other_ptr) noexcept {
return *other_ptr == char8_t{0};
}

friend constexpr auto operator!=(self_type, const char8_t * other_ptr) noexcept {
return *other_ptr != char8_t{0};
}

#if __cpp_impl_three_way_comparison < 201907L
friend constexpr auto operator==(const char8_t * other_ptr, self_type) noexcept {
return *other_ptr == char8_t{0};
}

friend constexpr auto operator!=(const char8_t * other_ptr, self_type) noexcept {
return *other_ptr != char8_t{0};
}
#endif
};

const char8_t * ptr{nullptr};
Expand All @@ -3078,8 +3104,8 @@ struct utf8_iterator {
return lhs.ptr < lhs.end;
}

constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
return rhs.ptr < rhs.end;
constexpr friend bool operator!=(const utf8_iterator & lhs, const char8_t * rhs) {
return lhs.ptr != rhs;
}

constexpr friend bool operator!=(const utf8_iterator & lhs, const utf8_iterator & rhs) {
Expand All @@ -3090,10 +3116,33 @@ struct utf8_iterator {
return lhs.ptr >= lhs.end;
}

constexpr friend bool operator==(const utf8_iterator & lhs, const char8_t * rhs) {
return lhs.ptr == rhs;
}

constexpr friend bool operator==(const utf8_iterator & lhs, const utf8_iterator & rhs) {
return lhs.ptr == rhs.ptr;
}

#if __cpp_impl_three_way_comparison < 201907L
constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
return rhs.ptr < rhs.end;
}

constexpr friend bool operator!=(const char8_t * lhs, const utf8_iterator & rhs) {
return lhs == rhs.ptr;
}

constexpr friend bool operator==(sentinel, const utf8_iterator & rhs) {
return rhs.ptr >= rhs.end;
}

constexpr friend bool operator==(const char8_t * lhs, const utf8_iterator & rhs) {
return lhs == rhs.ptr;
}
#endif


constexpr utf8_iterator & operator=(const char8_t * rhs) {
ptr = rhs;
return *this;
Expand Down Expand Up @@ -3874,6 +3923,12 @@ constexpr auto first(ctll::list<Content...> l, ctll::list<sequence<Seq...>, Tail
return first(l, ctll::list<Seq..., Tail...>{});
}

// atomic group
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...> l, ctll::list<atomic_group<Seq...>, Tail...>) noexcept {
return first(l, ctll::list<Seq..., Tail...>{});
}

// plus
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...> l, ctll::list<plus<Seq...>, Tail...>) noexcept {
Expand Down Expand Up @@ -4858,6 +4913,19 @@ constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator curre
}
}

template <typename...> constexpr auto dependent_false = false;

// atomic (unsupported for now)
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename... Content, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<atomic_group<Content...>, Tail...>) noexcept {
(void)begin;
(void)current;
(void)last;
(void)f;
(void)captures;
static_assert(dependent_false<Content...>, "Atomic groups are not supported (yet)");
}

// switching modes
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename Mode, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<mode_switch<Mode>, Tail...>) noexcept {
Expand Down
76 changes: 72 additions & 4 deletions single-header/ctre.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Software.
#include <utility>
#include <cstddef>
#include <string_view>
#include <array>
#include <cstdint>

namespace ctll {
Expand Down Expand Up @@ -268,11 +269,16 @@ constexpr length_value_t length_and_value_of_utf16_code_point(uint16_t first_uni
else return {first_unit, 1};
}

struct construct_from_pointer_t { };

constexpr auto construct_from_pointer = construct_from_pointer_t{};

template <size_t N> struct fixed_string {
char32_t content[N] = {};
size_t real_size{0};
bool correct_flag{true};
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept {

template <typename T> constexpr fixed_string(construct_from_pointer_t, const T * input) noexcept {
if constexpr (std::is_same_v<T, char>) {
#ifdef CTRE_STRING_IS_UTF8
size_t out{0};
Expand Down Expand Up @@ -370,6 +376,10 @@ template <size_t N> struct fixed_string {
}
}
}

template <typename T> constexpr fixed_string(const std::array<T, N> & in) noexcept: fixed_string{construct_from_pointer, in.data()} { }
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept: fixed_string{construct_from_pointer, input} { }

constexpr fixed_string(const fixed_string & other) noexcept {
for (size_t i{0}; i < N; ++i) {
content[i] = other.content[i];
Expand Down Expand Up @@ -437,6 +447,8 @@ template <> class fixed_string<0> {
};

template <typename CharT, size_t N> fixed_string(const CharT (&)[N]) -> fixed_string<N-1>;
template <typename CharT, size_t N> fixed_string(const std::array<CharT,N> &) -> fixed_string<N>;

template <size_t N> fixed_string(fixed_string<N>) -> fixed_string<N>;

}
Expand Down Expand Up @@ -3048,7 +3060,7 @@ struct utf8_iterator {

struct sentinel {
// this is here only because I want to support std::make_reverse_iterator
using self_type = utf8_iterator;
using self_type = sentinel;
using value_type = char8_t;
using reference = char8_t &;
using pointer = const char8_t *;
Expand All @@ -3066,6 +3078,20 @@ struct utf8_iterator {
friend constexpr auto operator==(self_type, const char8_t * other_ptr) noexcept {
return *other_ptr == char8_t{0};
}

friend constexpr auto operator!=(self_type, const char8_t * other_ptr) noexcept {
return *other_ptr != char8_t{0};
}

#if __cpp_impl_three_way_comparison < 201907L
friend constexpr auto operator==(const char8_t * other_ptr, self_type) noexcept {
return *other_ptr == char8_t{0};
}

friend constexpr auto operator!=(const char8_t * other_ptr, self_type) noexcept {
return *other_ptr != char8_t{0};
}
#endif
};

const char8_t * ptr{nullptr};
Expand All @@ -3075,8 +3101,8 @@ struct utf8_iterator {
return lhs.ptr < lhs.end;
}

constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
return rhs.ptr < rhs.end;
constexpr friend bool operator!=(const utf8_iterator & lhs, const char8_t * rhs) {
return lhs.ptr != rhs;
}

constexpr friend bool operator!=(const utf8_iterator & lhs, const utf8_iterator & rhs) {
Expand All @@ -3087,10 +3113,33 @@ struct utf8_iterator {
return lhs.ptr >= lhs.end;
}

constexpr friend bool operator==(const utf8_iterator & lhs, const char8_t * rhs) {
return lhs.ptr == rhs;
}

constexpr friend bool operator==(const utf8_iterator & lhs, const utf8_iterator & rhs) {
return lhs.ptr == rhs.ptr;
}

#if __cpp_impl_three_way_comparison < 201907L
constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
return rhs.ptr < rhs.end;
}

constexpr friend bool operator!=(const char8_t * lhs, const utf8_iterator & rhs) {
return lhs == rhs.ptr;
}

constexpr friend bool operator==(sentinel, const utf8_iterator & rhs) {
return rhs.ptr >= rhs.end;
}

constexpr friend bool operator==(const char8_t * lhs, const utf8_iterator & rhs) {
return lhs == rhs.ptr;
}
#endif


constexpr utf8_iterator & operator=(const char8_t * rhs) {
ptr = rhs;
return *this;
Expand Down Expand Up @@ -3871,6 +3920,12 @@ constexpr auto first(ctll::list<Content...> l, ctll::list<sequence<Seq...>, Tail
return first(l, ctll::list<Seq..., Tail...>{});
}

// atomic group
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...> l, ctll::list<atomic_group<Seq...>, Tail...>) noexcept {
return first(l, ctll::list<Seq..., Tail...>{});
}

// plus
template <typename... Content, typename... Seq, typename... Tail>
constexpr auto first(ctll::list<Content...> l, ctll::list<plus<Seq...>, Tail...>) noexcept {
Expand Down Expand Up @@ -4855,6 +4910,19 @@ constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator curre
}
}

template <typename...> constexpr auto dependent_false = false;

// atomic (unsupported for now)
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename... Content, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<atomic_group<Content...>, Tail...>) noexcept {
(void)begin;
(void)current;
(void)last;
(void)f;
(void)captures;
static_assert(dependent_false<Content...>, "Atomic groups are not supported (yet)");
}

// switching modes
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename Mode, typename... Tail>
constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator current, const EndIterator last, const flags & f, R captures, ctll::list<mode_switch<Mode>, Tail...>) noexcept {
Expand Down

0 comments on commit ac79656

Please sign in to comment.