-
Notifications
You must be signed in to change notification settings - Fork 1
/
str.h
368 lines (339 loc) · 14.9 KB
/
str.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
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
//============================================================================
// Mini Spin-X Library
//
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
#ifndef PFC_CORE_STR_H
#define PFC_CORE_STR_H
//----------------------------------------------------------------------------
//============================================================================
// interface
//============================================================================
// external
#include "cstr.h"
#include "meta.h"
#include <stdio.h>
#include <cstdarg>
namespace pfc
{
template<typename> struct hash_func;
// new
template<class> class str_base;
template<typename T> struct is_type_str;
class heap_str;
template<unsigned capacity> class stack_str;
typedef stack_str<8> stack_str8;
typedef stack_str<16> stack_str16;
typedef stack_str<32> stack_str32;
typedef stack_str<64> stack_str64;
typedef stack_str<128> stack_str128;
typedef stack_str<256> stack_str256;
typedef stack_str<512> stack_str512;
typedef stack_str<1024> stack_str1024;
typedef stack_str1024 filepath_str;
class const_pstr;
class pstr;
// string operations
template<class S0, class S1> PFC_INLINE bool operator==(const str_base<S0>&, const str_base<S1>&);
template<class S> PFC_INLINE bool operator==(const str_base<S>&, const char*);
template<class S> PFC_INLINE bool operator==(const char*, const str_base<S>&);
template<class S0, class S1> PFC_INLINE bool operator!=(const str_base<S0>&, const str_base<S1>&);
template<class S> PFC_INLINE bool operator!=(const str_base<S>&, const char*);
template<class S> PFC_INLINE bool operator!=(const char*, const str_base<S>&);
template<class S0, class S1> PFC_INLINE bool operator<(const str_base<S0>&, const str_base<S1>&);
template<class S> PFC_INLINE bool operator<(const str_base<S>&, const char*);
template<class S> PFC_INLINE bool operator<(const char*, const str_base<S>&);
template<class S0, class S1> PFC_INLINE bool operator>(const str_base<S0>&, const str_base<S1>&);
template<class S> PFC_INLINE bool operator>(const str_base<S>&, const char*);
template<class S> PFC_INLINE bool operator>(const char*, const str_base<S>&);
template<class S0, class S1> PFC_INLINE bool operator<=(const str_base<S0>&, const str_base<S1>&);
template<class S> PFC_INLINE bool operator<=(const str_base<S>&, const char*);
template<class S> PFC_INLINE bool operator<=(const char*, const str_base<S>&);
template<class S0, class S1> PFC_INLINE bool operator>=(const str_base<S0>&, const str_base<S1>&);
template<class S> PFC_INLINE bool operator>=(const str_base<S>&, const char*);
template<class S> PFC_INLINE bool operator>=(const char*, const str_base<S>&);
template<class S> bool replace_substr(str_base<S> &res_, const str_base<S> &src_, const char *replace_, const char *replacement_);
// misc
stack_str8 bcd16_version_str(uint16_t version_); // e.g. 0x1020 = "1.02", or 0x123a = "1.23a"
//----------------------------------------------------------------------------
//============================================================================
// str_base
//============================================================================
template<class S>
class str_base
{
public:
// config
enum {max_format_str_size=1024};
//--------------------------------------------------------------------------
// construction
PFC_INLINE void operator=(const str_base&);
template<class S2> PFC_INLINE void operator=(const str_base<S2>&);
PFC_INLINE void operator=(const char*);
PFC_INLINE void set(const char*, usize_t num_chars_);
PFC_INLINE void clear();
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE usize_t size() const;
PFC_INLINE usize_t capacity() const;
PFC_INLINE const char &operator[](usize_t idx_) const;
PFC_INLINE char &operator[](usize_t idx_);
PFC_INLINE const char &front() const;
PFC_INLINE char &front();
PFC_INLINE const char &back() const;
PFC_INLINE char &back();
PFC_INLINE const char *c_str() const;
PFC_INLINE char *c_str();
PFC_INLINE const char *data() const;
PFC_INLINE char *data();
template<class S2> PFC_INLINE void operator+=(const str_base<S2>&);
PFC_INLINE void operator+=(const char*);
PFC_INLINE void operator+=(char);
void format(const char*, ...);
PFC_INLINE void push_front(char);
PFC_INLINE void push_front(char, usize_t num_chars_);
PFC_INLINE void push_front(const char*);
PFC_INLINE void push_front(const char*, usize_t num_chars_);
PFC_INLINE void push_back(char);
PFC_INLINE void push_back(char, usize_t num_chars_);
PFC_INLINE void push_back(const char*, usize_t num_chars_);
void push_back_format(const char*, ...);
PFC_INLINE void remove_front(usize_t num_chars_);
PFC_INLINE void remove_back(usize_t num_chars_);
//--------------------------------------------------------------------------
protected:
// construction
PFC_INLINE str_base();
PFC_INLINE str_base(const str_base&);
PFC_INLINE ~str_base();
};
//----------------------------------------------------------------------------
//============================================================================
// heap_str
//============================================================================
class heap_str: public str_base<heap_str>
{ PFC_MONO(heap_str) PFC_INTROSPEC_DECL;
public:
// construction
PFC_INLINE heap_str(memory_allocator_base *alloc_=0);
PFC_INLINE heap_str(const heap_str&, memory_allocator_base *alloc_=0);
template<class S> PFC_INLINE heap_str(const str_base<S>&, memory_allocator_base *alloc_=0);
PFC_INLINE heap_str(const char*, memory_allocator_base *alloc_=0);
PFC_INLINE heap_str(const char*, usize_t num_chars_, memory_allocator_base *alloc_=0);
PFC_INLINE heap_str(char, memory_allocator_base *alloc_=0);
PFC_INLINE ~heap_str();
PFC_INLINE void set_allocator(memory_allocator_base*);
PFC_INLINE void operator=(const heap_str&);
template<class S> PFC_INLINE void operator=(const str_base<S>&);
PFC_INLINE void operator=(const char*);
PFC_INLINE void set(const char*, usize_t num_chars_);
PFC_INLINE void clear();
PFC_INLINE void reset_size(usize_t size_);
PFC_INLINE void resize(usize_t size_);
PFC_INLINE void reserve(usize_t capacity_);
void swap(heap_str&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE memory_allocator_base &allocator() const;
PFC_INLINE usize_t size() const;
PFC_INLINE usize_t capacity() const;
PFC_INLINE const char &operator[](usize_t idx_) const;
PFC_INLINE char &operator[](usize_t idx_);
PFC_INLINE const char &front() const;
PFC_INLINE char &front();
PFC_INLINE const char &back() const;
PFC_INLINE char &back();
PFC_INLINE const char *c_str() const;
PFC_INLINE char *c_str();
PFC_INLINE const char *data() const;
PFC_INLINE char *data();
template<class S> PFC_INLINE void operator+=(const str_base<S>&);
PFC_INLINE void operator+=(const char*);
PFC_INLINE void operator+=(char);
PFC_INLINE void push_front(char);
PFC_INLINE void push_front(char, usize_t num_chars_);
PFC_INLINE void push_front(const char*);
PFC_INLINE void push_front(const char*, usize_t num_chars_);
PFC_INLINE void push_back(char);
PFC_INLINE void push_back(char, usize_t num_chars_);
PFC_INLINE void push_back(const char*, usize_t num_chars_);
PFC_INLINE void remove_front(usize_t num_chars_);
PFC_INLINE void remove_back(usize_t num_chars_);
//--------------------------------------------------------------------------
private:
enum {min_grow_alloc=15};
PFC_INLINE void reserve_offset(usize_t capacity_, usize_t offset_);
//--------------------------------------------------------------------------
memory_allocator_base *m_allocator;
char *m_data;
usize_t m_size;
usize_t m_capacity;
};
PFC_SET_TYPE_TRAIT(heap_str, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// stack_str
//============================================================================
template<unsigned cap>
class stack_str: public str_base<stack_str<cap> >
{ PFC_MONO(stack_str) PFC_INTROSPEC_DECL;
public:
// properties
enum {str_capacity=cap};
//--------------------------------------------------------------------------
// construction
PFC_INLINE stack_str();
PFC_INLINE stack_str(const stack_str&);
template<class S> PFC_INLINE stack_str(const str_base<S>&);
PFC_INLINE stack_str(const char*);
PFC_INLINE stack_str(char);
PFC_INLINE void operator=(const stack_str&);
template<class S> PFC_INLINE void operator=(const str_base<S>&);
PFC_INLINE void operator=(const char*);
PFC_INLINE void set(const char *str_, usize_t size_);
PFC_INLINE void clear();
PFC_INLINE void resize(usize_t size_);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE usize_t size() const;
PFC_INLINE usize_t capacity() const;
PFC_INLINE const char &operator[](usize_t idx_) const;
PFC_INLINE char &operator[](usize_t idx_);
PFC_INLINE const char &front() const;
PFC_INLINE char &front();
PFC_INLINE const char &back() const;
PFC_INLINE char &back();
PFC_INLINE const char *c_str() const;
PFC_INLINE char *c_str();
PFC_INLINE const char *data() const;
PFC_INLINE char *data();
template<class S> PFC_INLINE void operator+=(const str_base<S>&);
PFC_INLINE void operator+=(const char*);
PFC_INLINE void operator+=(char);
PFC_INLINE void push_front(char);
PFC_INLINE void push_front(char, usize_t num_chars_);
PFC_INLINE void push_front(const char*);
PFC_INLINE void push_front(const char*, usize_t num_chars_);
PFC_INLINE void push_back(char);
PFC_INLINE void push_back(char, usize_t num_chars_);
PFC_INLINE void push_back(const char*, usize_t num_chars_);
PFC_INLINE void remove_front(usize_t num_chars_);
PFC_INLINE void remove_back(usize_t num_chars_);
//--------------------------------------------------------------------------
private:
typedef typename meta_if<cap<256, uint8_t, uint16_t>::res size_type;
size_type m_size;
char m_data[cap+1];
};
PFC_SET_TYPE_TRAIT_PARTIAL(unsigned cap, stack_str<cap>, is_type_pod, true);
//----------------------------------------------------------------------------
//============================================================================
// const_pstr
//============================================================================
class const_pstr: public str_base<const_pstr>
{
public:
// construction
PFC_INLINE const_pstr();
PFC_INLINE const_pstr(const char *cstr_);
PFC_INLINE const_pstr(const char *str_, usize_t size_);
PFC_INLINE void init(const char *cstr_);
PFC_INLINE void init(const char *str_, usize_t size_);
PFC_INLINE void clear();
void swap(const_pstr&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE usize_t size() const;
PFC_INLINE usize_t capacity() const;
PFC_INLINE const char &operator[](usize_t idx_) const;
PFC_INLINE const char &front() const;
PFC_INLINE const char &back() const;
PFC_INLINE const char *c_str() const;
PFC_INLINE const char *data() const;
//--------------------------------------------------------------------------
private:
// unimplemented hidden mutating base-class members
void operator=(const const_pstr&);
void operator=(const char*);
PFC_INLINE void set(const char *str_, usize_t size_);
char *c_str();
template<class S> PFC_INLINE void operator+=(const str_base<S>&);
void operator+=(const char*);
void operator+=(char);
void format(const char*, ...);
void push_front(char);
void push_front(char, usize_t num_chars_);
void push_front(const char*);
void push_front(const char*, usize_t num_chars_);
void push_back(char);
void push_back(char, usize_t num_chars_);
void push_back(const char*, usize_t num_chars_);
void remove_front(usize_t num_chars_);
void remove_back(usize_t num_chars_);
//--------------------------------------------------------------------------
usize_t m_size;
const char *m_data;
};
PFC_SET_TYPE_TRAIT(const_pstr, is_type_pod, true);
//----------------------------------------------------------------------------
//============================================================================
// pstr
//============================================================================
class pstr: public str_base<pstr>
{
public:
// construction
PFC_INLINE pstr();
PFC_INLINE pstr(const pstr&);
PFC_INLINE pstr(char *cstr_);
PFC_INLINE pstr(char *str_, usize_t size_);
PFC_INLINE pstr(char *str_, usize_t size_, usize_t capacity_);
PFC_INLINE void init(char *cstr_);
PFC_INLINE void init(char *str_, usize_t size_);
PFC_INLINE void init(char *str_, usize_t size_, usize_t capacity_);
PFC_INLINE void operator=(const pstr&);
template<class S> PFC_INLINE void operator=(const str_base<S>&);
PFC_INLINE void operator=(const char*);
PFC_INLINE void set(const char *str_, usize_t size_);
PFC_INLINE void clear();
PFC_INLINE void resize(usize_t size_);
void swap(pstr&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE usize_t size() const;
PFC_INLINE usize_t capacity() const;
PFC_INLINE const char &operator[](usize_t idx_) const;
PFC_INLINE char &operator[](usize_t idx_);
PFC_INLINE const char &front() const;
PFC_INLINE char &front();
PFC_INLINE const char &back() const;
PFC_INLINE char &back();
PFC_INLINE const char *c_str() const;
PFC_INLINE char *c_str();
PFC_INLINE const char *data() const;
PFC_INLINE char *data();
template<class S> PFC_INLINE void operator+=(const str_base<S>&);
PFC_INLINE void operator+=(const char*);
PFC_INLINE void operator+=(char);
PFC_INLINE void push_front(char);
PFC_INLINE void push_front(char, usize_t num_chars_);
PFC_INLINE void push_front(const char*);
PFC_INLINE void push_front(const char*, usize_t num_chars_);
PFC_INLINE void push_back(char);
PFC_INLINE void push_back(char, usize_t num_chars_);
PFC_INLINE void push_back(const char*, usize_t num_chars_);
PFC_INLINE void remove_front(usize_t num_chars_);
PFC_INLINE void remove_back(usize_t num_chars_);
//--------------------------------------------------------------------------
private:
usize_t m_size;
usize_t m_capacity;
char *m_data;
};
PFC_SET_TYPE_TRAIT(pstr, is_type_pod, true);
//----------------------------------------------------------------------------
//============================================================================
#include "str.inl"
} // namespace pfc
#endif