-
Notifications
You must be signed in to change notification settings - Fork 2
/
CUtlString.cpp
310 lines (245 loc) · 7.1 KB
/
CUtlString.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
#define NOMINMAX
#include <Windows.h>
#include <stdio.h>
#include "CUtlString.hpp"
//-----------------------------------------------------------------------------
// Base class, containing simple memory management
//-----------------------------------------------------------------------------
CUtlBinaryBlock::CUtlBinaryBlock(int growSize, int initSize) : m_Memory(growSize, initSize)
{
m_nActualLength = 0;
}
CUtlBinaryBlock::CUtlBinaryBlock(void* pMemory, int nSizeInBytes, int nInitialLength) : m_Memory((unsigned char*)pMemory, nSizeInBytes)
{
m_nActualLength = nInitialLength;
}
CUtlBinaryBlock::CUtlBinaryBlock(const void* pMemory, int nSizeInBytes) : m_Memory((const unsigned char*)pMemory, nSizeInBytes)
{
m_nActualLength = nSizeInBytes;
}
CUtlBinaryBlock::CUtlBinaryBlock(const CUtlBinaryBlock& src)
{
Set(src.Get(), src.Length());
}
void CUtlBinaryBlock::Get(void* pValue, int nLen) const
{
assert(nLen > 0);
if (m_nActualLength < nLen) {
nLen = m_nActualLength;
}
if (nLen > 0) {
memcpy(pValue, m_Memory.Base(), nLen);
}
}
void CUtlBinaryBlock::SetLength(int nLength)
{
assert(!m_Memory.IsReadOnly());
m_nActualLength = nLength;
if (nLength > m_Memory.NumAllocated()) {
int nOverFlow = nLength - m_Memory.NumAllocated();
m_Memory.Grow(nOverFlow);
// If the reallocation failed, clamp length
if (nLength > m_Memory.NumAllocated()) {
m_nActualLength = m_Memory.NumAllocated();
}
}
#ifdef _DEBUG
if (m_Memory.NumAllocated() > m_nActualLength) {
memset(((char*)m_Memory.Base()) + m_nActualLength, 0xEB, m_Memory.NumAllocated() - m_nActualLength);
}
#endif
}
void CUtlBinaryBlock::Set(const void* pValue, int nLen)
{
assert(!m_Memory.IsReadOnly());
if (!pValue) {
nLen = 0;
}
SetLength(nLen);
if (m_nActualLength) {
if (((const char*)m_Memory.Base()) >= ((const char*)pValue) + nLen ||
((const char*)m_Memory.Base()) + m_nActualLength <= ((const char*)pValue)) {
memcpy(m_Memory.Base(), pValue, m_nActualLength);
}
else {
memmove(m_Memory.Base(), pValue, m_nActualLength);
}
}
}
CUtlBinaryBlock& CUtlBinaryBlock::operator=(const CUtlBinaryBlock& src)
{
assert(!m_Memory.IsReadOnly());
Set(src.Get(), src.Length());
return *this;
}
bool CUtlBinaryBlock::operator==(const CUtlBinaryBlock& src) const
{
if (src.Length() != Length())
return false;
return !memcmp(src.Get(), Get(), Length());
}
//-----------------------------------------------------------------------------
// Simple string class.
//-----------------------------------------------------------------------------
CUtlString::CUtlString()
{
}
CUtlString::CUtlString(const char* pString)
{
Set(pString);
}
CUtlString::CUtlString(const CUtlString& string)
{
Set(string.Get());
}
// Attaches the string to external memory. Useful for avoiding a copy
CUtlString::CUtlString(void* pMemory, int nSizeInBytes, int nInitialLength) : m_Storage(pMemory, nSizeInBytes, nInitialLength)
{
}
CUtlString::CUtlString(const void* pMemory, int nSizeInBytes) : m_Storage(pMemory, nSizeInBytes)
{
}
void CUtlString::Set(const char* pValue)
{
assert(!m_Storage.IsReadOnly());
int nLen = pValue ? strlen(pValue) + 1 : 0;
m_Storage.Set(pValue, nLen);
}
// Returns strlen
int CUtlString::Length() const
{
return m_Storage.Length() ? m_Storage.Length() - 1 : 0;
}
// Sets the length (used to serialize into the buffer )
void CUtlString::SetLength(int nLen)
{
assert(!m_Storage.IsReadOnly());
// Add 1 to account for the NULL
m_Storage.SetLength(nLen > 0 ? nLen + 1 : 0);
}
const char* CUtlString::Get() const
{
if (m_Storage.Length() == 0) {
return "";
}
return reinterpret_cast<const char*>(m_Storage.Get());
}
// Converts to c-strings
CUtlString::operator const char* () const
{
return Get();
}
char* CUtlString::Get()
{
assert(!m_Storage.IsReadOnly());
if (m_Storage.Length() == 0) {
// In general, we optimise away small mallocs for empty strings
// but if you ask for the non-const bytes, they must be writable
// so we can't return "" here, like we do for the const version - jd
m_Storage.SetLength(1);
m_Storage[0] = '\0';
}
return reinterpret_cast<char*>(m_Storage.Get());
}
CUtlString& CUtlString::operator=(const CUtlString& src)
{
assert(!m_Storage.IsReadOnly());
m_Storage = src.m_Storage;
return *this;
}
CUtlString& CUtlString::operator=(const char* src)
{
assert(!m_Storage.IsReadOnly());
Set(src);
return *this;
}
bool CUtlString::operator==(const CUtlString& src) const
{
return m_Storage == src.m_Storage;
}
bool CUtlString::operator==(const char* src) const
{
return (strcmp(Get(), src) == 0);
}
CUtlString& CUtlString::operator+=(const CUtlString& rhs)
{
assert(!m_Storage.IsReadOnly());
const int lhsLength(Length());
const int rhsLength(rhs.Length());
const int requestedLength(lhsLength + rhsLength);
SetLength(requestedLength);
const int allocatedLength(Length());
const int copyLength(allocatedLength - lhsLength < rhsLength ? allocatedLength - lhsLength : rhsLength);
memcpy(Get() + lhsLength, rhs.Get(), copyLength);
m_Storage[allocatedLength] = '\0';
return *this;
}
CUtlString& CUtlString::operator+=(const char* rhs)
{
assert(!m_Storage.IsReadOnly());
const int lhsLength(Length());
const int rhsLength(strlen(rhs));
const int requestedLength(lhsLength + rhsLength);
SetLength(requestedLength);
const int allocatedLength(Length());
const int copyLength(allocatedLength - lhsLength < rhsLength ? allocatedLength - lhsLength : rhsLength);
memcpy(Get() + lhsLength, rhs, copyLength);
m_Storage[allocatedLength] = '\0';
return *this;
}
CUtlString& CUtlString::operator+=(char c)
{
assert(!m_Storage.IsReadOnly());
int nLength = Length();
SetLength(nLength + 1);
m_Storage[nLength] = c;
m_Storage[nLength + 1] = '\0';
return *this;
}
CUtlString& CUtlString::operator+=(int rhs)
{
assert(!m_Storage.IsReadOnly());
assert(sizeof(rhs) == 4);
char tmpBuf[12]; // Sufficient for a signed 32 bit integer [ -2147483648 to +2147483647 ]
snprintf(tmpBuf, sizeof(tmpBuf), "%d", rhs);
tmpBuf[sizeof(tmpBuf) - 1] = '\0';
return operator+=(tmpBuf);
}
CUtlString& CUtlString::operator+=(double rhs)
{
assert(!m_Storage.IsReadOnly());
char tmpBuf[256]; // How big can doubles be??? Dunno.
snprintf(tmpBuf, sizeof(tmpBuf), "%lg", rhs);
tmpBuf[sizeof(tmpBuf) - 1] = '\0';
return operator+=(tmpBuf);
}
int CUtlString::Format(const char* pFormat, ...)
{
assert(!m_Storage.IsReadOnly());
char tmpBuf[4096]; //< Nice big 4k buffer, as much memory as my first computer had, a Radio Shack Color Computer
va_list marker;
va_start(marker, pFormat);
int len = _vsnprintf_s(tmpBuf, 4096, sizeof(tmpBuf) - 1, pFormat, marker);
va_end(marker);
// Len < 0 represents an overflow
if (len < 0) {
len = sizeof(tmpBuf) - 1;
tmpBuf[sizeof(tmpBuf) - 1] = 0;
}
Set(tmpBuf);
return len;
}
//-----------------------------------------------------------------------------
// Strips the trailing slash
//-----------------------------------------------------------------------------
void CUtlString::StripTrailingSlash()
{
if (IsEmpty())
return;
int nLastChar = Length() - 1;
char c = m_Storage[nLastChar];
if (c == '\\' || c == '/') {
m_Storage[nLastChar] = 0;
m_Storage.SetLength(m_Storage.Length() - 1);
}
}