-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
UTF8Char.h
114 lines (93 loc) · 1.81 KB
/
UTF8Char.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#ifndef UTF8_CHAR_H
#define UTF8_CHAR_H
#include <ctype.h>
#include <string.h>
#include <UnicodeChar.h>
struct UTF8Char {
char bytes[4];
UTF8Char()
{
}
UTF8Char(char c)
{
bytes[0] = c;
}
UTF8Char(const char* c)
{
SetTo(c, ByteCount(*c));
}
UTF8Char(const char* c, int32 count)
{
SetTo(c, count);
}
void SetTo(const char* c, int32 count)
{
bytes[0] = c[0];
if (count > 1) {
bytes[1] = c[1];
if (count > 2) {
bytes[2] = c[2];
if (count > 3)
bytes[3] = c[3];
}
}
}
static int32 ByteCount(char firstChar)
{
// Note, this does not recognize invalid chars
uchar c = firstChar;
if (c < 0x80)
return 1;
if (c < 0xe0)
return 2;
return c < 0xf0 ? 3 : 4;
}
int32 ByteCount() const
{
return ByteCount(bytes[0]);
}
bool IsFullWidth() const
{
// TODO: Implement!
return false;
}
bool IsSpace() const
{
return BUnicodeChar::IsSpace(BUnicodeChar::FromUTF8(bytes));
}
bool IsAlNum() const
{
return BUnicodeChar::IsAlNum(BUnicodeChar::FromUTF8(bytes));
}
UTF8Char ToLower() const
{
uint32 c = BUnicodeChar::ToLower(BUnicodeChar::FromUTF8(bytes));
UTF8Char character;
char* utf8 = character.bytes;
BUnicodeChar::ToUTF8(c, &utf8);
return character;
}
bool operator==(const UTF8Char& other) const
{
int32 byteCount = ByteCount();
bool equals = bytes[0] == other.bytes[0];
if (byteCount > 1 && equals) {
equals = bytes[1] == other.bytes[1];
if (byteCount > 2 && equals) {
equals = bytes[2] == other.bytes[2];
if (byteCount > 3 && equals)
equals = bytes[3] == other.bytes[3];
}
}
return equals;
}
bool operator!=(const UTF8Char& other) const
{
return !(*this == other);
}
};
#endif // UTF8_CHAR_H