-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathchar_traits.h
74 lines (58 loc) · 2 KB
/
char_traits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdint>
#include <cstring>
#include <string>
namespace std
{
template<>
struct char_traits<uint8_t>
{
using char_type = uint8_t;
using int_type = unsigned int;
using off_type = std::streamoff;
using pos_type = std::streampos;
using state_type = std::mbstate_t;
static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; }
static bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; }
static bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; }
static int compare(const char_type* s1, const char_type* s2, std::size_t n) noexcept
{
return std::memcmp(s1, s2, n);
}
static std::size_t length(const char_type* s) noexcept
{
const char_type* p = s;
while (*p != 0)
++p;
return p - s;
}
static const char_type* find(const char_type* s, std::size_t n, const char_type& a) noexcept
{
for (std::size_t i = 0; i < n; ++i)
{
if (eq(s[i], a))
{
return s + i;
}
}
return nullptr;
}
static char_type* move(char_type* s1, const char_type* s2, std::size_t n) noexcept
{
return static_cast<char_type*>(std::memmove(s1, s2, n));
}
static char_type* copy(char_type* s1, const char_type* s2, std::size_t n) noexcept
{
return static_cast<char_type*>(std::memcpy(s1, s2, n));
}
static char_type* assign(char_type* s, std::size_t n, char_type a) noexcept
{
std::fill_n(s, n, a);
return s;
}
static int_type not_eof(int_type c) noexcept { return c == eof() ? ~eof() : c; }
static char_type to_char_type(int_type c) noexcept { return static_cast<char_type>(c); }
static int_type to_int_type(char_type c) noexcept { return static_cast<int_type>(c); }
static bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; }
static int_type eof() noexcept { return static_cast<int_type>(-1); }
};
} // namespace std