This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.hpp
667 lines (566 loc) · 21 KB
/
string.hpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#ifndef MUSH_STRING_V2_HEADER
#define MUSH_STRING_V2_HEADER
#ifdef MUSH_STRING_HEADER
#warning Old version of string in use, careful.
#endif
#include <vector>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <string>
#include "core.hpp"
namespace mush
{
class String;
inline bool match_char32(char32_t c, const String& chars);
inline uint8_t read_utf32(char32_t& ref, const char* in)
{
char32_t byte;
byte = *in;
if (byte < 0x80) { ref = byte; return 1; }
else if ((byte & 0xe0) == 0xc0)
{
ref = (byte & 0x1f) << 6;
byte = *(in+1);
if (!((byte & 0xc0) == 0x80)) return 2;
ref |= (byte & 0x3f);
return 2;
}
else if ((byte & 0xf0) == 0xe0)
{
ref = (byte & 0x0f) << 12;
byte = *(in+1);
if (!((byte & 0xc0) == 0x80)) return 3;
ref |= ((byte & 0x3f) << 6);
byte = *(in+2);
if (!((byte & 0xc0) == 0x80)) return 3;
ref |= (byte & 0x3f);
return 3;
} else if ((byte & 0xf8) == 0xf0) {
ref = (byte & 0x07) << 18;
byte = *(in+1);
if (!((byte & 0xc0) == 0x80)) return 4;
ref |= ((byte & 0x3f) << 12);
byte = *(in+2);
if (!((byte & 0xc0) == 0x80)) return 4;
ref |= ((byte & 0x3f) << 6);
byte = *(in+3);
if (!((byte & 0xc0) == 0x80)) return 4;
ref |= (byte & 0x3f);
return 4;
}
return 0;
}
template <typename T>
uint8_t utf32_to_utf8(char32_t cp, T& output)
{
if (cp < 0x80)
{
output.push_back(static_cast<uint8_t>(cp));
return 1;
}
else if (cp < 0x800)
{
output.push_back(static_cast<uint8_t>((cp >> 6) | 0xc0));
output.push_back(static_cast<uint8_t>((cp & 0x3f) | 0x80));
return 2;
}
else if (cp < 0x10000)
{
output.push_back(static_cast<uint8_t>((cp >> 12) | 0xe0));
output.push_back(static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80));
output.push_back(static_cast<uint8_t>((cp & 0x3f) | 0x80));
return 3;
}
else
{
output.push_back(static_cast<uint8_t>((cp >> 18) | 0xe0));
output.push_back(static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80));
output.push_back(static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80));
output.push_back(static_cast<uint8_t>((cp & 0x3f) | 0x80));
return 4;
}
return 0;
}
//! String class, almost a drop-in-replacement for std::string
/*!
mush::String is almost a drop-in-replacement for std::string, though it handles
its data internally as UTF32.
*/
class String
{
private:
std::vector<char32_t> data;
public:
constexpr static char END_OF_FILE[] = "<MUSH_EOF>";
typedef char32_t value_type;
typedef char32_t& reference;
typedef const char32_t& const_reference;
typedef char32_t* pointer;
typedef const char32_t* const_pointer;
typedef size_t size_type;
constexpr static size_type npos = ~0;
// Constructors
String() = default;
String(const String& other) = default;
String(String&& other) = default;
//! Create from C++11 char32_t array
/*!
Creates a mush string instance from data provided in C++11 char32_t
array. (i.e. U("This is an example.")) It is presumed that the
character array is already UTF32-encoded and no conversions or checks
are made to ensure this.
*/
String(const char32_t* in)
{
data.clear();
while(*in != 0x00000000)
{
data.push_back(*in);
in++;
}
}
//! Create from character array
/*!
Creates a mush string instance from data provided in character array.
It is presumed that the character array data is encoded in either
UTF-8 or ASCII format.
*/
String(const char* in, size_t length = ~0)
{
char32_t utf32;
uint32_t len, tlen = 0;
while(*in != 0x00)
{
len = read_utf32(utf32, in);
if (len == 0) break;
in += len;
tlen += len;
if (tlen > length)
break;
data.push_back(utf32);
}
}
String(const unsigned char* in, size_t length = ~0)
{
char32_t utf32;
uint8_t len, tlen = 0;
while(*in != 0x00)
{
len = read_utf32(utf32, (const char*)in);
if (len == 0) break;
in += len;
tlen += len;
if (tlen > length)
break;
data.push_back(utf32);
}
}
//! Construct from STL string
/*!
Creates mush string instance from data provided in std::string.
It is presumed that the std::string data is encoded in UTF-8 or
ASCII format.
*/
String(const std::string& in)
{
data.clear();
char32_t utf32;
size_t i = 0;
while (i < in.size())
{
i += read_utf32(utf32, in.c_str() + i);
data.push_back(utf32);
}
}
template <typename T, typename std::enable_if<std::is_integral<T>::value>::type = 0>
String(T value)
{
std::string result = std::to_string(value);
*this = String(result);
}
template <typename T, typename std::enable_if<std::is_floating_point<T>::value>::type = 0>
String(T value)
{
std::string result = std::to_string(value);
*this = String(result);
}
String(const char32_t c) { data.push_back(c); }
std::vector<char32_t>::iterator begin() { return data.begin(); }
std::vector<char32_t>::const_iterator begin() const { return data.begin(); }
std::vector<char32_t>::iterator end() { return data.end(); }
std::vector<char32_t>::const_iterator end() const { return data.end(); }
std::vector<char32_t>::reverse_iterator rbegin() { return data.rbegin(); }
std::vector<char32_t>::const_reverse_iterator rbegin() const { return data.rbegin(); }
std::vector<char32_t>::reverse_iterator rend() { return data.rend(); }
std::vector<char32_t>::const_reverse_iterator rend() const { return data.rend(); }
char32_t& operator[](const int index) { return *(data.begin() + index); }
const char32_t operator[](const int index) const { return *(data.begin() + index); }
size_t length() const { return data.size(); }
size_t size() const { return data.size(); }
/*
TODO: Implement this
size_t utf8_length() const
{
size_t len;
for (value_type c : *this)
{
}
return len;
}
*/
const char32_t* ptr() const { return &data[0]; }
//! Generate std::string
/*
The string's contents are converted from UTF-32 format to UTF-8 encoding
and returned as C++ STL string.
*/
std::string std_str() const
{
std::string result;
for (auto c : data)
utf32_to_utf8(c, result);
return result;
}
//! Copy assignment operator
String& operator=(const String& other)
{
if (this == &other)
return *this;
data.resize(other.data.size());
memcpy(&data[0], &other.data[0], other.data.size() * sizeof(char32_t));
return *this;
}
//! Move assignment operator
String& operator=(String&& other)
{
if (this == &other)
return *this;
std::swap(data, other.data);
return *this;
}
//! Concatenate (addition) operator
/*!
Allows concatenating strings together, may be used with types
conversible to strings, such as integers and floats.
*/
String operator+(const String& other) const
{
String rval(*this);
for (auto c : other.data)
rval.data.push_back(c);
return rval;
}
String& operator+=(const String& other)
{
*this = *this + other;
return *this;
}
bool operator==(const String& other) const
{
if (data.size() != other.data.size())
return false;
for (size_t i = 0; i < data.size(); ++i)
if (data[i] != other.data[i])
return false;
return true;
}
bool operator!=(const String& other) const
{
return !(*this == other);
}
bool operator<(const String& other) const
{
for (size_t i = 0; i < std::min(size(), other.size()); ++i)
if (data[i] < other.data[i])
return true;
else if (data[i] > other.data[i])
return false;
return false;
}
bool operator>(const String& other) const
{
for (size_t i = 0; i < std::min(size(), other.size()); ++i)
if (data[i] < other.data[i])
return false;
else if (data[i] > other.data[i])
return true;
return true;
}
//! Implicit conversion to std::string
operator std::string() const
{
return this->std_str();
}
//! Checks if the string is empty
/*!
*/
bool empty() const
{
return size() == 0;
}
//! Clears all data from the string
/*!
*/
void clear()
{
data.clear();
}
//! Split a string by a delim
/*!
Returns a vector of string objects that are substrings cut at given deliminators.
*/
std::vector<String> split(const String& delim = " \n\t") const
{
std::vector<String> rval;
if (data.size() == 0)
return rval;
size_t pos = 0;
for (size_t i = 0; i < data.size(); ++i)
{
if (match_char32(data[i], delim))
{
while(match_char32(data[pos], delim))
pos++;
String temp = this->substr(pos, i - pos);
if (temp.length() != 0)
{
rval.push_back(temp);
pos = i;
}
}
}
while(match_char32(data[pos], delim))
pos++;
String temp = this->substr(pos, data.size() - pos);
if (temp.length() != 0)
rval.push_back(temp);
return rval;
}
template <typename T>
T to_value() const
{
if constexpr (std::is_integral<T>::value)
return strtol(std_str().c_str(), nullptr, 0);
else if constexpr (std::is_floating_point<T>::value)
return atof(std_str().c_str());
else
{
static_assert(dependent_false<T>(), "invalid type template argument to to_value()");
}
}
//! Generate substring
/*!
Returns a newly constructed string object with its value initialised to
a copy of a substring of this object
The substring is part of the object that starts at character position pos
and spans len characters or until the end of string.
*/
String substr(size_t pos, size_t len = ~0) const
{
String rval;
for (uint32_t it = pos; it < pos + len; ++it)
{
if (it >= data.size())
return rval;
rval.data.push_back(data[it]);
}
return rval;
}
//! Does a string start with a sequence
/*!
Checks if the string starts with a sequence specified by the argument seq
*/
bool starts_with(const String& seq) const
{
if (this->length() < seq.length())
return false;
if (memcmp(this->ptr(), seq.ptr(), seq.length() * sizeof(value_type)) != 0)
return false;
return true;
}
//! Does a string contain a sequence
/*!
Searches the string for a sequence specified by the argument seq
*/
bool contains(const String& seq, size_type* pos = nullptr) const
{
if (this->length() < seq.length())
return false;
size_t lastpos = this->size() - seq.size() + 1;
for (size_t i = 0; i < lastpos; ++i)
{
if (substr(i, seq.size()) == seq)
{
if (pos != nullptr)
*pos = i + seq.size();
return true;
}
}
return false;
}
size_type find_last_of(const String& seq) const
{
for (size_t i = size()-1; i != npos; --i)
{
if (match_char32((*this)[i], seq))
return i;
}
return npos;
}
//! Remove trailing characters
String rstrip(const String& seq = " \t")
{
size_t pos;
if ((pos = find_last_of(seq)) == npos)
return *this;
return substr(0, pos);
}
//! Get substring after a sequence
String after(const String& seq) const
{
size_t pos = 0;
if (contains(seq, &pos))
return substr(pos, size());
return "";
}
friend inline std::ostream& operator<<(std::ostream& out, const mush::String& str)
{
out << str.std_str();
return out;
}
friend inline String operator+(const char* left, const mush::String& right)
{
return mush::String(left) + right;
}
template <size_t SizeSize = sizeof(size_t)>
inline size_t hash() const noexcept
{
std::abort();
}
};
// 64-bit hash implementation
template<> inline size_t String::hash<8>() const noexcept
{
size_t hash = 0xCBF29CE484222325;
constexpr uint64_t prime = 0x100000001B3;
uint8_t* bytep = (uint8_t*)ptr();
for (size_t it = 0; it < length() * 4; ++it)
hash = (hash ^ *(bytep + it)) * prime;
return hash;
};
// 32-bit version
template<> inline size_t String::hash<4>() const noexcept
{
size_t hash = 0x811C9DC5;
constexpr uint32_t prime = 0x1000193;
uint8_t* bytep = (uint8_t*)ptr();
for (size_t it = 0; it < length() * 4; ++it)
hash = (hash ^ *(bytep + it)) * prime;
return hash;
};
#ifndef MUSH_INTERNAL_REMOVE_CR
#define MUSH_INTERNAL_REMOVE_CR
template <typename T> struct remove_cr { typedef T type; };
template <typename T> struct remove_cr<T&> { typedef T type; };
template <typename T> struct remove_cr<T&&> { typedef T type; };
template <typename T> struct remove_cr<const T> { typedef T type; };
template <typename T> struct remove_cr<const T&> { typedef T type; };
template <typename T> struct remove_cr<const T&&> { typedef T type; };
#endif
#ifndef NO_CONCEPTS
template <typename T>
concept bool AnyString = std::is_same<typename remove_cr<T>::type, mush::String>::value;
#endif
// compares a character to number of other characters
inline bool match_char32(char32_t c, const String& chars)
{
for (char32_t c_c : chars)
if (c == c_c)
return true;
return false;
}
inline char* c_string(const String& s, char* out)
{
if (out == nullptr)
return nullptr;
size_t pos = 0; char32_t cp;
for (size_t i = 0; i < s.length(); i++)
{
cp = s[i];
if (cp < 0x80)
{
*(out + pos) = static_cast<char>(s[i]);
pos++;
continue;
} else if (cp < 0x800) {
*(out + pos) = static_cast<char>((s[i] >> 6) | 0xc0); pos++;
*(out + pos) = static_cast<char>((s[i] & 0x3f) | 0x80); pos++;
continue;
} else if (cp < 0x10000)
{
*(out + pos) = static_cast<char>((s[i] >> 12) | 0xe0); pos++;
*(out + pos) = static_cast<char>(((s[i] >> 6) & 0x3f) | 0x80); pos++;
*(out + pos) = static_cast<char>((s[i] & 0x3f) | 0x80); pos++;
} else
{
*(out + pos) = static_cast<char>((s[i] >> 18) | 0xe0); pos++;
*(out + pos) = static_cast<char>(((s[i] >> 12) & 0x3f) | 0x80); pos++;
*(out + pos) = static_cast<char>(((s[i] >> 6) & 0x3f) | 0x80); pos++;
*(out + pos) = static_cast<char>((s[i] & 0x3f) | 0x80); pos++;
}
}
*(out + pos) = '\0';
return out;
}
/*
template <typename CharT = char32_t>
class String_View
{
private:
CharT* pdata;
size_t ssize;
public:
typedef CharT value_type;
String_View() : pdata(nullptr), ssize(0) {}
String_View(const String_View&) = default;
String_View& operator=(const String_View&) = default;
template <typename CT>
String_View(const String_View<CT>& str) : pdata(str.data(), ssize(str.size())) {}
};
template <typename CharT = char32_t>
using string_view = String_View<CharT>;
*/
#ifndef DISABLE_LEGACY
using string = String;
#endif
}
namespace std
{
// std::hash specialisation for mush::string
template<>
struct hash<mush::String>
{
size_t operator()(const mush::String& __s) const noexcept
{
return __s.hash();
}
};
}
#endif
/*
Copyright (c) 2017 Jari Ronkainen
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/