-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpz_string.cpp
369 lines (314 loc) · 7.29 KB
/
pz_string.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Plasma strings
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include <string.h>
#include "pz_common.h"
#include "pz_gc.h"
#include "pz_string.h"
namespace pz {
static void
AssertAligned(const void *p) {
assert((reinterpret_cast<uintptr_t>(p) & HIGH_TAG_MASK) == 0);
}
String::String() :
mType(ST_EMPTY)
{
s.cStr = nullptr;
}
String::String(const BaseString * base_str) :
mType(ST_FLAT)
{
// Pointers must be aligned
AssertAligned(base_str);
s.baseStr = base_str;
}
String::String(const char *c_str) :
mType(ST_CONST)
{
// Pointers must be aligned
AssertAligned(c_str);
s.cStr = c_str;
}
void *
String::ptr() const {
if (mType == ST_EMPTY) {
return nullptr;
} else {
return reinterpret_cast<void*>(
reinterpret_cast<uintptr_t>(s.cStr) |
(static_cast<uintptr_t>(mType) << HIGH_TAG_SHIFT));
}
}
String
String::from_ptr(void *ptr) {
if (ptr == nullptr) {
return String();
} else {
StringType tag = static_cast<StringType>(
(reinterpret_cast<uintptr_t>(ptr) & HIGH_TAG_MASK) >> HIGH_TAG_SHIFT);
uintptr_t pointer_no_tag =
reinterpret_cast<uintptr_t>(ptr) & ~HIGH_TAG_MASK;
switch (tag) {
case ST_FLAT:
return String(reinterpret_cast<BaseString*>(pointer_no_tag));
case ST_CONST:
return String(reinterpret_cast<const char *>(pointer_no_tag));
default:
abort();
}
}
}
void
String::print() const {
switch (mType) {
case ST_CONST:
printf("%s", s.cStr);
break;
case ST_FLAT:
s.baseStr->print();
break;
case ST_EMPTY:
break;
}
}
uint32_t
String::length() const {
switch (mType) {
case ST_CONST:
return strlen(s.cStr);
case ST_FLAT:
return s.baseStr->length();
case ST_EMPTY:
return 0;
default:
abort();
}
}
bool
String::isEmpty() const {
switch (mType) {
case ST_CONST:
return s.cStr[0] == '\0';
case ST_FLAT:
return s.baseStr->isEmpty();
case ST_EMPTY:
return true;
default:
abort();
}
}
bool
String::equals(const String &other) const {
return equals_pointer(other) || (0 == strcmp(c_str(), other.c_str()));
}
bool
String::equals_pointer(const String &other) const {
return this->s.cStr == other.s.cStr;
}
bool
String::startsWith(const String & other, GCCapability &gc0) const {
if (other.length() > length())
return false;
GCTracer gc(gc0);
Root<StringPos> thispos(gc, begin(gc));
Root<StringPos> otherpos(gc, other.begin(gc));
while (!otherpos->at_end()) {
assert(!thispos->at_end());
if (thispos->next_char() != otherpos->next_char()) {
return false;
}
thispos = thispos->forward(gc);
otherpos = otherpos->forward(gc);
}
return true;
}
const char *
String::c_str() const {
switch (mType) {
case ST_CONST:
return s.cStr;
case ST_FLAT:
return s.baseStr->c_str();
case ST_EMPTY:
return "";
default:
abort();
}
}
CodePoint32
String::char_at(unsigned i) const {
assert(i < length());
// XXX make better.
return c_str()[i];
}
size_t
String::hash() const {
const char *c = c_str();
size_t hash = 0;
for (unsigned i = 0; i < length(); i++) {
hash = (hash >> (sizeof(size_t)*8-1) | hash << 1) ^
std::hash<char>{}(c[i]);
}
return hash;
}
String
String::append(GCCapability &gc, const String s1, const String s2) {
uint32_t len = s1.length() + s2.length();
if (len == 0) {
return String();
} else {
FlatString *s = FlatString::New(gc, len);
strcpy(s->buffer(), s1.c_str());
strcat(s->buffer(), s2.c_str());
return String(s);
}
}
String
String::substring(GCCapability &gc, const StringPos * pos1,
const StringPos * pos2)
{
assert(pos1->mPos <= pos1->mStr.length());
assert(pos2->mPos <= pos2->mStr.length());
if (!pos1->mStr.equals_pointer(pos2->mStr)) {
fprintf(stderr, "Substring for two different strings\n");
exit(1);
}
// This uses negative numbers to check when the beginning is after the
// end.
int len = pos2->mPos - pos1->mPos;
if (len <= 0) {
return String();
}
FlatString *s = FlatString::New(gc, len);
strncpy(s->buffer(), &pos1->mStr.c_str()[pos1->mPos], len);
return String(s);
}
String
String::dup(GCCapability &gc, const std::string & str)
{
uint32_t len = str.length();
if (len == 0) {
return String();
} else {
FlatString *s = FlatString::New(gc, len);
strcpy(s->buffer(), str.c_str());
return String(s);
}
}
bool
String::operator==(const String other) const
{
return equals(other);
}
StringPos*
String::begin(GCCapability &gc) const {
return new(gc) StringPos(*this, 0);
}
StringPos*
String::end(GCCapability &gc) const {
// XXX won't work with other encodings.
return new(gc) StringPos(*this, length());
}
/*
* FlatString
*************/
FlatString::FlatString(uint32_t len) :
mLen(len)
{
#ifdef DEBUG
// Make debugging slightly more predictable.
memset(mBuffer, 'X', len);
#endif
mBuffer[len] = 0;
}
FlatString*
FlatString::New(GCCapability &gc, uint32_t len) {
void *mem = gc.alloc_bytes(sizeof(FlatString) + len + 1);
return new(mem) FlatString(len);
}
StringType
FlatString::type() const{
return ST_FLAT;
}
void
FlatString::print() const {
printf("%s", mBuffer);
}
uint32_t
FlatString::length() const {
return mLen;
}
bool
FlatString::isEmpty() const {
return mLen == 0;
}
uint32_t
FlatString::storageSize() const {
return sizeof(FlatString) + mLen + 1;
}
const char *
FlatString::c_str() const {
return reinterpret_cast<const char *>(mBuffer);
}
void
FlatString::fixSize(uint32_t len) {
assert(len <= mLen);
mBuffer[len] = 0;
mLen = len;
}
/*
* StringPos
************/
bool
StringPos::at_beginning() const {
return mPos == 0;
}
bool
StringPos::at_end() const {
return mPos == mStr.length();
}
StringPos *
StringPos::forward(GCCapability &gc) const {
if (at_end()) {
fprintf(stderr, "StringPos already at end\n");
exit(1);
}
return new(gc) StringPos(mStr, mPos+1);
}
StringPos *
StringPos::backward(GCCapability &gc) const {
if (at_beginning()) {
fprintf(stderr, "StringPos already at beginning");
exit(1);
}
return new(gc) StringPos(mStr, mPos-1);
}
CodePoint32
StringPos::next_char() const {
if (mPos == mStr.length()) {
fprintf(stderr, "Access next character at end of string\n");
exit(1);
}
return mStr.char_at(mPos);
}
CodePoint32
StringPos::prev_char() const {
if (mPos == 0) {
fprintf(stderr, "Access previous character at beginning of string\n");
exit(1);
}
return mStr.char_at(mPos - 1);
}
} // namespace pz
namespace std
{
size_t
hash<pz::String>::operator()(pz::String const& s) const noexcept
{
return s.hash();
}
}