-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringx.cpp
106 lines (86 loc) · 2.9 KB
/
stringx.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
#include "stringx.h"
char currentBuffer[16];
bool startsWith(const char* target, const char* text) {
int count = strlen(text);
for (int i = 0; i < count; i++) {
if (target[i] != text[i]) return false;
}
return true;
}
bool startsWith(SecuredString* target, SecuredString* text) {
int count = text->size();
for (int i = 0; i < count; i++) {
if (target->at(i) != text->at(i)) return false;
}
return true;
}
bool check(const char* target, const char* text) {
if (strlen(target) != strlen(text)) return false;
int count = strlen(text);
for (int i = 0; i < count; i++) {
if (target[i] != text[i]) return false;
}
return true;
}
bool check(SecuredString* target, SecuredString* text) {
if (target->size() != text->size()) return false;
int count = text->size();
for (int i = 0; i < count; i++) {
if (target->at(i) != text->at(i)) return false;
}
return true;
}
bool check(SecuredString* target, char* text) {
return check(target->c_str(), text);
}
SecuredStringList* split(SecuredStringList* sourceList, char* text, int textLength, char separator) {
int itemPtr = 0;
int textPtr = 0;
bool inQuotes = false;
SecuredStringList* list = sourceList == nullptr ? new SecuredStringList() : sourceList;
// Old behaviour : each element is cloned from the currentBuffer
for (int i = 0; i < textLength; i++) {
if (text[i] == separator && !inQuotes) {
currentBuffer[textPtr] = '\0';
list->copy(currentBuffer, textPtr);
memset(currentBuffer, '\0', 100);
textPtr = 0;
continue;
}
if (text[i] == '"') {
inQuotes = !inQuotes;
continue;
}
currentBuffer[textPtr] = text[i];
textPtr++;
}
currentBuffer[textPtr] = '\0';
list->copy(currentBuffer, textPtr);
memset(currentBuffer, '\0', 100);
// New behaviour : the list now store elements original pointers instead of clones
// for (int i = 0; i < textLength; i++) {
// if (text[i] == separator && !inQuotes) {
// // Adding to the list the element's pointer :
// // Pointer : [source text pointer] + [last index saved]
// // Size : [index] - [last index saved]
// list->add(text + itemPtr, i - itemPtr);
// itemPtr = i+1; // Save the index [last index saved] = [index]+1 (Because char at index is the separator char)
// continue;
// }
// if (text[i] == '"') {
// inQuotes = !inQuotes;
// continue;
// }
// }
// list->add(text + itemPtr, textLength - itemPtr);
return list;
}
char intToString(int value) {
return '0' + value;
}
char uintToString(uint32_t value) {
return '0' + value;
}
bool operator==(SecuredString left, SecuredString right) {
return check(&left, &right);
}