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
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fud_string_view.hpp"
#include "fud_string.hpp"
namespace fud {
StringView::StringView(const String& fudString) noexcept : StringView{fudString.length(), fudString.data()}
{
}
bool StringView::nullTerminated() const
{
return m_data != nullptr && m_data[m_length] == '\0';
}
bool StringView::utf8Valid() const
{
if (m_data == nullptr) {
return false;
}
for (size_t idx = 0; idx < m_length;) {
if (Ascii::valid(m_data[idx])) {
idx++;
} else if (idx + 1 < m_length && Utf82Byte::valid(m_data[idx], m_data[idx + 1])) {
idx += 2;
} else if (idx + 2 < m_length && Utf83Byte::valid(m_data[idx], m_data[idx + 1], m_data[idx + 2])) {
idx += 3;
} else if (
idx + 3 < m_length && Utf84Byte::valid(m_data[idx], m_data[idx + 1], m_data[idx + 2], m_data[idx + 3])) {
idx += 4;
} else {
return false;
}
}
return true;
}
Result<size_t, FudStatus> StringView::skipWhitespace()
{
using RetType = Result<size_t, FudStatus>;
if (m_data == nullptr) {
return RetType::error(FudStatus::NullPointer);
}
size_t index = 0;
while (m_length > 0 && classify::isSpace(static_cast<char>(m_data[0]))) {
m_data++;
m_length--;
index++;
}
return RetType::okay(index);
}
Result<size_t, FudStatus> StringView::trimWhitespace()
{
using RetType = Result<size_t, FudStatus>;
if (m_data == nullptr) {
return RetType::error(FudStatus::NullPointer);
}
size_t count = 0;
while (m_length > 0 && classify::isSpace(static_cast<char>(m_data[m_length - 1]))) {
m_length--;
count++;
}
return RetType::okay(count);
}
bool StringView::advance()
{
if (m_length < 1) {
return false;
}
m_length--;
m_data++;
return true;
}
void StringView::advanceUnsafe()
{
fudAssert(m_length > 0);
m_length--;
m_data++;
}
bool StringView::advance(size_t size)
{
if (size > m_length) {
return false;
}
m_length -= size;
m_data += size;
return true;
}
void StringView::advanceUnsafe(size_t size)
{
fudAssert(size <= m_length);
m_length -= size;
m_data += size;
}
FudStatus skipWhitespace(StringView& view, size_t& skipIndex)
{
auto skipResult = view.skipWhitespace();
if (skipResult.isError()) {
return skipResult.getError();
}
skipIndex = skipResult.getOkay();
if (view.length() < 1) {
return FudStatus::ArgumentInvalid;
}
return FudStatus::Success;
}
#if 0
FudStatus fud_string_truncate(ExtBasicString* source, ssize_t newLength)
{
if (source == nullptr) {
return FudStatus::NullPointer;
}
StringBorrow wrapper{*source};
if (!wrapper.valid()) {
return FudStatus::StringInvalid;
}
if ((newLength > 0 && static_cast<size_t>(newLength) > source->m_length) ||
(static_cast<size_t>(-newLength) > source->m_length)) {
return FudStatus::ArgumentInvalid;
}
if (newLength < 0) {
source->m_length = source->m_length - static_cast<size_t>(-newLength);
} else {
source->m_length = static_cast<size_t>(newLength);
}
return wrapper.nullTerminate();
}
FudStatus fud_string_reverse(ExtBasicString* source)
{
if (source == nullptr || source->m_data == nullptr) {
return FudStatus::NullPointer;
}
return fud_string_reverse_substring(source, StringView{source->m_length, source->m_data});
}
FudStatus fud_string_reverse_substring(ExtBasicString* source, StringView subString)
{
auto dataOffset = subString.data - source->m_data;
if (dataOffset < 0 || static_cast<size_t>(dataOffset) > source->m_length) {
return FudStatus::ArgumentInvalid;
}
if (static_cast<size_t>(dataOffset) + subString.length > source->m_length) {
return FudStatus::ArgumentInvalid;
}
if (source == nullptr || source->m_data == nullptr) {
return FudStatus::NullPointer;
}
StringView view{subString};
size_t index = 0;
auto* data = source->m_data + dataOffset;
while (index < subString.length) {
if (fud_char_is_ascii(static_cast<char>(data[index]))) {
index++;
continue;
}
auto utf8 = FudUtf8::fromStringView(view, index);
if (!utf8.valid()) {
return ExtUtf8Invalid;
}
const auto* utf8Data = utf8.data();
if (utf8Data == nullptr) {
return ExtFailure;
}
auto utf8Size = utf8.size();
switch (utf8Size) {
case 2:
data[index] = utf8Data[1];
data[index + 1] = utf8Data[0];
break;
case 3:
data[index] = utf8Data[2];
data[index + 2] = utf8Data[0];
break;
case 4:
data[index] = utf8Data[3];
data[index + 1] = utf8Data[2];
data[index + 2] = utf8Data[1];
data[index + 3] = utf8Data[0];
break;
default:
return ExtFailure;
}
index += utf8Size;
}
DataView<uint8_t> dataView{subString.length, data};
reverse(dataView);
return FudStatus::Success;
}
FudStatus fud_string_compare(StringView levo, StringView dextro, int* difference)
{
if (anyAreNull(difference, levo.data, dextro.data)) {
return FudStatus::NullPointer;
}
int diff = 0;
size_t index = 0;
while (diff == 0 && index < levo.length && index < dextro.length) {
diff = levo.data[index] - dextro.data[index];
index++;
}
if (diff != 0 || levo.length == dextro.length) {
/* nothing to do */
} else if (levo.length > dextro.length) {
diff = static_cast<int>(levo.data[index]);
} else {
diff = -static_cast<int>(dextro.data[index]);
}
*difference = diff;
return FudStatus::Success;
}
FudStatus fud_string_chr(StringView extStringView, char character, size_t* index)
{
if (anyAreNull(extStringView.data, index)) {
return FudStatus::NullPointer;
}
bool found = false;
for (size_t localIndex = 0; localIndex < extStringView.length; ++localIndex) {
if (extStringView.data[localIndex] == static_cast<uint8_t>(character)) {
*index = localIndex;
found = true;
break;
}
}
if (found) {
return FudStatus::Success;
}
return ExtNotFound;
}
FudStatus fud_string_unicode_chr(StringView extString, const ExtUtf8* unicode, size_t* index)
{
if (anyAreNull(extString.data, unicode, index)) {
return FudStatus::NullPointer;
}
if (!unicode->valid()) {
return ExtUtf8Invalid;
}
size_t charSize = unicode->size();
ExtDebugAssert(charSize != 0);
const uint8_t* dataMem = unicode->data();
ExtDebugAssert(dataMem != nullptr);
Array<uint8_t, 4> localData{};
auto copyStatus = ExtCopyMem(localData.data(), localData.size(), dataMem, charSize);
ExtDebugAssert(copyStatus == FudStatus::Success);
for (size_t sIdx = 0; sIdx + charSize - 1 < extString.length;) {
auto localChar = FudUtf8::fromStringView(extString, sIdx);
if (!localChar.valid()) {
return ExtUtf8Invalid;
}
if (localChar.m_variant == unicode->m_variant) {
*index = sIdx;
return FudStatus::Success;
}
sIdx += localChar.size();
}
return ExtNotFound;
}
FudStatus fud_string_span_c_api(
const StringView& inputView,
const StringView& characterSetString,
StringView& result,
bool inSet)
{
size_t firstIndex = inputView.length;
size_t sIdx = 0;
while (sIdx < firstIndex) {
auto stringChar = FudUtf8::fromStringView(inputView, sIdx);
if (!stringChar.valid()) {
return ExtUtf8Invalid;
}
size_t cIdx = 0;
bool found = false;
while (firstIndex > 0 && cIdx < firstIndex && cIdx < characterSetString.length) {
auto setChar = FudUtf8::fromStringView(characterSetString, cIdx);
if (!setChar.valid()) {
return ExtUtf8Invalid;
}
if (stringChar == setChar) {
found = true;
}
cIdx += setChar.size();
}
if (!inSet && found) {
firstIndex = sIdx;
} else if (inSet && !found) {
if (sIdx > 0) {
firstIndex = sIdx;
}
break;
}
sIdx += stringChar.size();
}
if (firstIndex < inputView.length) {
result.length = inputView.length - firstIndex;
result.data = inputView.data + firstIndex;
return FudStatus::Success;
}
return ExtNotFound;
}
FudStatus fud_string_span_set(StringView inputView, const ExtUtf8Set* characterSet, StringView* stringView, bool inSet)
{
if (anyAreNull(inputView.data, characterSet, stringView)) {
return FudStatus::NullPointer;
}
if (!characterSet->valid()) {
return ExtUtf8Invalid;
}
size_t firstIndex = inputView.length;
size_t sIdx = 0;
while (sIdx < firstIndex) {
auto localChar = FudUtf8::fromStringView(inputView, sIdx);
if (!localChar.valid()) {
return ExtUtf8Invalid;
}
bool found = characterSet->contains(localChar);
if (!inSet && found) {
firstIndex = sIdx;
} else if (inSet && !found) {
if (sIdx > 0) {
firstIndex = sIdx;
}
break;
}
sIdx += localChar.size();
}
if (firstIndex < inputView.length) {
stringView->length = inputView.length - firstIndex;
stringView->data = inputView.data + firstIndex;
return FudStatus::Success;
}
return ExtNotFound;
}
FudStatus fud_string_span(StringView extString, StringView characterSetString, StringView* result)
{
if (result == nullptr) {
return FudStatus::NullPointer;
}
const StringView inputView{extString};
const StringView characterSet{characterSetString};
return fud_string_span_c_api(inputView, characterSet, *result, true);
}
FudStatus fud_string_c_span(StringView extString, StringView characterSetString, StringView* result)
{
if (result == nullptr) {
return FudStatus::NullPointer;
}
const StringView inputView{extString};
const StringView characterSet{characterSetString};
return fud_string_span_c_api(inputView, characterSet, *result, false);
}
FudStatus fud_string_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView)
{
return fud_string_span_set(extString, characterSet, stringView, true);
}
FudStatus fud_string_c_span_set(StringView extString, const ExtUtf8Set* characterSet, StringView* stringView)
{
return fud_string_span_set(extString, characterSet, stringView, false);
}
FudStatus fud_string_find_substring(StringView haystack, StringView needle, StringView* stringView)
{
if (anyAreNull(haystack.data, needle.data, stringView)) {
return FudStatus::NullPointer;
}
if (needle.length > haystack.length) {
return ExtNotFound;
}
if (needle.length == 1) {
size_t index = 0;
auto chrFindStatus = fud_string_chr(haystack, static_cast<char>(needle.data[0]), &index);
if (chrFindStatus == FudStatus::Success) {
stringView->data = haystack.data + index;
stringView->length = 1;
}
return chrFindStatus;
}
size_t haystackIdx = 0;
while (haystackIdx < haystack.length - needle.length) {
StringView lhs;
lhs.data = haystack.data + haystackIdx;
lhs.length = haystack.length - haystackIdx;
size_t lhsIndex = 0;
auto chrFindStatus = fud_string_chr(lhs, static_cast<char>(needle.data[0]), &lhsIndex);
if (chrFindStatus != FudStatus::Success) {
return chrFindStatus;
}
haystackIdx += lhsIndex;
// GE or GT?
if (haystackIdx + needle.length >= haystack.length) {
break;
}
lhs.data = haystack.data + haystackIdx;
lhs.length = needle.length;
int difference = -1;
auto cmpStatus = fud_string_compare(lhs, needle, &difference);
ExtDebugAssert(cmpStatus == FudStatus::Success);
if (difference == 0) {
stringView->data = lhs.data;
stringView->length = lhs.length;
return FudStatus::Success;
}
haystackIdx++;
}
return ExtNotFound;
}
#endif
} // namespace fud
|