Skip to content

Commit 637755d

Browse files
committed
Fix ustring type relying on non-standard C++ behavior
1 parent 9d7400a commit 637755d

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/types.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
1+
#pragma once
2+
13
#include <string>
24

3-
typedef std::basic_string<unsigned char> ustring;
5+
// Define custom char traits since std::char_traits<unsigend char> is not part of C++ standard
6+
struct uchar_traits : std::char_traits<char>
7+
{
8+
using super = std::char_traits<char>;
9+
using char_type = unsigned char;
10+
11+
static void assign(char_type& c1, const char_type& c2) noexcept {
12+
c1 = c2;
13+
}
14+
static char_type* assign(char_type* ptr, std::size_t count, char_type c2) {
15+
return reinterpret_cast<char_type*>(
16+
super::assign(reinterpret_cast<char*>(ptr), count, static_cast<char>(c2)));
17+
}
18+
19+
static char_type* move(char_type* dest, const char_type* src, std::size_t count) {
20+
return reinterpret_cast<char_type*>(
21+
super::move(reinterpret_cast<char*>(dest), reinterpret_cast<const char*>(src), count));
22+
}
23+
24+
static char_type* copy(char_type* dest, const char_type* src, std::size_t count) {
25+
return reinterpret_cast<char_type*>(
26+
super::copy(reinterpret_cast<char*>(dest), reinterpret_cast<const char*>(src), count));
27+
}
28+
29+
static int compare(const char_type* s1, const char_type* s2, std::size_t count) {
30+
return super::compare(reinterpret_cast<const char*>(s1), reinterpret_cast<const char*>(s2), count);
31+
}
32+
33+
static char_type to_char_type(int_type c) noexcept {
34+
return static_cast<char_type>(c);
35+
}
36+
};
37+
38+
typedef std::basic_string<unsigned char, uchar_traits> ustring;
439
typedef unsigned int uint;
540
typedef unsigned char u8;

0 commit comments

Comments
 (0)