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// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_STRING_CHAR_TRAITS_HPP
5#define TETL_STRING_CHAR_TRAITS_HPP
6
7#include <etl/_compare/strong_ordering.hpp>
8#include <etl/_cstddef/size_t.hpp>
9#include <etl/_cstdint/uint_least_t.hpp>
10#include <etl/_cwchar/wint_t.hpp>
11#include <etl/_ios/typedefs.hpp>
12#include <etl/_strings/cstr.hpp>
13
14namespace etl {
15
16namespace detail {
17
18template <typename CharType, typename IntType, IntType Eof>
19struct char_traits_base {
20 using char_type = CharType;
21 using int_type = IntType;
22 using off_type = etl::streamoff;
23 using comparison_category = etl::strong_ordering;
24
25 static constexpr auto assign(char_type& a, char_type const& b) noexcept -> void
26 {
27 a = b;
28 }
29
30 static constexpr auto eq(char_type a, char_type b) noexcept -> bool
31 {
32 return a == b;
33 }
34
35 static constexpr auto lt(char_type a, char_type b) noexcept -> bool
36 {
37 return a < b;
38 }
39
40 static constexpr auto compare(char_type const* lhs, char_type const* rhs, size_t count) -> int
41 {
42 if (count == 0) {
43 return 0;
44 }
45
46 for (size_t i = 0; i < count; ++i) {
47 if (lhs[i] < rhs[i]) {
48 return -1;
49 }
50 if (lhs[i] > rhs[i]) {
51 return 1;
52 }
53 }
54
55 return 0;
56 }
57
58 static constexpr auto length(char_type const* str) -> size_t
59 {
60 return etl::detail::strlen<char_type, size_t>(str);
61 }
62
63 static constexpr auto find(char_type const* str, size_t count, char_type const& token) -> char_type const*
64 {
65 for (size_t i = 0; i < count; ++i) {
66 if (str[i] == token) {
67 return &str[i];
68 }
69 }
70
71 return nullptr;
72 }
73
74 static constexpr auto move(char_type* dest, char_type const* source, size_t count) -> char_type*
75 {
76 for (size_t i = 0; i < count; ++i) {
77 dest[i] = source[i];
78 }
79 return dest;
80 }
81
82 static constexpr auto copy(char_type* dest, char_type const* source, size_t count) -> char_type*
83 {
84 for (size_t i = 0; i < count; ++i) {
85 assign(dest[i], source[i]);
86 }
87 return dest;
88 }
89
90 static constexpr auto assign(char_type* str, size_t count, char_type token) -> char_type*
91 {
92 for (size_t i = 0; i < count; ++i) {
93 assign(str[i], token);
94 }
95 return str;
96 }
97
98 static constexpr auto to_char_type(int_type c) noexcept -> char_type
99 {
100 return static_cast<char_type>(c);
101 }
102
103 static constexpr auto to_int_type(char_type c) noexcept -> int_type
104 {
105 return static_cast<int_type>(c);
106 }
107
108 static constexpr auto eq_int_type(int_type lhs, int_type rhs) noexcept -> bool
109 {
110 if (lhs == rhs) {
111 return true;
112 }
113 if (lhs == eof() and rhs == eof()) {
114 return true;
115 }
116 if (lhs == eof() or rhs == eof()) {
117 return false;
118 }
119 return false;
120 }
121
122 static constexpr auto eof() noexcept -> int_type
123 {
124 return Eof;
125 }
126
127 static constexpr auto not_eof(int_type c) noexcept -> int_type
128 {
129 return !eq_int_type(c, eof()) ? c : 0;
130 }
131};
132
133} // namespace detail
134
135/// The char_traits class is a traits class template that abstracts basic
136/// character and string operations for a given character type.
137///
138/// The defined operation set is such that generic algorithms almost always can be
139/// implemented in terms of it. It is thus possible to use such algorithms with
140/// almost any possible character or string type, just by supplying a customized
141/// char_traits class. The char_traits class template serves as a basis for
142/// explicit instantiations. The user can provide a specialization for any
143/// custom character types. Several specializations are defined for the standard
144/// character types. If an operation on traits emits an exception, the behavior
145/// is undefined.
146///
147/// \headerfile etl/string.hpp
148/// \ingroup string
149template <typename CharT>
150struct char_traits;
151
152/// Specializations of char_traits for type char.
153/// \ingroup string
154template <>
155struct char_traits<char> : detail::char_traits_base<char, int, -1> { };
156
157/// Specializations of char_traits for type wchar_t.
158/// \ingroup string
159template <>
160struct char_traits<wchar_t> : detail::char_traits_base<wchar_t, wint_t, static_cast<wint_t>(WEOF)> { };
161
162/// Specializations of char_traits for type char8_t.
163/// \ingroup string
164template <>
165struct char_traits<char8_t> : detail::char_traits_base<char8_t, unsigned, static_cast<unsigned>(-1)> { };
166
167/// Specializations of char_traits for type char16_t.
168/// \ingroup string
169template <>
170struct char_traits<char16_t> : detail::char_traits_base<char16_t, uint_least16_t, uint_least16_t(0xFFFF)> { };
171
172/// Specializations of char_traits for type char32_t.
173/// \ingroup string
174template <>
175struct char_traits<char32_t> : detail::char_traits_base<char32_t, uint_least32_t, uint_least32_t(0xFFFFFFFF)> { };
176
177} // namespace etl
178
179#endif // TETL_STRING_CHAR_TRAITS_HPP
Definition adjacent_find.hpp:9
Definition strong_ordering.hpp:15