Skip to content

Commit 7774719

Browse files
committed
implemented std::char_traits
1 parent ead8c00 commit 7774719

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

src/libcxx/include/__char_traits

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
10+
#pragma clang system_header
11+
12+
namespace std {
13+
14+
using streamoff = ptrdiff_t;
15+
using streamsize = ptrdiff_t;
16+
17+
} // namespace std
18+
19+
namespace std {
20+
21+
template<class CharT> struct char_traits;
22+
template<> struct char_traits<char>;
23+
template<> struct char_traits<char8_t>;
24+
template<> struct char_traits<char16_t>;
25+
template<> struct char_traits<char32_t>;
26+
template<> struct char_traits<wchar_t>;
27+
28+
template<> struct char_traits<char> {
29+
using char_type = char;
30+
using int_type = int;
31+
using off_type = streamoff;
32+
33+
static constexpr void assign(char_type& c1, const char_type& c2) noexcept {
34+
c1 = c2;
35+
}
36+
static constexpr bool eq(char_type c1, char_type c2) noexcept {
37+
return (c1 == c2);
38+
}
39+
static constexpr bool lt(char_type c1, char_type c2) noexcept {
40+
return (c1 < c2);
41+
}
42+
43+
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n) {
44+
return std::memcmp(s1, s2, n);
45+
}
46+
static constexpr size_t length(const char_type* s) {
47+
return std::strlen(s);
48+
}
49+
static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a) {
50+
return static_cast<const char_type*>(std::memchr(s, a, n));
51+
}
52+
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n) {
53+
return static_cast<char_type*>(std::memmove(s1, s2, n));
54+
}
55+
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n) {
56+
return static_cast<char_type*>(std::memcpy(s1, s2, n));
57+
}
58+
static constexpr char_type* assign(char_type* s, size_t n, char_type a) {
59+
return static_cast<char_type*>(std::memset(s, a, n));
60+
}
61+
62+
static constexpr char_type to_char_type(int_type c) noexcept {
63+
return static_cast<char_type>(c);
64+
}
65+
static constexpr int_type to_int_type(char_type c) noexcept {
66+
return static_cast<int_type>(c);
67+
}
68+
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept {
69+
return (c1 == c2);
70+
}
71+
static constexpr int_type eof() noexcept {
72+
return EOF;
73+
}
74+
static constexpr int_type not_eof(int_type c) noexcept {
75+
return (c != eof()) ? c : '\0';
76+
}
77+
};
78+
79+
template<> struct char_traits<char8_t> {
80+
using char_type = char8_t;
81+
using int_type = unsigned int;
82+
using off_type = streamoff;
83+
84+
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
85+
static constexpr bool eq(char_type c1, char_type c2) noexcept;
86+
static constexpr bool lt(char_type c1, char_type c2) noexcept;
87+
88+
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
89+
static constexpr size_t length(const char_type* s);
90+
static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a);
91+
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
92+
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
93+
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
94+
95+
static constexpr char_type to_char_type(int_type c) noexcept;
96+
static constexpr int_type to_int_type(char_type c) noexcept;
97+
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
98+
static constexpr int_type eof() noexcept;
99+
static constexpr int_type not_eof(int_type c) noexcept;
100+
};
101+
102+
template<> struct char_traits<char16_t> {
103+
using char_type = char16_t;
104+
using int_type = uint_least16_t;
105+
using off_type = streamoff;
106+
107+
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
108+
static constexpr bool eq(char_type c1, char_type c2) noexcept;
109+
static constexpr bool lt(char_type c1, char_type c2) noexcept;
110+
111+
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
112+
static constexpr size_t length(const char_type* s);
113+
static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a);
114+
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
115+
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
116+
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
117+
118+
static constexpr char_type to_char_type(int_type c) noexcept;
119+
static constexpr int_type to_int_type(char_type c) noexcept;
120+
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
121+
static constexpr int_type eof() noexcept;
122+
static constexpr int_type not_eof(int_type c) noexcept;
123+
};
124+
125+
template<> struct char_traits<char32_t> {
126+
using char_type = char32_t;
127+
using int_type = uint_least32_t;
128+
using off_type = streamoff;
129+
130+
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
131+
static constexpr bool eq(char_type c1, char_type c2) noexcept;
132+
static constexpr bool lt(char_type c1, char_type c2) noexcept;
133+
134+
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
135+
static constexpr size_t length(const char_type* s);
136+
static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a);
137+
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
138+
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
139+
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
140+
141+
static constexpr char_type to_char_type(int_type c) noexcept;
142+
static constexpr int_type to_int_type(char_type c) noexcept;
143+
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
144+
static constexpr int_type eof() noexcept;
145+
static constexpr int_type not_eof(int_type c) noexcept;
146+
};
147+
148+
template<> struct char_traits<wchar_t> {
149+
using char_type = wchar_t;
150+
using int_type = __WINT_TYPE__;
151+
using off_type = streamoff;
152+
153+
static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
154+
static constexpr bool eq(char_type c1, char_type c2) noexcept;
155+
static constexpr bool lt(char_type c1, char_type c2) noexcept;
156+
157+
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n);
158+
static constexpr size_t length(const char_type* s);
159+
static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a);
160+
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n);
161+
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n);
162+
static constexpr char_type* assign(char_type* s, size_t n, char_type a);
163+
164+
static constexpr char_type to_char_type(int_type c) noexcept;
165+
static constexpr int_type to_int_type(char_type c) noexcept;
166+
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
167+
static constexpr int_type eof() noexcept;
168+
static constexpr int_type not_eof(int_type c) noexcept;
169+
};
170+
171+
} // namespace std
172+
173+
#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)