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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 | #include "fsStr.h"
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fsCore/src/debug/fsAssert.h>
const fsStr fsStr::mLineBreakStr = "<br>";
const fsStr fsStr::mLessThanStr = "<";
const fsStr fsStr::mGreaterThanStr = ">";
const fsStr fsStr::mLessThanCode = "<";
const fsStr fsStr::mGreaterThanCode = ">";
const fsStr fsStr::mAmpersandCode = "&";
const fsStr fsStr::mQuoteCode = """;
const fsStr fsStr::mAposCode = "'";
const fsStr fsStr::mHardSpaceCode = " ";
const fsStr fsStr::mHyphenCode = "-";
const fsU8* fsStr::ptrGet() const
{
return reinterpret_cast<const fsU8*>(mImpl.c_str());
}
fsStr::fsStr(const char* data, sizeT len)
{
copy(data, 0, static_cast<int>(len));
}
fsS32 fsStr::count(fsChar pChar) const
{
fsS32 count = 0;
const char* p = utf8Get();
char c = *p++;
while (c != 0)
{
if (pChar == c)
{
++count;
}
c = *p++;
}
return count;
}
void fsStr::formattingRemove()
{
hTMLCodesReplace();
removeXmlTags(); // Do this one last as it is indiscriminate
}
namespace
{
// Rich-text tags (axmol's ax::ui::RichText::createWithXML syntax) are left in place by
// removeXmlTags() rather than stripped, so bold/italic/colour markup survives localised
// string loading. Everything else keeps the original indiscriminate-strip behaviour.
fsBool xmlTagIsRichTextGet(const std::string& pTagInner)
{
const size_t nameStart = (!pTagInner.empty() && pTagInner[0] == '/') ? 1 : 0;
const size_t nameEnd = pTagInner.find_first_of(" \t", nameStart);
const std::string name = pTagInner.substr(nameStart, nameEnd == std::string::npos ? std::string::npos : nameEnd - nameStart);
return name == "b" || name == "i" || name == "font";
}
}
void fsStr::removeXmlTags()
{
sizeT xmlStart = mImpl.find(mLessThanStr.mImpl);
while (xmlStart != npos)
{
sizeT xmlEnd = mImpl.find(mGreaterThanStr.mImpl, xmlStart);
if (xmlEnd != npos)
{
if (xmlTagIsRichTextGet(mImpl.substr(xmlStart + 1, xmlEnd - xmlStart - 1)))
{
xmlStart = mImpl.find(mLessThanStr.mImpl, xmlEnd + mGreaterThanStr.dataSizeGet());
}
else
{
mImpl = mImpl.erase(xmlStart, xmlEnd + mGreaterThanStr.dataSizeGet() - xmlStart);
xmlStart = mImpl.find(mLessThanStr.mImpl);
}
}
else
{
fsLogError("XML tag ERROR in the string: %s", mImpl.c_str());
break;
}
}
}
void fsStr::hTMLCodesReplace()
{
replaceAll("%%", "%");
replaceAll(mLessThanCode, "<");
replaceAll(mGreaterThanCode, ">");
replaceAll(mLineBreakStr, "\n");
if (0 == find(mAmpersandCode))
{
erase(0, mAmpersandCode.dataSizeGet());
}
replaceAll(mHardSpaceCode, " ");
replaceAll(mHyphenCode, "-");
replaceAll(mAmpersandCode, "&");
replaceAll(mQuoteCode, "\"");
replaceAll(mAposCode, "\'");
}
fsBool fsStr::isDigit(fsChar pC)
{
return (pC >= '0' && pC <= '9');
}
fsStr operator+(const fsChar* pLHS, const fsStr& pRHS)
{
return fsStr(fsStr(pLHS) + pRHS);
}
fsStr operator+(const fsStr& pLHS, const fsChar* pRHS)
{
return fsStr(pLHS + fsStr(pRHS));
}
fsBool operator==(const fsChar* pLHS, const fsStr& pRhs)
{
return pRhs == pLHS;
}
fsBool operator!=(const fsChar* pLHS, const fsStr& pRhs)
{
return pRhs != pLHS;
}
fsStr& fsStr::operator+=(fsS32 pUnicode)
{
if ((pUnicode & 0xffffff80) == 0)
{
mImpl.operator+=(pUnicode);
return *this;
}
fsU8 byte = 0;
fsS32 numBytes = 4;
if (pUnicode < 0x800)
{
numBytes = 2;
}
else if (pUnicode < 0x10000)
{
numBytes = 3;
}
else
{
fsAssert(pUnicode < 0x200000, "Invalid Unicode value");
}
switch (numBytes)
{
case 2:
byte = 0xc0; // 110xxxxx
byte |= ((pUnicode & 0x07c0) >> 6); // ---- -xxx xx-- ----
mImpl.push_back(byte);
byte = 0x80; // 10xxxxxx
byte |= (pUnicode & 0x003f); // ---- ---- --xx xxxx
mImpl.push_back(byte);
break;
case 3:
byte = 0xe0; // 1110xxxx
byte |= ((pUnicode & 0xf000) >> 12); // xxxx ---- ---- ----
mImpl.push_back(byte);
byte = 0x80; // 10xxxxxx
byte |= ((pUnicode & 0x0fc0) >> 6); // ---- xxxx xx-- ----
mImpl.push_back(byte);
byte = 0x80; // 10xxxxxx
byte |= (pUnicode & 0x003f); // ---- ---- --xx xxxx
mImpl.push_back(byte);
break;
case 4:
byte = 0xf0; // 11110xxx
byte |= ((pUnicode & 0x1c0000) >> 18); // ---x xx-- ---- ---- ---- ----
mImpl.push_back(byte);
byte = 0x80; // 10xxxxxx
byte |= ((pUnicode & 0x03f000) >> 12); // ---- --xx xxxx ---- ---- ----
mImpl.push_back(byte);
byte = 0x80; // 10xxxxxx
byte |= ((pUnicode & 0x000fc0) >> 6); // ---- ---- ---- xxxx xx-- ----
mImpl.push_back(byte);
byte = 0x80; // 10xxxxxx
byte |= (pUnicode & 0x0000003f); // ---- ---- ---- ---- --xx xxxx
mImpl.push_back(byte);
break;
default:
fsAssert(false, "Something went wrong.");
break;
}
return *this;
}
fsStr::sizeT fsStr::lengthGet() const
{
sizeT ret = 0;
fsU32 currBytePos = 0;
while (mImpl[currBytePos] != 0)
{
const fsU8 currByte = mImpl[currBytePos++];
if ((currByte & 0x80) == 0) // Normal ASCII range
{
++ret;
}
else
{
fsU32 shift = 6;
while ((currByte & (1 << shift--)) != 0)
{
++currBytePos;
}
++ret;
}
}
return ret;
}
void fsStr::eraseToLast(const fsStr& pMatch)
{
sizeT matchPos = rfind(pMatch);
if (npos != matchPos)
{
erase(0, matchPos + pMatch.dataSizeGet());
}
}
void fsStr::eraseFromLast(const fsStr& pMatch)
{
sizeT matchPos = rfind(pMatch);
if (npos != matchPos)
{
erase(matchPos);
}
}
fsStr fsStr::chomp(const fsStr& pTerm)
{
const sizeT termLoc = this->find(pTerm);
if (npos == termLoc)
{
const fsStr bite(*this);
mImpl = "";
return bite;
}
fsStr bite;
bite.mImpl = mImpl.substr(0, termLoc);
this->erase(0, termLoc + pTerm.dataSizeGet());
if (pTerm == "\n")
{
if (!bite.emptyGet() && bite[bite.dataSizeGet() - 1] == '\r')
{
bite.erase(bite.dataSizeGet() - 1);
}
}
return bite;
}
fsStr fsStr::chunk()
{
leadingWhitespaceTrim();
while (dataSizeGet() > 0 && '#' == mImpl[0])
{
chomp("\n");
leadingWhitespaceTrim();
}
if (dataSizeGet() > 0 && '\"' == mImpl[0])
{
erase(0, 1);
fsAssert(npos != find( "\"" ), "Missing '\"' in string.\n");
return chomp("\"");
}
fsS32 numGoodChars = 0;
if (dataSizeGet() > 0)
{
while ('=' != mImpl[numGoodChars] &&
mSpaceChar != mImpl[numGoodChars] &&
mTabChar != mImpl[numGoodChars] &&
mNewLineChar != mImpl[numGoodChars] &&
mLineFeedChar != mImpl[numGoodChars] &&
0 != mImpl[numGoodChars])
{
++numGoodChars;
}
}
fsStr bite;
if (numGoodChars > 0)
{
bite.copy(utf8Get(), 0, numGoodChars);
erase(0, numGoodChars);
leadingWhitespaceTrim();
}
return bite;
}
void fsStr::leadingWhitespaceTrim()
{
fsS32 firstGoodChar = 0;
while (firstGoodChar < static_cast<fsS32>(dataSizeGet())
&& (mSpaceChar == mImpl[firstGoodChar] || mTabChar == mImpl[firstGoodChar] ||
mNewLineChar == mImpl[firstGoodChar] || mLineFeedChar == mImpl[firstGoodChar]))
{
++firstGoodChar;
}
if (firstGoodChar > 0)
{
erase(0, firstGoodChar);
}
}
void fsStr::trailingWhitespaceTrim()
{
fsS32 lastGoodChar = dataSizeGet() - 1;
while (lastGoodChar >= 0 && (mSpaceChar == mImpl[lastGoodChar] || mTabChar == mImpl[lastGoodChar] ||
mNewLineChar == mImpl[lastGoodChar] || mLineFeedChar == mImpl[lastGoodChar]))
{
--lastGoodChar;
}
if (lastGoodChar < (static_cast<fsS32>(dataSizeGet()) - 1))
{
erase(lastGoodChar + 1);
}
}
void fsStr::trimToLength(fsS32 pNumChars)
{
fsS32 charsToErase = lengthGet() - pNumChars;
fsS32 bytesToErase = 0;
while (charsToErase-- > 0)
{
++bytesToErase;
while ((mImpl[dataSizeGet() - bytesToErase] & 0xc0) == 0x80)
{
++bytesToErase;
}
}
if (bytesToErase > 0)
{
erase(dataSizeGet() - bytesToErase, bytesToErase);
}
}
void fsStr::copy(const char* pString)
{
copy(pString, 0, static_cast<int>(strlen(pString)));
}
void fsStr::copy(const char* pString, int pStart, int pCount)
{
if (pString == nullptr || pCount <= 0)
{
mImpl.clear();
return;
}
mImpl.assign(pString + pStart, static_cast<std::string::size_type>(pCount));
}
void fsStr::stringSet(const char* str, int len)
{
copy(str, 0, len);
}
fsBool fsStr::isAlphaNumeric() const
{
const char* p = utf8Get();
char c = *p++;
while (c != 0)
{
if (!std::isalnum(static_cast<unsigned char>(c)))
{
return false;
}
c = *p++;
}
return true;
}
void fsStr::downcase()
{
for (auto& c : mImpl)
{
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));<--- Consider using std::transform algorithm instead of a raw loop.
}
}
void fsStr::upcase()
{
for (auto& c : mImpl)
{
c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));<--- Consider using std::transform algorithm instead of a raw loop.
}
}
fsBool fsStr::compare(const char* pString, int len) const
{
const char* data = utf8Get();
return memcmp(data, pString, len) == 0;
}
fsF32 fsStr::toF32() const
{
return static_cast<fsF32>(strtod(mImpl.c_str(), nullptr));
}
fsS32 fsStr::toS32() const
{
return strtol(mImpl.c_str(), nullptr, 10);
}
fsU32 fsStr::toU32() const
{
return strtoul(mImpl.c_str(), nullptr, 10);
}
fsS64 fsStr::toS64() const
{
return strtoll(mImpl.c_str(), nullptr, 10);
}
void fsStr::replaceFirst(const fsStr& pFind, const fsStr& pReplace)
{
if (pFind.emptyGet())
{
return;
}
sizeT p = mImpl.find(pFind.mImpl);
if (p != npos)
{
mImpl.replace(p, pFind.dataSizeGet(), pReplace.mImpl);
}
}
void fsStr::replaceAll(const fsStr& pFind, const fsStr& pReplace)
{
if (pFind.emptyGet())
{
return;
}
sizeT p = mImpl.find(pFind.mImpl);
while (p != npos)
{
mImpl.replace(p, pFind.dataSizeGet(), pReplace.mImpl);
p = mImpl.find(pFind.mImpl, p + pReplace.dataSizeGet());
}
}
|