-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstrings.cpp
436 lines (380 loc) · 10.9 KB
/
strings.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
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
#include "general.h"
#include "strings.h"
char StringClass::TempStrings[MAX_TEMP_STRING][MAX_TEMP_BYTES] = {};
unsigned int StringClass::FreeTempStrings = 0xFF;
char WideStringClass::TempStrings[MAX_TEMP_STRING][MAX_TEMP_BYTES] = {};
unsigned int WideStringClass::FreeTempStrings = 0xF;
char StringClass::m_NullChar = 0;
char *StringClass::m_EmptyString = &m_NullChar;
wchar_t WideStringClass::m_NullChar = 0;
wchar_t *WideStringClass::m_EmptyString = &m_NullChar;
int __cdecl StringClass::Format(const char* format,...)
{
va_list arg_list;
va_start(arg_list,format);
char temp_buffer[512];
int x = vsnprintf(temp_buffer,512,format,arg_list);
*this = (const char *)temp_buffer;
va_end(arg_list);
return x;
}
int __cdecl StringClass::Format_Args(const char* format,const va_list& arg_list)
{
char temp_buffer[512];
int x = vsnprintf(temp_buffer,512,format,arg_list);
*this = (const char *)temp_buffer;
return x;
}
void StringClass::Get_String(int length, bool is_temp)
{
if (!is_temp && !length) Set_Buffer_And_Allocated_Length(m_EmptyString, 0);
else if (is_temp && length < MAX_TEMP_LEN && FreeTempStrings)
{
uint32 index = 0;
BitScanForward((DWORD*)&index, FreeTempStrings); // Find the first free temp string
FreeTempStrings &= ~(1 << index); // Remove it from the free pool
char* buffer = TempStrings[index] + sizeof(_HEADER);
Set_Buffer_And_Allocated_Length(buffer, MAX_TEMP_LEN);
}
else if (length > 0) Set_Buffer_And_Allocated_Length(Allocate_Buffer(length), length);
else Free_String();
}
void StringClass::Resize(int new_len)
{
if (new_len > Get_Allocated_Length())
{
char *x = Allocate_Buffer(new_len);
strcpy(x,m_Buffer);
Free_String();
Set_Buffer_And_Allocated_Length(x,new_len);
}
}
void StringClass::Uninitialised_Grow(int new_len)
{
if (new_len > Get_Allocated_Length())
{
char *x = Allocate_Buffer(new_len);
Free_String();
Set_Buffer_And_Allocated_Length(x,new_len);
}
Store_Length(0);
}
void StringClass::Free_String()
{
if (m_Buffer == m_EmptyString) return;
ptrdiff_t buffer_base = intptr_t(m_Buffer) - sizeof(_HEADER);
ptrdiff_t diff = buffer_base - intptr_t(TempStrings[0]);
if (diff >= 0 && diff < MAX_TEMP_BYTES * MAX_TEMP_STRING)
{
// It was a temp string, let's get the index and cast Undead.
ptrdiff_t index = diff / MAX_TEMP_BYTES;
m_Buffer[0] = m_NullChar;
FreeTempStrings |= 1 << index;
}
else
{
char* buffer = (char*)buffer_base;
delete[] buffer;
}
m_Buffer = m_EmptyString;
}
void StringClass::Release_Resources()
{
}
int StringClass::Replace(const char* search, const char* replace, bool bCaseSensitive, int maxCount)
{
if (m_Buffer == m_EmptyString) return 0;
int nReplacements = 0;
char* newstring = NULL; // The modified string, NULL until we make a change
int newstringlen = Get_Length()+1; // The length of the modified string
const char* searchPtr = m_Buffer; // The starting point for the next search, always lastreplacement+1
// Figure out lengths in advance to avoid doing it repeatedly
int searchlen = strlen(search);
int replacelen = strlen(replace);
while ( NULL != searchPtr && (-1 == maxCount || nReplacements < maxCount) )
{
// Find the next instance of the search string
const char* foundPtr = ( bCaseSensitive ) ? stristr(searchPtr,search) : strstr(searchPtr,search);
searchPtr = NULL;
if ( NULL != foundPtr )
{
// Figure out the 0-based index of the first character to be replaced
int replaceindex = (int)foundPtr - (int)((NULL==newstring)?m_Buffer:newstring);
// Allocate a new string to fit the replacement data if necessary
if ( searchlen != replacelen || NULL == newstring )
{
// Cache old data so we can clean up memory
int oldnewstringlen = newstringlen;
char* oldnewstring = newstring;
newstringlen = oldnewstringlen + (replacelen-searchlen);
newstring = new char[newstringlen];
// Copy characters preceeding the string to be replaced
memcpy(newstring, (NULL==oldnewstring)?m_Buffer:oldnewstring, replaceindex);
// Copy characters following the string to be replaced
int postsearchindex = replaceindex+searchlen;
int postreplaceindex = replaceindex+replacelen;
memcpy(newstring+postreplaceindex, ((NULL==oldnewstring)?m_Buffer:oldnewstring)+postsearchindex, oldnewstringlen-postsearchindex);
delete [] oldnewstring;
}
// Copy in the replacement string in the location of the located search string
memcpy(newstring+replaceindex, replace, replacelen);
// Update the search pointer
searchPtr = newstring + replaceindex + replacelen;
nReplacements++;
}
}
// Update the string with the modified version, if any
if ( NULL != newstring )
{
*this = newstring;
delete [] newstring;
}
return nReplacements;
}
bool StringClass::Copy_Wide(const wchar_t *str)
{
if (str)
{
mbstate_t ps;
memset(&ps,0,sizeof(ps));
int len = (int)wcsrtombs(0,&str,0,&ps);
if (len > 0)
{
Uninitialised_Grow(len+1);
wcsrtombs(m_Buffer,&str,len,&ps);
m_Buffer[len] = 0;
Store_Length(len);
return true;
}
return false;
}
return false;
}
int __cdecl WideStringClass::Format(const wchar_t* format,...)
{
if (format == NULL)
{
return 0;
}
va_list arg_list;
va_start(arg_list,format);
wchar_t temp_buffer[512];
int x = _vsnwprintf(temp_buffer,512,format,arg_list);
*this = temp_buffer;
va_end(arg_list);
return x;
}
int __cdecl WideStringClass::Format_Args(const wchar_t* format,const va_list& arg_list)
{
if (format == NULL)
{
return 0;
}
wchar_t temp_buffer[512];
int x = _vsnwprintf(temp_buffer,512,format,arg_list);
*this = temp_buffer;
return x;
}
void WideStringClass::Get_String(int length,bool is_temp)
{
if (!is_temp && !length) Set_Buffer_And_Allocated_Length(m_EmptyString, 0);
else if (is_temp && length < MAX_TEMP_LEN && FreeTempStrings)
{
uint32 index = 0;
BitScanForward((DWORD*)&index, FreeTempStrings); // Find the first free temp string
FreeTempStrings &= ~(1 << index); // Remove it from the free pool
wchar_t* buffer = (wchar_t*)(TempStrings[index] + sizeof(_HEADER));
Set_Buffer_And_Allocated_Length(buffer, MAX_TEMP_LEN);
}
else if (length > 0) Set_Buffer_And_Allocated_Length(Allocate_Buffer(length), length);
else Free_String();
}
void WideStringClass::Resize(int new_len)
{
if (new_len > Get_Allocated_Length())
{
wchar_t *x = Allocate_Buffer(new_len);
wcscpy(x,m_Buffer);
Free_String();
Set_Buffer_And_Allocated_Length(x,new_len);
}
}
void WideStringClass::Uninitialised_Grow(int new_len)
{
if (new_len > Get_Allocated_Length())
{
wchar_t *x = Allocate_Buffer(new_len);
Free_String();
Set_Buffer_And_Allocated_Length(x,new_len);
}
Store_Length(0);
}
void WideStringClass::Free_String()
{
if (m_Buffer == m_EmptyString) return;
ptrdiff_t buffer_base = intptr_t(m_Buffer) - sizeof(_HEADER);
ptrdiff_t diff = buffer_base - intptr_t(TempStrings[0]);
if (diff >= 0 && diff < MAX_TEMP_BYTES * MAX_TEMP_STRING)
{
// It was a temp string, let's get the index and cast Undead.
ptrdiff_t index = diff / MAX_TEMP_BYTES;
m_Buffer[0] = m_NullChar;
FreeTempStrings |= 1 << index;
}
else
{
char* buffer = (char*)buffer_base;
delete[] buffer;
}
m_Buffer = m_EmptyString;
}
void WideStringClass::Release_Resources()
{
}
bool WideStringClass::Convert_From(const char *str)
{
if (str)
{
mbstate_t ps;
memset(&ps,0,sizeof(ps));
#pragma warning(suppress: 6387) // header entry of mbsrtowcs is incorrect, NULL is a valid first entry
int len = (int)mbsrtowcs(NULL,&str,0,&ps);
if (len >= 0)
{
Uninitialised_Grow(len+1);
mbsrtowcs(m_Buffer,&str,len,&ps);
m_Buffer[len] = 0;
Store_Length(len);
return true;
}
}
return false;
}
bool WideStringClass::Is_ANSI()
{
if (m_Buffer)
{
for (int i = 0;m_Buffer[i] != 0;i++)
{
unsigned short value = m_Buffer[i];
if (value > 255)
{
return false;
}
}
}
return true;
}
WideStringClass WideStringClass::Substring(int start, int length) const
{
UL_ASSERT(start + length <= Get_Length());
WideStringClass result;
result.Uninitialised_Grow(length+1);
result.Store_Length(length);
memcpy(result.m_Buffer, m_Buffer + start, length * sizeof(wchar_t));
result.m_Buffer[length] = L'\0';
return result;
}
void WideStringClass::RemoveSubstring(int start, int length)
{
if (length > 0)
{
int oldLength = Get_Length();
int newLength = oldLength - length;
UL_ASSERT(start + length <= oldLength);
memmove(m_Buffer + start, m_Buffer + start + length, (newLength - start) * sizeof(wchar_t));
m_Buffer[newLength] = L'\0';
Store_Length(newLength);
}
}
void WideStringClass::ReplaceSubstring(int start, int length, const WideStringClass& substring)
{
int substringLength = substring.Get_Length();
int oldLength = Get_Length();
int newLength = oldLength - length + substringLength;
UL_ASSERT(start + length <= oldLength);
if (substringLength > length)
Resize(newLength + 1);
memmove(m_Buffer + start + substringLength, m_Buffer + start + length, (oldLength - start - length) * sizeof(wchar_t));
memcpy(m_Buffer + start, substring.m_Buffer, substringLength * sizeof(wchar_t));
m_Buffer[newLength] = L'\0';
Store_Length(newLength);
}
const wchar_t *CharToWideChar(const char *str)
{
int length = (int)strlen(str);
wchar_t *text = new wchar_t[length+1];
mbstowcs(text,str,length+1);
return text;
}
const char *WideCharToChar(const wchar_t *wcs)
{
if (!wcs)
{
char *c = new char[2];
c[0] = 0;
c[1] = 0;
return c;
}
int length = (int)wcslen(wcs);
char *text = new char[length+1];
wcstombs(text,wcs,length+1);
return text;
}
char *newstr(const char *str)
{
size_t len = strlen(str)+1;
char *s = new char[len];
memcpy(s,str,len);
return s;
};
wchar_t *newwcs(const wchar_t *str)
{
size_t len = wcslen(str)+2;
wchar_t *s = new wchar_t[len];
memcpy(s,str,len*2);
return s;
};
char *strtrim(char *v)
{
if (v)
{
char *r = v;
while (*r > 0 && *r < 0x21)
r++;
strcpy(v,r);
r = v + strlen(v);
while (r > v && r[-1] > 0 && r[-1] < 0x21)
r--;
*r = 0;
}
return v;
}
char *strrtrim(char *s)
{
char *t, *tt;
UL_ASSERT(s != NULL);
for (tt = t = s; *t != '\0'; ++t)
if (!isspace(*(unsigned char *)t))
tt = t+1;
*tt = '\0';
return s;
}
const char *stristr(const char *str, const char *substr){
while (*str){
if (_strnicmp(str, substr, strlen(substr)) == 0)
return str;
str++;
}
return NULL;
}
const wchar_t *wcsistr(const wchar_t *str, const wchar_t *substr){
if (!*str)
return NULL;
while (*str){
if (_wcsnicmp(str, substr, wcslen(substr)) == 0)
return str;
str++;
}
return NULL;
}