-
Notifications
You must be signed in to change notification settings - Fork 1
/
ELString.h
442 lines (375 loc) · 6.95 KB
/
ELString.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
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
437
438
439
440
441
442
#ifndef _ELSTRING_H_
#define _ELSTRING_H_
/*
Author: Brent Pease (embeddedlibraryfeedback@gmail.com)
The MIT License (MIT)
Copyright (c) 2015-FOREVER Brent Pease
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
A fixed length string buffer that can not be overflowed.
*/
#include "EL.h"
template <size_t inBufferSize> class TString
{
public:
TString(
)
{
Clear();
}
TString(
char const* inString,
...)
{
va_list vaList;
va_start(vaList, inString);
SetVA(inString, vaList);
va_end(vaList);
}
operator char*(
)
{
return buffer;
}
operator char const*(
)
{
return buffer;
}
bool
operator ==(
char const* inString)
{
return strcmp(buffer, inString) == 0;
}
TString<inBufferSize>&
operator =(
char const* inString)
{
Set(inString);
return *this;
}
TString<inBufferSize>&
operator +=(
char const* inString)
{
Append(inString);
return *this;
}
bool
operator +=(
char inC)
{
return Append(inC);
}
// Since overloading [] produces abmiguous overload warnings use () instead
char
operator()(
size_t inIndex)
{
if(inIndex >= length)
{
return -1;
}
return buffer[inIndex];
}
bool
AppendF(
char const* inString,
...)
{
va_list vaList;
va_start(vaList, inString);
bool result = AppendVA(inString, vaList);
va_end(vaList);
return result;
}
bool
Append(
char const* inString)
{
size_t strLen = strlen(inString);
if(length + strLen + 1 <= inBufferSize)
{
memcpy(buffer + length, inString, strLen + 1);
length += strLen;
return true;
}
memcpy(buffer + length, inString, inBufferSize - length - 1);
length = inBufferSize - 1;
buffer[inBufferSize - 1] = 0; // Ensure we have a valid cstring
return false;
}
bool
Append(
char inC)
{
if(length >= inBufferSize - 1)
{
return false;
}
buffer[length++] = inC;
buffer[length] = 0; // Ensure we always have a valid cstring
return true;
}
bool
AppendVA(
char const* inString,
va_list inVAList)
{
int result;
result = vsnprintf(buffer + length, inBufferSize - length, inString, inVAList);
if(result < 0 || (size_t)result >= inBufferSize - length)
{
// we have overflowed
length = inBufferSize - 1;
buffer[inBufferSize - 1] = 0; // Ensure we have a valid cstring
return false;
}
length += result;
return true;
}
bool
Insert(
size_t inPos,
char const* inString)
{
size_t strLen = strlen(inString);
if(inPos >= inBufferSize)
{
return false;
}
if(inPos + strLen >= inBufferSize)
{
length = inBufferSize - 1;
strLen = length - inPos;
memcpy(buffer + inPos, inString, strLen);
buffer[inBufferSize - 1] = 0; // Ensure we have a valid cstring
return false;
}
if(strLen + length >= inBufferSize)
{
length = inBufferSize - 1;
memmove(buffer + inPos + strLen, buffer + inPos, length - strLen - inPos);
memcpy(buffer + inPos, inString, strLen);
buffer[inBufferSize - 1] = 0; // Ensure we have a valid cstring
return false;
}
memmove(buffer + inPos + strLen, buffer + inPos, length - inPos);
memcpy(buffer + inPos, inString, strLen);
length += strLen;
buffer[length] = 0;
return true;
}
bool
SetF(
char const* inString,
...)
{
va_list vaList;
va_start(vaList, inString);
bool result = SetVA(inString, vaList);
va_end(vaList);
return result;
}
bool
Set(
char const* inString)
{
if(inString == NULL)
{
buffer[0] = 0;
length = 0;
return true;
}
size_t strLen = strlen(inString);
bool overflow = strLen > inBufferSize - 1;
if(overflow)
{
strLen = inBufferSize - 1;
}
memcpy(buffer, inString, strLen);
buffer[strLen] = 0;
length = strLen;
return overflow;
}
bool
SetVA(
char const* inString,
va_list inVAList)
{
int result;
result = vsnprintf(buffer, inBufferSize, inString, inVAList);
if(result < 0 || (size_t)result >= inBufferSize)
{
// we have overflowed
length = inBufferSize - 1;
buffer[inBufferSize - 1] = 0; // Ensure we have a valid cstring
return false;
}
length = result;
return true;
}
void
Clear(
void)
{
length = 0;
buffer[0] = 0;
}
size_t
GetLength(
void)
{
return length;
}
size_t
GetSize(
void)
{
return inBufferSize - 1;
}
bool
StartsWith(
char const* inString)
{
size_t strLen = strlen(inString);
return strncmp(buffer, inString, strLen) == 0;
}
bool
EndsWith(
char inChar)
{
return buffer[length - 1] == inChar;
}
bool
TrimAfterChr(
char inC)
{
char* charPtr = strchr(buffer, inC);
if(charPtr == NULL)
{
return false;
}
*charPtr = 0;
length = strlen(buffer);
return true;
}
bool
TrimBeforeChr(
char inC)
{
char* charPtr = strchr(buffer, inC);
if(charPtr == NULL)
{
return false;
}
length = buffer + length - charPtr;
memmove(buffer, charPtr + 1, length + 1);
return true;
}
void
TrimStartingSpace(
void)
{
char* nonSpaceStr = buffer;
while(isspace(*nonSpaceStr))
{
++nonSpaceStr;
}
length = buffer + length - nonSpaceStr;
memmove(buffer, nonSpaceStr, length + 1);
}
void
TrimUntilNextWord(
void)
{
char* sp = buffer;
while(!isspace(*sp))
{
++sp;
}
while(isspace(*sp))
{
++sp;
}
length = buffer + length - sp;
memmove(buffer, sp, length + 1);
}
bool
SetChar(
size_t inPos,
char inC)
{
if(inPos >= length)
{
return false;
}
buffer[inPos] = inC;
return true;
}
bool
Contains(
char const* inString)
{
return strstr(buffer, inString) != NULL;
}
bool
IsSpace(
void)
{
for(size_t i = 0; i < length; ++i)
{
if(!isspace(buffer[i]))
{
return false;
}
}
return true;
}
bool
IsDigit(
void)
{
for(size_t i = 0; i < length; ++i)
{
if(!isdigit(buffer[i]))
{
return false;
}
}
return true;
}
bool
IsNumber(
void)
{
for(size_t i = 0; i < length; ++i)
{
if(!(isdigit(buffer[i]) || buffer[i] == '-' || buffer[i] == '+'))
{
return false;
}
}
return true;
}
private:
char buffer[inBufferSize];
size_t length;
};
#endif /* _ELSTRING_H_ */