-
Notifications
You must be signed in to change notification settings - Fork 7
/
fixed_substring.h
275 lines (234 loc) · 9.61 KB
/
fixed_substring.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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_FIXED_SUBSTRING_H
#define EASTL_FIXED_SUBSTRING_H
#include <eastl/string.h>
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// fixedSubstring
///
/// Implements a string which is a reference to a segment of characters.
/// This class is efficient because it allocates no memory and copies no
/// memory during construction and assignment, but rather refers directly
/// to the segment of chracters. A common use of this is to have a
/// fixedSubstring efficiently refer to a substring within another string.
///
/// You cannot directly resize a fixedSubstring (e.g. via resize, insert,
/// append, erase), but you can assign a different substring to it.
/// You can modify the characters within a substring in place.
/// As of this writing, in the name of being lean and simple it is the
/// user's responsibility to not call unsupported resizing functions
/// such as those listed above. A detailed listing of the functions which
/// are not supported is given below in the class declaration.
///
/// The c_str function doesn't act as one might hope, as it simply
/// returns the pointer to the beginning of the string segment and the
/// 0-terminator may be beyond the end of the segment. If you want to
/// always be able to use c_str as expected, use the fixed string solution
/// we describe below.
///
/// Another use of fixedSubstring is to provide C++ string-like functionality
/// with a C character array. This allows you to work on a C character array
/// as if it were a C++ string as opposed using the C string API. Thus you
/// can do this:
///
/// void DoSomethingForUser(char* timeStr, size_t timeStrCapacity)
/// {
/// fixedSubstring tmp(timeStr, timeStrCapacity);
/// tmp = "hello ";
/// tmp += "world";
/// }
///
/// Note that this class constructs and assigns from const string pointers
/// and const string objects, yet this class does not declare its member
/// data as const. This is a concession in order to allow this implementation
/// to be simple and lean. It is the user's responsibility to make sure
/// that strings that should not or can not be modified are either not
/// used by fixedSubstring or are not modified by fixedSubstring.
///
/// A more flexible alternative to fixedSubstring is fixedString.
/// fixedString has none of the functional limitations that fixedSubstring
/// has and like fixedSubstring it doesn't allocate memory. However,
/// fixedString makes a *copy* of the source string and uses local
/// memory to store that copy. Also, fixedString objects on the stack
/// are going to have a limit as to their maximum size.
///
/// Notes:
/// As of this writing, the string class necessarily reallocates when
/// an insert of self is done into self. As a result, the fixedSubstring
/// class doesn't support inserting self into self.
///
/// Example usage:
/// basicString<char> str("hello world");
/// fixedSubstring<char> sub(str, 2, 5); // sub == "llo w"
///
template <typename T>
class fixedSubstring : public basicString<T>
{
public:
typedef basicString<T> base_type;
typedef fixedSubstring<T> this_type;
typedef typename base_type::size_type size_type;
typedef typename base_type::value_type value_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
using base_type::npos;
using base_type::getAllocator;
private:
using base_type::mPair;
using base_type::AllocateSelf;
using base_type::internalLayout;
void SetInternalHeapLayout(value_type* pBeginPtr, size_type nSize, size_type nCap)
{
internalLayout().SetHeapBeginPtr(pBeginPtr);
internalLayout().SetHeapSize(nSize);
internalLayout().SetHeapCapacity(nCap);
}
public:
fixedSubstring()
: base_type()
{
}
fixedSubstring(const fixedSubstring& x)
: fixedSubstring(static_cast<const base_type&>(x))
{}
fixedSubstring(const base_type& x)
: base_type()
{
#if EASTL_NAME_ENABLED
getAllocator().setName(x.getAllocator().getName());
#endif
assign(x);
}
// We gain no benefit from having an rvalue move constructor or assignment operator,
// as this class is a const class.
fixedSubstring(const base_type& x, size_type position, size_type n = base_type::npos)
: base_type()
{
#if EASTL_NAME_ENABLED
getAllocator().setName(x.getAllocator().getName());
#endif
assign(x, position, n);
}
fixedSubstring(const value_type* p, size_type n)
: base_type()
{
assign(p, n);
}
fixedSubstring(const value_type* p)
: base_type()
{
assign(p);
}
fixedSubstring(const value_type* pBegin, const value_type* pEnd)
: base_type()
{
assign(pBegin, pEnd);
}
~fixedSubstring()
{
// We need to reset, as otherwise the parent destructor will
// attempt to free our memory.
AllocateSelf();
}
this_type& operator=(const this_type& x)
{
assign(x);
return *this;
}
this_type& operator=(const base_type& x)
{
assign(x);
return *this;
}
this_type& operator=(const value_type* p)
{
assign(p);
return *this;
}
this_type& assign(const base_type& x)
{
// By design, we need to cast away const-ness here.
SetInternalHeapLayout(const_cast<value_type*>(x.data()), x.size(), x.size());
return *this;
}
this_type& assign(const base_type& x, size_type position, size_type n)
{
// By design, we need to cast away const-ness here.
SetInternalHeapLayout(const_cast<value_type*>(x.data()) + position, n, n);
return *this;
}
this_type& assign(const value_type* p, size_type n)
{
// By design, we need to cast away const-ness here.
SetInternalHeapLayout(const_cast<value_type*>(p), n, n);
return *this;
}
this_type& assign(const value_type* p)
{
// By design, we need to cast away const-ness here.
SetInternalHeapLayout(const_cast<value_type*>(p), (size_type)CharStrlen(p), (size_type)CharStrlen(p));
return *this;
}
this_type& assign(const value_type* pBegin, const value_type* pEnd)
{
// By design, we need to cast away const-ness here.
SetInternalHeapLayout(const_cast<value_type*>(pBegin), (size_type)(pEnd - pBegin), (size_type)(pEnd - pBegin));
return *this;
}
// Partially supported functionality
//
// When using fixedSubstring on a character sequence that is within another
// string, the following functions may do one of two things:
// 1 Attempt to reallocate
// 2 Write a 0 char at the end of the fixedSubstring
//
// Item #1 will result in a crash, due to the attempt by the underlying
// string class to free the substring memory. Item #2 will result in a 0
// char being written to the character array. Item #2 may or may not be
// a problem, depending on how you use fixedSubstring. Thus the following
// functions cannot be used safely.
#if 0 // !defined(EA_COMPILER_NO_DELETED_FUNCTIONS) We may want to enable these deletions after some investigation of possible user impact.
this_type& operator=(value_type c) = delete;
void resize(size_type n, value_type c) = delete;
void resize(size_type n) = delete;
void reserve(size_type = 0) = delete;
void setCapacity(size_type n) = delete;
void clear() = delete;
this_type& operator+=(const base_type& x) = delete;
this_type& operator+=(const value_type* p) = delete;
this_type& operator+=(value_type c) = delete;
this_type& append(const base_type& x) = delete;
this_type& append(const base_type& x, size_type position, size_type n) = delete;
this_type& append(const value_type* p, size_type n) = delete;
this_type& append(const value_type* p) = delete;
this_type& append(size_type n) = delete;
this_type& append(size_type n, value_type c) = delete;
this_type& append(const value_type* pBegin, const value_type* pEnd) = delete;
this_type& appendSprintfVaList(const value_type* pFormat, va_list arguments) = delete;
this_type& appendSprintf(const value_type* pFormat, ...) = delete;
void pushBack(value_type c) = delete;
void popBack() = delete;
this_type& assign(size_type n, value_type c) = delete;
this_type& insert(size_type position, const base_type& x) = delete;
this_type& insert(size_type position, const base_type& x, size_type beg, size_type n) = delete;
this_type& insert(size_type position, const value_type* p, size_type n) = delete;
this_type& insert(size_type position, const value_type* p) = delete;
this_type& insert(size_type position, size_type n, value_type c) = delete;
iterator insert(const_iterator p, value_type c) = delete;
void insert(const_iterator p, size_type n, value_type c) = delete;
void insert(const_iterator p, const value_type* pBegin, const value_type* pEnd) = delete;
this_type& erase(size_type position = 0, size_type n = npos) = delete;
iterator erase(const_iterator p) = delete;
iterator erase(const_iterator pBegin, const_iterator pEnd) = delete;
void swap(base_type& x) = delete;
this_type& sprintfVaList(const value_type* pFormat, va_list arguments) = delete;
this_type& sprintf(const value_type* pFormat, ...) = delete;
#endif
}; // fixedSubstring
} // namespace eastl
#endif // Header include guard