tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
char_traits.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_STRING_CHAR_TRAITS_HPP
4#define TETL_STRING_CHAR_TRAITS_HPP
5
10#include <etl/_ios/typedefs.hpp>
11#include <etl/_strings/cstr.hpp>
12
13namespace etl {
14
15namespace detail {
16
17template <typename CharType, typename IntType, IntType Eof>
18struct char_traits_base {
19 using char_type = CharType;
20 using int_type = IntType;
21 using off_type = etl::streamoff;
22 using comparison_category = etl::strong_ordering;
23
24 static constexpr auto assign(char_type& a, char_type const& b) noexcept -> void { a = b; }
25
26 static constexpr auto eq(char_type a, char_type b) noexcept -> bool { return a == b; }
27
28 static constexpr auto lt(char_type a, char_type b) noexcept -> bool { return a < b; }
29
30 static constexpr auto compare(char_type const* lhs, char_type const* rhs, size_t count) -> int
31 {
32 if (count == 0) {
33 return 0;
34 }
35
36 for (size_t i = 0; i < count; ++i) {
37 if (lhs[i] < rhs[i]) {
38 return -1;
39 }
40 if (lhs[i] > rhs[i]) {
41 return 1;
42 }
43 }
44
45 return 0;
46 }
47
48 static constexpr auto length(char_type const* str) -> size_t { return etl::detail::strlen<char_type, size_t>(str); }
49
50 static constexpr auto find(char_type const* str, size_t count, char_type const& token) -> char_type const*
51 {
52 for (size_t i = 0; i < count; ++i) {
53 if (str[i] == token) {
54 return &str[i];
55 }
56 }
57
58 return nullptr;
59 }
60
61 static constexpr auto move(char_type* dest, char_type const* source, size_t count) -> char_type*
62 {
63 for (size_t i = 0; i < count; ++i) {
64 dest[i] = source[i];
65 }
66 return dest;
67 }
68
69 static constexpr auto copy(char_type* dest, char_type const* source, size_t count) -> char_type*
70 {
71 for (size_t i = 0; i < count; ++i) {
72 assign(dest[i], source[i]);
73 }
74 return dest;
75 }
76
77 static constexpr auto assign(char_type* str, size_t count, char_type token) -> char_type*
78 {
79 for (size_t i = 0; i < count; ++i) {
80 assign(str[i], token);
81 }
82 return str;
83 }
84
85 static constexpr auto to_char_type(int_type c) noexcept -> char_type { return static_cast<char_type>(c); }
86
87 static constexpr auto to_int_type(char_type c) noexcept -> int_type { return static_cast<int_type>(c); }
88
89 static constexpr auto eq_int_type(int_type lhs, int_type rhs) noexcept -> bool
90 {
91 if (lhs == rhs) {
92 return true;
93 }
94 if (lhs == eof() and rhs == eof()) {
95 return true;
96 }
97 if (lhs == eof() or rhs == eof()) {
98 return false;
99 }
100 return false;
101 }
102
103 static constexpr auto eof() noexcept -> int_type { return Eof; }
104
105 static constexpr auto not_eof(int_type c) noexcept -> int_type { return !eq_int_type(c, eof()) ? c : 0; }
106};
107
108} // namespace detail
109
124template <typename CharT>
126
129template <>
130struct char_traits<char> : detail::char_traits_base<char, int, -1> { };
131
134template <>
135struct char_traits<wchar_t> : detail::char_traits_base<wchar_t, wint_t, static_cast<wint_t>(WEOF)> { };
136
139template <>
140struct char_traits<char8_t> : detail::char_traits_base<char8_t, unsigned, static_cast<unsigned>(-1)> { };
141
144template <>
145struct char_traits<char16_t> : detail::char_traits_base<char16_t, uint_least16_t, uint_least16_t(0xFFFF)> { };
146
149template <>
150struct char_traits<char32_t> : detail::char_traits_base<char32_t, uint_least32_t, uint_least32_t(0xFFFFFFFF)> { };
151
152} // namespace etl
153
154#endif // TETL_STRING_CHAR_TRAITS_HPP
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21
Definition adjacent_find.hpp:8
long streamoff
Definition typedefs.hpp:11
The char_traits class is a traits class template that abstracts basic character and string operations...
Definition char_traits.hpp:125