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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include <String.h>
#include <string.h>
String::String() {
ptr = 0;
len = 0;
}
String::String(const String &other) {
len = other.len;
if (len == 0) {
ptr = 0;
} else {
ptr = (char*)malloc(len + 1);
libc::memcpy(ptr, other.ptr, len + 1);
}
}
String::String(char* from) {
len = (from == 0 ? 0 : libc::strlen(from));
if (len == 0) {
ptr = 0;
} else {
ptr = (char*)malloc(len + 1);
libc::memcpy(ptr, from, len + 1);
}
}
String::String(char* from, int l) {
len = l;
if (len < 0) len = 0;
if (len == 0) {
ptr = 0;
} else {
ptr = (char*)malloc(len + 1);
libc::memcpy(ptr, from, len);
ptr[len] = 0;
}
}
String::String(char c, int count) {
len = count;
if (len == 0) {
ptr = 0;
} else {
ptr = (char*)malloc(len + 1);
libc::memset(ptr, c, len);
ptr[len] = 0;
}
}
String::~String() {
if (ptr != 0) free(ptr);
}
void String::operator=(const String &other) {
if (ptr != 0) free(ptr);
len = other.len;
if (len == 0) {
ptr = 0;
} else {
ptr = (char*)malloc(len + 1);
libc::memcpy(ptr, other.ptr, len + 1);
}
}
void String::operator=(char* from) {
if (ptr != 0) free(ptr);
len = (from == 0 ? 0 : libc::strlen(from));
if (len == 0) {
ptr = 0;
} else {
ptr = (char*)malloc(len + 1);
libc::memcpy(ptr, from, len + 1);
}
}
char* String::c_str() {
if (ptr == 0) return "";
return ptr;
}
bool String::operator==(const String& other) {
if (len != other.len) return false;
for (int i = 0; i < len; i++) if (ptr[i] != other.ptr[i]) return false;
return true;
}
bool String::operator==(char* other) {
if (other == 0) return (len == 0);
if (len != libc::strlen(other)) return false;
for (int i = 0; i < len; i++) if (ptr[i] != other[i]) return false;
return true;
}
bool String::operator<(const String& other) {
for (int i = 0; i < len && i < other.len; i++) {
if (ptr[i] > other.ptr[i]) return false;
if (ptr[i] < other.ptr[i]) return true;
}
if (len < other.len) return true;
return false;
}
static char crap;
char &String::operator[](int pos) {
if (pos >= 0 && pos < len) return ptr[pos];
crap = 0;
return crap;
}
String String::substr(int start, int count) {
if (start + count > len) count = len - start;
return String(ptr + start, count);
}
String String::operator+(const String& other) {
String ret(' ', len + other.len);
libc::memcpy(ret.ptr, ptr, len);
libc::memcpy(ret.ptr + len, other.ptr, other.len);
return ret;
}
void String::operator+=(const String& other) {
if (other.len == 0) return;
int newlen = len + other.len;
char* newptr = (char*)malloc(newlen + 1);
libc::memcpy(newptr, ptr, len);
libc::memcpy(newptr+len, other.ptr, other.len);
newptr[newlen] = 0;
free(ptr);
ptr = newptr;
len = newlen;
}
void String::operator+=(char c) {
char* newptr = (char*)malloc(len + 2);
libc::memcpy(newptr, ptr, len);
newptr[len] = c;
len++;
newptr[len] = 0;
free(ptr);
ptr = newptr;
}
String String::dec(int i) {
char b[16];
const char *e = libc::format_int(b, i);
return String(b, e - b);
}
String String::hex(uint32_t v) {
char b[16];
const char *e = libc::format_hex(b, v);
return String(b, e - b);
}
|