forked from msinilo/rdestl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_string.h
335 lines (302 loc) · 8.07 KB
/
basic_string.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
#ifndef RDESTL_BASIC_STRING_H
#define RDESTL_BASIC_STRING_H
#include "rdestl_common.h"
#include "allocator.h"
#include "simple_string_storage.h"
#include "string_utils.h"
//#include <istream>
//#include <ostream>
namespace rde
{
//=============================================================================
// @note: this one is totally _not_ std::string compatible for the time being!
// one way conversion should work, ie rde --> STL.
template<typename E,
class TAllocator = rde::allocator,
typename TStorage = rde::simple_string_storage<E, TAllocator>>
class basic_string : private TStorage
{
public:
typedef typename TStorage::value_type value_type;
typedef typename TStorage::size_type size_type;
typedef typename TStorage::const_iterator const_iterator;
typedef typename TStorage::allocator_type allocator_type;
//For find
static const size_type npos = size_type(-1);
explicit basic_string(const allocator_type& allocator = allocator_type())
: TStorage(allocator)
{
/**/
}
// yeah, EXPLICIT.
explicit basic_string(const value_type* str, const allocator_type& allocator = allocator_type())
: TStorage(str, allocator)
{
/**/
}
basic_string(const value_type* str, size_type len, const allocator_type& allocator = allocator_type())
: TStorage(str, len, allocator)
{
/**/
}
basic_string(const basic_string& str, const allocator_type& allocator = allocator_type())
: TStorage(str, allocator)
{
/**/
}
~basic_string()
{
/**/
}
size_type capacity() const { return TStorage::capacity(); }
// No operator returning ref for the time being. It's dangerous with COW.
value_type operator[](size_type i) const
{
RDE_ASSERT(i < length());
RDE_ASSERT(invariant());
return c_str()[i];
}
basic_string& operator=(const basic_string& rhs)
{
RDE_ASSERT(rhs.invariant());
if (this != &rhs) {
TStorage::operator=((TStorage&)rhs);
}
RDE_ASSERT(invariant());
return *this;
}
basic_string& operator=(const value_type* str)
{
return assign(str);
}
basic_string& assign(const value_type* str, size_type len)
{
TStorage::assign(str, len);
RDE_ASSERT(invariant());
return *this;
}
basic_string& assign(const value_type* str)
{
return assign(str, strlen(str));
}
basic_string substr(size_type begin, size_type end) const
{
RDE_ASSERT(end >= begin && end <= length() && begin >= 0);
return basic_string(c_str() + begin, end - begin);
}
basic_string substr(size_type begin) const
{
return substr(begin, length());
}
void append(const value_type* str, size_type len)
{
if (!str || len == 0 || *str == 0)
return;
TStorage::append(str, len);
}
void append(const basic_string& str)
{
append(str.c_str(), str.length());
}
void append(const value_type* str)
{
append(str, strlen(str));
}
void append(const value_type ch)
{
append(&ch, 1);
}
basic_string& operator+=(const basic_string& rhs)
{
append(rhs);
return *this;
}
int compare(const value_type* str) const
{
const size_type thisLen = length();
const size_type strLen = strlen(str);
if (thisLen < strLen)
return -1;
if (thisLen > strLen)
return 1;
return strcompare(c_str(), str, thisLen);
}
int compare(const basic_string& rhs) const
{
// COW optimization.
// Commented out, COW as policy now.
//if (c_str() == rhs.c_str())
// return 0;
const size_type thisLen = length();
const size_type rhsLen = rhs.length();
if (thisLen < rhsLen)
return -1;
if (thisLen > rhsLen)
return 1;
return strcompare(c_str(), rhs.c_str(), thisLen);
}
// @note: do NOT const_cast!
const value_type* c_str() const { RDE_ASSERT(invariant()); return TStorage::c_str(); }
const_iterator begin() const { RDE_ASSERT(invariant()); return c_str(); }
const_iterator end() const { RDE_ASSERT(invariant()); return c_str() + length(); }
size_type length() const { return TStorage::length(); }
bool empty() const { return length() == 0; }
const allocator_type& get_allocator() const { return TStorage::get_allocator; }
value_type* reserve(size_type capacity_hint)
{
return TStorage::reserve(capacity_hint);
}
void clear()
{
TStorage::clear();
}
void resize(size_type size)
{
TStorage::resize(size);
}
void make_lower()
{
const size_type len = length();
TStorage::make_unique(len);
static const int chDelta = 'a' - 'A';
E* data = TStorage::get_data();
for (size_type i = 0; i < len; ++i)
{
if (data[i] < 'a')
data[i] += chDelta;
}
}
void make_upper()
{
const size_type len = length();
TStorage::make_unique(len);
static const int chDelta = 'a' - 'A';
E* data = TStorage::get_data();
for (size_type i = 0; i < len; ++i)
{
if (data[i] > 'Z')
data[i] -= chDelta;
}
}
size_type find_index_of(const value_type ch) const
{
size_type retIndex(basic_string::npos);
const E* ptr = c_str();
size_type currentIndex(0);
while (*ptr != value_type(0))
{
if (*ptr == ch)
{
retIndex = currentIndex;
break;
}
++ptr;
++currentIndex;
}
return retIndex;
}
size_type find_index_of_last(const value_type ch) const
{
size_type retIndex(basic_string::npos);
const value_type* ptr = c_str();
size_type currentIndex(0);
while (*ptr != value_type(0))
{
if (*ptr == ch)
retIndex = currentIndex;
++ptr;
++currentIndex;
}
return retIndex;
}
size_type find(const value_type* needle) const
{
const value_type* s(c_str());
size_type si(0);
while (*s)
{
const value_type* n = needle;
if (*s == *n) //first character matches
{
//go through the sequence, and make sure while(x) x == n for all of n
const value_type* x = s;
size_type match = 0;
while (*x && *n)
{
if (*n == *x)
++match;
++n;
++x;
}
if (match == strlen(needle))
return si;
}
++s;
++si;
}
return basic_string::npos;
}
size_type rfind(const value_type* needle) const
{
const value_type* s(c_str() + length());
size_type si(length() + 1);
//find the last index of the first char in needle
//searching from end->start for obvious reasons
while (--si != basic_string::npos)
{
//if the first character matches, run our check
if (*s-- == *needle) {
//go through the sequence, and make sure while(x) x == n for all of n
const value_type* x = c_str() + si;
const value_type* n = needle;
size_type match = 0;
while (*x && *n)
{
if (*n == *x)
++match;
++n;
++x;
}
if (match == strlen(needle))
return si;
}
}
return basic_string::npos;
}
private:
bool invariant() const
{
return TStorage::invariant();
}
};
//-----------------------------------------------------------------------------
template<typename E, class TStorage, class TAllocator>
bool operator==(const basic_string<E, TStorage, TAllocator>& lhs,
const basic_string<E, TStorage, TAllocator>& rhs)
{
return lhs.compare(rhs) == 0;
}
//-----------------------------------------------------------------------------
template<typename E, class TStorage, class TAllocator>
bool operator!=(const basic_string<E, TStorage, TAllocator>& lhs,
const basic_string<E, TStorage, TAllocator>& rhs)
{
return !(lhs == rhs);
}
//-----------------------------------------------------------------------------
template<typename E, class TStorage, class TAllocator>
bool operator<(const basic_string<E, TStorage, TAllocator>& lhs,
const basic_string<E, TStorage, TAllocator>& rhs)
{
return lhs.compare(rhs) < 0;
}
//-----------------------------------------------------------------------------
template<typename E, class TStorage, class TAllocator>
bool operator>(const basic_string<E, TStorage, TAllocator>& lhs,
const basic_string<E, TStorage, TAllocator>& rhs)
{
return lhs.compare(rhs) > 0;
}
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_BASIC_STRING_H