Skip to content

Commit db1496f

Browse files
committed
implemented std::char_traits
1 parent d3fed74 commit db1496f

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed

src/libcxx/include/__char_traits

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
// -*- C++ -*-
2+
#ifndef _EZCXX_CHAR_TRAITS
3+
#define _EZCXX_CHAR_TRAITS
4+
5+
#include <cstddef>
6+
#include <cstdint>
7+
#include <cstring>
8+
#include <cstdio>
9+
#include <cwchar>
10+
11+
#pragma clang system_header
12+
13+
namespace std {
14+
15+
using streamoff = ptrdiff_t;
16+
using streamsize = ptrdiff_t;
17+
18+
} // namespace std
19+
20+
namespace std {
21+
22+
template<class CharT> struct char_traits;
23+
template<> struct char_traits<char>;
24+
template<> struct char_traits<char8_t>;
25+
template<> struct char_traits<char16_t>;
26+
template<> struct char_traits<char32_t>;
27+
template<> struct char_traits<wchar_t>;
28+
29+
template<> struct char_traits<char> {
30+
using char_type = char;
31+
using int_type = int;
32+
using off_type = streamoff;
33+
34+
static constexpr void assign(char_type& __c1, const char_type& __c2) noexcept {
35+
__c1 = __c2;
36+
}
37+
static constexpr bool eq(char_type __c1, char_type __c2) noexcept {
38+
return (__c1 == __c2);
39+
}
40+
static constexpr bool lt(char_type __c1, char_type __c2) noexcept {
41+
return (__c1 < __c2);
42+
}
43+
44+
static constexpr size_t length(const char_type* __s) {
45+
return __builtin_strlen(__s);
46+
}
47+
static constexpr int compare(const char_type* __s1, const char_type* __s2, size_t __n) {
48+
return __builtin_memcmp(__s1, __s2, __n);
49+
}
50+
static constexpr const char_type* find(const char_type* __s, size_t __n, const char_type& __a) {
51+
return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
52+
}
53+
static constexpr char_type* move(char_type* __s1, const char_type* __s2, size_t __n) {
54+
return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
55+
}
56+
static constexpr char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) {
57+
return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
58+
}
59+
static constexpr char_type* assign(char_type* __s, size_t __n, char_type __a) {
60+
return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
61+
}
62+
63+
static constexpr char_type to_char_type(int_type __c) noexcept {
64+
return static_cast<char_type>(__c);
65+
}
66+
static constexpr int_type to_int_type(char_type __c) noexcept {
67+
return static_cast<int_type>(__c);
68+
}
69+
static constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept {
70+
return (__c1 == __c2);
71+
}
72+
static constexpr int_type eof() noexcept {
73+
return EOF;
74+
}
75+
static constexpr int_type not_eof(int_type __c) noexcept {
76+
return (__c != eof()) ? __c : '\0';
77+
}
78+
};
79+
80+
template<> struct char_traits<char8_t> {
81+
using char_type = char8_t;
82+
using int_type = unsigned int;
83+
using off_type = streamoff;
84+
85+
static constexpr void assign(char_type& __c1, const char_type& __c2) noexcept {
86+
__c1 = __c2;
87+
}
88+
static constexpr bool eq(char_type __c1, char_type __c2) noexcept {
89+
return (__c1 == __c2);
90+
}
91+
static constexpr bool lt(char_type __c1, char_type __c2) noexcept {
92+
return (__c1 < __c2);
93+
}
94+
95+
static constexpr size_t length(const char_type* __s);
96+
static constexpr int compare(const char_type* __s1, const char_type* __s2, size_t __n);
97+
static constexpr const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
98+
static constexpr char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
99+
static constexpr char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
100+
static constexpr char_type* assign(char_type* __s, size_t __n, char_type __a);
101+
102+
static constexpr char_type to_char_type(int_type __c) noexcept;
103+
static constexpr int_type to_int_type(char_type __c) noexcept;
104+
static constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept;
105+
static constexpr int_type eof() noexcept;
106+
static constexpr int_type not_eof(int_type __c) noexcept;
107+
};
108+
109+
template<> struct char_traits<char16_t> {
110+
using char_type = char16_t;
111+
using int_type = uint_least16_t;
112+
using off_type = streamoff;
113+
114+
static constexpr void assign(char_type& __c1, const char_type& __c2) noexcept {
115+
__c1 = __c2;
116+
}
117+
static constexpr bool eq(char_type __c1, char_type __c2) noexcept {
118+
return (__c1 == __c2);
119+
}
120+
static constexpr bool lt(char_type __c1, char_type __c2) noexcept {
121+
return (__c1 < __c2);
122+
}
123+
124+
static constexpr size_t length(const char_type* __s);
125+
static constexpr int compare(const char_type* __s1, const char_type* __s2, size_t __n);
126+
static constexpr const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
127+
static constexpr char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
128+
static constexpr char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
129+
static constexpr char_type* assign(char_type* __s, size_t __n, char_type __a);
130+
131+
static constexpr char_type to_char_type(int_type __c) noexcept;
132+
static constexpr int_type to_int_type(char_type __c) noexcept;
133+
static constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept;
134+
static constexpr int_type eof() noexcept;
135+
static constexpr int_type not_eof(int_type __c) noexcept;
136+
};
137+
138+
template<> struct char_traits<char32_t> {
139+
using char_type = char32_t;
140+
using int_type = uint_least32_t;
141+
using off_type = streamoff;
142+
143+
static constexpr void assign(char_type& __c1, const char_type& __c2) noexcept {
144+
__c1 = __c2;
145+
}
146+
static constexpr bool eq(char_type __c1, char_type __c2) noexcept {
147+
return (__c1 == __c2);
148+
}
149+
static constexpr bool lt(char_type __c1, char_type __c2) noexcept {
150+
return (__c1 < __c2);
151+
}
152+
153+
static constexpr size_t length(const char_type* __s);
154+
static constexpr int compare(const char_type* __s1, const char_type* __s2, size_t __n);
155+
static constexpr const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
156+
static constexpr char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
157+
static constexpr char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
158+
static constexpr char_type* assign(char_type* __s, size_t __n, char_type __a);
159+
160+
static constexpr char_type to_char_type(int_type __c) noexcept;
161+
static constexpr int_type to_int_type(char_type __c) noexcept;
162+
static constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept;
163+
static constexpr int_type eof() noexcept;
164+
static constexpr int_type not_eof(int_type __c) noexcept;
165+
};
166+
167+
template<> struct char_traits<wchar_t> {
168+
using char_type = wchar_t;
169+
using int_type = wint_t;
170+
using off_type = streamoff;
171+
172+
static constexpr void assign(char_type& __c1, const char_type& __c2) noexcept {
173+
__c1 = __c2;
174+
}
175+
static constexpr bool eq(char_type __c1, char_type __c2) noexcept {
176+
return (__c1 == __c2);
177+
}
178+
static constexpr bool lt(char_type __c1, char_type __c2) noexcept {
179+
return (__c1 < __c2);
180+
}
181+
182+
static constexpr size_t length(const char_type* __s) {
183+
return __builtin_wcslen(__s);
184+
}
185+
static constexpr int compare(const char_type* __s1, const char_type* __s2, size_t __n) {
186+
return __builtin_wmemcmp(__s1, __s2, __n);
187+
}
188+
static constexpr const char_type* find(const char_type* __s, size_t __n, const char_type& __a) {
189+
return static_cast<const char_type*>(__builtin_wmemchr(__s, __a, __n));
190+
}
191+
static constexpr char_type* move(char_type* __s1, const char_type* __s2, size_t __n) {
192+
return static_cast<char_type*>(__builtin_wmemmove(__s1, __s2, __n));
193+
}
194+
static constexpr char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) {
195+
return static_cast<char_type*>(__builtin_wmemcpy(__s1, __s2, __n));
196+
}
197+
static constexpr char_type* assign(char_type* __s, size_t __n, char_type __a) {
198+
return static_cast<char_type*>(std::wmemset(__s, __a, __n));
199+
}
200+
201+
static constexpr char_type to_char_type(int_type __c) noexcept {
202+
return static_cast<char_type>(__c);
203+
}
204+
static constexpr int_type to_int_type(char_type __c) noexcept {
205+
return static_cast<int_type>(__c);
206+
}
207+
static constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept {
208+
return (__c1 == __c2);
209+
}
210+
static constexpr int_type eof() noexcept {
211+
return WEOF;
212+
}
213+
static constexpr int_type not_eof(int_type __c) noexcept {
214+
return (__c != eof()) ? __c : L'\0';
215+
}
216+
};
217+
218+
} // namespace std
219+
220+
#endif // _EZCXX_CHAR_TRAITS

src/libcxx/include/string

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88
#include <cstdio>
99
#include <cstdlib>
1010
#include <EASTL/string.h>
11+
#include <__char_traits>
1112

1213
#pragma clang system_header
1314

1415
namespace std {
1516

17+
template<
18+
typename CharT,
19+
typename Traits = std::char_traits<CharT>,
20+
typename Allocator = eastl::allocator
21+
>
22+
using basic_string = eastl::basic_string<CharT, Allocator>;
23+
1624
inline string to_string(int value) {
1725
char buf[sizeof("-8388608")];
1826
boot_sprintf(buf, "%d", value);

src/libcxx/include/string_view

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44

55
#include <__eastl_config>
66
#include <EASTL/string_view.h>
7+
#include <__char_traits>
78

89
#pragma clang system_header
910

11+
namespace std {
12+
13+
template<
14+
typename CharT,
15+
typename Traits = std::char_traits<CharT>
16+
>
17+
using basic_string_view = eastl::basic_string_view<CharT>;
18+
19+
} // namespace std
20+
1021
#endif // _EZCXX_STRING_VIEW

0 commit comments

Comments
 (0)