Skip to content

Commit ac79656

Browse files
committed
update single header
1 parent 9aaf1bc commit ac79656

File tree

2 files changed

+144
-8
lines changed

2 files changed

+144
-8
lines changed

single-header/ctre-unicode.hpp

+72-4
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Software.
239239
#include <utility>
240240
#include <cstddef>
241241
#include <string_view>
242+
#include <array>
242243
#include <cstdint>
243244

244245
namespace ctll {
@@ -271,11 +272,16 @@ constexpr length_value_t length_and_value_of_utf16_code_point(uint16_t first_uni
271272
else return {first_unit, 1};
272273
}
273274

275+
struct construct_from_pointer_t { };
276+
277+
constexpr auto construct_from_pointer = construct_from_pointer_t{};
278+
274279
template <size_t N> struct fixed_string {
275280
char32_t content[N] = {};
276281
size_t real_size{0};
277282
bool correct_flag{true};
278-
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept {
283+
284+
template <typename T> constexpr fixed_string(construct_from_pointer_t, const T * input) noexcept {
279285
if constexpr (std::is_same_v<T, char>) {
280286
#ifdef CTRE_STRING_IS_UTF8
281287
size_t out{0};
@@ -373,6 +379,10 @@ template <size_t N> struct fixed_string {
373379
}
374380
}
375381
}
382+
383+
template <typename T> constexpr fixed_string(const std::array<T, N> & in) noexcept: fixed_string{construct_from_pointer, in.data()} { }
384+
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept: fixed_string{construct_from_pointer, input} { }
385+
376386
constexpr fixed_string(const fixed_string & other) noexcept {
377387
for (size_t i{0}; i < N; ++i) {
378388
content[i] = other.content[i];
@@ -440,6 +450,8 @@ template <> class fixed_string<0> {
440450
};
441451

442452
template <typename CharT, size_t N> fixed_string(const CharT (&)[N]) -> fixed_string<N-1>;
453+
template <typename CharT, size_t N> fixed_string(const std::array<CharT,N> &) -> fixed_string<N>;
454+
443455
template <size_t N> fixed_string(fixed_string<N>) -> fixed_string<N>;
444456

445457
}
@@ -3051,7 +3063,7 @@ struct utf8_iterator {
30513063

30523064
struct sentinel {
30533065
// this is here only because I want to support std::make_reverse_iterator
3054-
using self_type = utf8_iterator;
3066+
using self_type = sentinel;
30553067
using value_type = char8_t;
30563068
using reference = char8_t &;
30573069
using pointer = const char8_t *;
@@ -3069,6 +3081,20 @@ struct utf8_iterator {
30693081
friend constexpr auto operator==(self_type, const char8_t * other_ptr) noexcept {
30703082
return *other_ptr == char8_t{0};
30713083
}
3084+
3085+
friend constexpr auto operator!=(self_type, const char8_t * other_ptr) noexcept {
3086+
return *other_ptr != char8_t{0};
3087+
}
3088+
3089+
#if __cpp_impl_three_way_comparison < 201907L
3090+
friend constexpr auto operator==(const char8_t * other_ptr, self_type) noexcept {
3091+
return *other_ptr == char8_t{0};
3092+
}
3093+
3094+
friend constexpr auto operator!=(const char8_t * other_ptr, self_type) noexcept {
3095+
return *other_ptr != char8_t{0};
3096+
}
3097+
#endif
30723098
};
30733099

30743100
const char8_t * ptr{nullptr};
@@ -3078,8 +3104,8 @@ struct utf8_iterator {
30783104
return lhs.ptr < lhs.end;
30793105
}
30803106

3081-
constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
3082-
return rhs.ptr < rhs.end;
3107+
constexpr friend bool operator!=(const utf8_iterator & lhs, const char8_t * rhs) {
3108+
return lhs.ptr != rhs;
30833109
}
30843110

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

3119+
constexpr friend bool operator==(const utf8_iterator & lhs, const char8_t * rhs) {
3120+
return lhs.ptr == rhs;
3121+
}
3122+
3123+
constexpr friend bool operator==(const utf8_iterator & lhs, const utf8_iterator & rhs) {
3124+
return lhs.ptr == rhs.ptr;
3125+
}
3126+
3127+
#if __cpp_impl_three_way_comparison < 201907L
3128+
constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
3129+
return rhs.ptr < rhs.end;
3130+
}
3131+
3132+
constexpr friend bool operator!=(const char8_t * lhs, const utf8_iterator & rhs) {
3133+
return lhs == rhs.ptr;
3134+
}
3135+
30933136
constexpr friend bool operator==(sentinel, const utf8_iterator & rhs) {
30943137
return rhs.ptr >= rhs.end;
30953138
}
30963139

3140+
constexpr friend bool operator==(const char8_t * lhs, const utf8_iterator & rhs) {
3141+
return lhs == rhs.ptr;
3142+
}
3143+
#endif
3144+
3145+
30973146
constexpr utf8_iterator & operator=(const char8_t * rhs) {
30983147
ptr = rhs;
30993148
return *this;
@@ -3874,6 +3923,12 @@ constexpr auto first(ctll::list<Content...> l, ctll::list<sequence<Seq...>, Tail
38743923
return first(l, ctll::list<Seq..., Tail...>{});
38753924
}
38763925

3926+
// atomic group
3927+
template <typename... Content, typename... Seq, typename... Tail>
3928+
constexpr auto first(ctll::list<Content...> l, ctll::list<atomic_group<Seq...>, Tail...>) noexcept {
3929+
return first(l, ctll::list<Seq..., Tail...>{});
3930+
}
3931+
38773932
// plus
38783933
template <typename... Content, typename... Seq, typename... Tail>
38793934
constexpr auto first(ctll::list<Content...> l, ctll::list<plus<Seq...>, Tail...>) noexcept {
@@ -4858,6 +4913,19 @@ constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator curre
48584913
}
48594914
}
48604915

4916+
template <typename...> constexpr auto dependent_false = false;
4917+
4918+
// atomic (unsupported for now)
4919+
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename... Content, typename... Tail>
4920+
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 {
4921+
(void)begin;
4922+
(void)current;
4923+
(void)last;
4924+
(void)f;
4925+
(void)captures;
4926+
static_assert(dependent_false<Content...>, "Atomic groups are not supported (yet)");
4927+
}
4928+
48614929
// switching modes
48624930
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename Mode, typename... Tail>
48634931
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 {

single-header/ctre.hpp

+72-4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Software.
236236
#include <utility>
237237
#include <cstddef>
238238
#include <string_view>
239+
#include <array>
239240
#include <cstdint>
240241

241242
namespace ctll {
@@ -268,11 +269,16 @@ constexpr length_value_t length_and_value_of_utf16_code_point(uint16_t first_uni
268269
else return {first_unit, 1};
269270
}
270271

272+
struct construct_from_pointer_t { };
273+
274+
constexpr auto construct_from_pointer = construct_from_pointer_t{};
275+
271276
template <size_t N> struct fixed_string {
272277
char32_t content[N] = {};
273278
size_t real_size{0};
274279
bool correct_flag{true};
275-
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept {
280+
281+
template <typename T> constexpr fixed_string(construct_from_pointer_t, const T * input) noexcept {
276282
if constexpr (std::is_same_v<T, char>) {
277283
#ifdef CTRE_STRING_IS_UTF8
278284
size_t out{0};
@@ -370,6 +376,10 @@ template <size_t N> struct fixed_string {
370376
}
371377
}
372378
}
379+
380+
template <typename T> constexpr fixed_string(const std::array<T, N> & in) noexcept: fixed_string{construct_from_pointer, in.data()} { }
381+
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept: fixed_string{construct_from_pointer, input} { }
382+
373383
constexpr fixed_string(const fixed_string & other) noexcept {
374384
for (size_t i{0}; i < N; ++i) {
375385
content[i] = other.content[i];
@@ -437,6 +447,8 @@ template <> class fixed_string<0> {
437447
};
438448

439449
template <typename CharT, size_t N> fixed_string(const CharT (&)[N]) -> fixed_string<N-1>;
450+
template <typename CharT, size_t N> fixed_string(const std::array<CharT,N> &) -> fixed_string<N>;
451+
440452
template <size_t N> fixed_string(fixed_string<N>) -> fixed_string<N>;
441453

442454
}
@@ -3048,7 +3060,7 @@ struct utf8_iterator {
30483060

30493061
struct sentinel {
30503062
// this is here only because I want to support std::make_reverse_iterator
3051-
using self_type = utf8_iterator;
3063+
using self_type = sentinel;
30523064
using value_type = char8_t;
30533065
using reference = char8_t &;
30543066
using pointer = const char8_t *;
@@ -3066,6 +3078,20 @@ struct utf8_iterator {
30663078
friend constexpr auto operator==(self_type, const char8_t * other_ptr) noexcept {
30673079
return *other_ptr == char8_t{0};
30683080
}
3081+
3082+
friend constexpr auto operator!=(self_type, const char8_t * other_ptr) noexcept {
3083+
return *other_ptr != char8_t{0};
3084+
}
3085+
3086+
#if __cpp_impl_three_way_comparison < 201907L
3087+
friend constexpr auto operator==(const char8_t * other_ptr, self_type) noexcept {
3088+
return *other_ptr == char8_t{0};
3089+
}
3090+
3091+
friend constexpr auto operator!=(const char8_t * other_ptr, self_type) noexcept {
3092+
return *other_ptr != char8_t{0};
3093+
}
3094+
#endif
30693095
};
30703096

30713097
const char8_t * ptr{nullptr};
@@ -3075,8 +3101,8 @@ struct utf8_iterator {
30753101
return lhs.ptr < lhs.end;
30763102
}
30773103

3078-
constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
3079-
return rhs.ptr < rhs.end;
3104+
constexpr friend bool operator!=(const utf8_iterator & lhs, const char8_t * rhs) {
3105+
return lhs.ptr != rhs;
30803106
}
30813107

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

3116+
constexpr friend bool operator==(const utf8_iterator & lhs, const char8_t * rhs) {
3117+
return lhs.ptr == rhs;
3118+
}
3119+
3120+
constexpr friend bool operator==(const utf8_iterator & lhs, const utf8_iterator & rhs) {
3121+
return lhs.ptr == rhs.ptr;
3122+
}
3123+
3124+
#if __cpp_impl_three_way_comparison < 201907L
3125+
constexpr friend bool operator!=(sentinel, const utf8_iterator & rhs) {
3126+
return rhs.ptr < rhs.end;
3127+
}
3128+
3129+
constexpr friend bool operator!=(const char8_t * lhs, const utf8_iterator & rhs) {
3130+
return lhs == rhs.ptr;
3131+
}
3132+
30903133
constexpr friend bool operator==(sentinel, const utf8_iterator & rhs) {
30913134
return rhs.ptr >= rhs.end;
30923135
}
30933136

3137+
constexpr friend bool operator==(const char8_t * lhs, const utf8_iterator & rhs) {
3138+
return lhs == rhs.ptr;
3139+
}
3140+
#endif
3141+
3142+
30943143
constexpr utf8_iterator & operator=(const char8_t * rhs) {
30953144
ptr = rhs;
30963145
return *this;
@@ -3871,6 +3920,12 @@ constexpr auto first(ctll::list<Content...> l, ctll::list<sequence<Seq...>, Tail
38713920
return first(l, ctll::list<Seq..., Tail...>{});
38723921
}
38733922

3923+
// atomic group
3924+
template <typename... Content, typename... Seq, typename... Tail>
3925+
constexpr auto first(ctll::list<Content...> l, ctll::list<atomic_group<Seq...>, Tail...>) noexcept {
3926+
return first(l, ctll::list<Seq..., Tail...>{});
3927+
}
3928+
38743929
// plus
38753930
template <typename... Content, typename... Seq, typename... Tail>
38763931
constexpr auto first(ctll::list<Content...> l, ctll::list<plus<Seq...>, Tail...>) noexcept {
@@ -4855,6 +4910,19 @@ constexpr CTRE_FORCE_INLINE R evaluate(const BeginIterator begin, Iterator curre
48554910
}
48564911
}
48574912

4913+
template <typename...> constexpr auto dependent_false = false;
4914+
4915+
// atomic (unsupported for now)
4916+
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename... Content, typename... Tail>
4917+
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 {
4918+
(void)begin;
4919+
(void)current;
4920+
(void)last;
4921+
(void)f;
4922+
(void)captures;
4923+
static_assert(dependent_false<Content...>, "Atomic groups are not supported (yet)");
4924+
}
4925+
48584926
// switching modes
48594927
template <typename R, typename BeginIterator, typename Iterator, typename EndIterator, typename Mode, typename... Tail>
48604928
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 {

0 commit comments

Comments
 (0)