-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.cpp
105 lines (80 loc) · 2 KB
/
Strings.cpp
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
#include "Strings.h"
#include <string.h>
String* String_New(uint16 size) {
String* string;
string = Allocate(String);
String_Initialize(string, size);
return string;
}
String* String_NewFromCString(int8* cString) {
String* string;
uint16 size;
size = (uint16)strlen(cString);
string = Allocate(String);
String_Initialize(string, size);
string->Length = 0;
String_AppendCString(string, cString);
return string;
}
void String_Initialize(String* string, uint16 size) {
string->Length = size;
Array_Initialize(&string->Data, size);
}
void String_Free(String* self) {
String_Uninitialize(self);
Free(self);
}
void String_Uninitialize(String* self) {
assert(self != NULL);
Array_Free(&self->Data);
self->Length = 0;
}
void String_AppendCString(String* self, int8* cString) {
uint16 size;
assert(self != NULL);
assert(cString != NULL);
size = (uint16)strlen(cString);
Array_Write(&self->Data, (uint8*)cString, self->Length, size);
self->Length += size;
}
void String_AppendBytes(String* self, int8* bytes, uint16 size) {
assert(self != NULL);
assert(bytes != NULL);
Array_Write(&self->Data, (uint8*)bytes, self->Length, size);
self->Length += size;
}
void String_AppendString(String* self, String* source) {
assert(self != NULL && source != NULL);
Array_Append(&self->Data, &source->Data);
self->Length += source->Length;
}
boolean String_IsUTF8(String* self) {
uint16 length = self->Length;
uint8 byte = 0;
uint8 bytesToFind = 0;
uint8 i;
uint8* bytes;
bytes = Array_Read(&self->Data, 0, self->Length);
for (byte = bytes[i = 0]; i < length; byte = bytes[++i])
if (bytesToFind == 0)
if ((byte >> 1) == 126)
bytesToFind = 5;
else if ((byte >> 2) == 62)
bytesToFind = 4;
else if ((byte >> 3) == 30)
bytesToFind = 3;
else if ((byte >> 4) == 14)
bytesToFind = 2;
else if ((byte >> 5) == 6)
bytesToFind = 1;
else if ((byte >> 7) == 0)
;
else
return false;
else
if ((byte >> 6) == 2)
bytesToFind--;
else
return false;
return true;
}