-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminejson.cpp
More file actions
279 lines (173 loc) · 7.46 KB
/
Copy pathminejson.cpp
File metadata and controls
279 lines (173 loc) · 7.46 KB
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
//
// Created by Micael Cossa on 29/07/2025.
//
#include "minejson.h"
#include <stack>
#include <limits>
constexpr std::string_view::size_type ERROR_VALUE = std::string_view::npos;
std::string_view::size_type getKeyName(const std::string_view stringView, std::string_view::size_type pos, std::string& out) {
if(stringView[pos] != '"')
return ERROR_VALUE;
std::string_view::size_type quotationEndPos = pos + 1;
std::string name;
while(quotationEndPos < stringView.size()) {
if(stringView[quotationEndPos] == '\\') {
quotationEndPos += 2;
continue;
}
if(stringView[quotationEndPos] == '"')
break;
name += stringView[quotationEndPos];
quotationEndPos++;
}
// The only reason this would be true is if the while loop stop due to quotationEndPos reaching the same length as the json string.
if(quotationEndPos >= stringView.size())
return ERROR_VALUE;
out = std::move(name);
return quotationEndPos;
}
// Pretty much just checking if there is a ':'
std::string_view::size_type hasValueSignate(std::string_view stringView, std::string_view::size_type pos) {
pos = stringView.find_first_not_of(" \t\n\r", pos);
if(pos == ERROR_VALUE)
return pos;
return stringView[pos] == ':' ? pos : ERROR_VALUE;
}
std::string_view::size_type validateCommaAndSkip(size_t listsize, std::string_view stringView, std::string_view::size_type pos) {
if(pos != ERROR_VALUE && stringView[pos] == ',') {
if (listsize == 0 || pos + 1 >= stringView.length()) // why is there a comma if there are no values on the array?
return ERROR_VALUE;
// skip the whitespace after ','
pos = stringView.find_first_not_of(" \t\n\r", pos + 1);
} else if(listsize > 0) // There must be a ',' after a value is entered on the array.
return ERROR_VALUE;
return pos;
}
std::string_view::size_type startObjectProccessing(json_object* root , std::string_view stringView, std::string_view::size_type pos) {
while (stringView.length() > pos) {
pos = stringView.find_first_not_of(" \t\n\r", pos);
if(pos == ERROR_VALUE || stringView[pos] == '}')
break;
pos = validateCommaAndSkip(root->size(), stringView, pos);
if(pos == ERROR_VALUE)
goto error;
std::string name;
// gets text between two quotation marks, returns the '"' end char pos
pos = getKeyName(stringView, pos, name);
// did an error happening while trying to process the key?
if(pos == ERROR_VALUE)
goto error;
// checks if ':' exists and returns the position of the : char
pos = hasValueSignate(stringView, pos + 1);
if(pos == ERROR_VALUE)
goto error;
json_node* value = nullptr;
// skip the whitespace after, returns the position of the character after the value identified
pos = valueIdentification(stringView, stringView.find_first_not_of(" \t\n\r", pos+1), value);
if(pos == ERROR_VALUE || value == nullptr)
goto error;
root->insertKeyValue(name, value);
}
// did a while loop end because of our desired outcome?
if(pos >= stringView.length() || stringView[pos] != '}')
goto error;
return pos;
error:
delete root;
return ERROR_VALUE;
}
std::string_view::size_type startArrayProccessing(json_array* root , std::string_view stringView, std::string_view::size_type pos) {
while (stringView.length() > pos) {
pos = stringView.find_first_not_of(" \t\n\r", pos);
if(pos == ERROR_VALUE || stringView[pos] == ']')
break;
pos = validateCommaAndSkip(root->size(), stringView, pos);
if(pos == ERROR_VALUE)
goto error;
json_node* node = nullptr;
pos = valueIdentification(stringView, pos, node);
if(pos == ERROR_VALUE)
goto error;
root->addValue(node);
}
// did a while loop end because of our desired outcome?
if(pos >= stringView.length() || stringView[pos] != ']')
goto error;
return pos;
error:
delete root;
return ERROR_VALUE;
}
std::string_view::size_type valueIdentification(std::string_view stringView, std::string_view::size_type pos, json_node*& out) {
if(pos >= stringView.length())
return ERROR_VALUE;
if(stringView[pos] == '{') {
out = new json_object();
pos = startObjectProccessing(dynamic_cast<json_object *>(out), stringView, pos + 1);
} else if(stringView[pos] == '[') {
out = new json_array();
pos = startArrayProccessing(dynamic_cast<json_array *>(out), stringView, pos + 1);
} else if(stringView[pos] == '"') {
std::string stringvalue;
pos = getKeyName(stringView, pos, stringvalue);
if(pos != ERROR_VALUE) {
out = new json_string(stringvalue);
}
} else if(isdigit(stringView[pos]) || stringView[pos] == '-') {
std::string originalValue;
long long number = 0;
bool decimal = false , negative = false;
if(stringView[pos] == '-') {
negative = true;
originalValue+=stringView[pos];
pos+=1;
}
for(; pos < stringView.length() ; pos++) {
if(stringView[pos] == '.') {
if(decimal)
return ERROR_VALUE; // why is there two '.' ?
decimal = true; // i cba to process decimal values, maybe some other time when im feeling challenged.
originalValue+=stringView[pos];
} else if(isdigit(stringView[pos]) ) {
originalValue+=stringView[pos];
if(!decimal) {
long long max_val = std::numeric_limits<long long>::max();
// Check if number*10 would overflow
if(number > max_val / 10) {
// Overflow will occur
return ERROR_VALUE;
}
number *= 10;
long long digit = stringView[pos] - '0';
// Check if adding digit would overflow
if(number > max_val - digit) {
// Overflow will occur
return ERROR_VALUE;
}
number += digit;
}
} else {
// idk what this character can be, I will let the startObjectProccessing or startArrayProccessing discover
pos-=1;
break;
}
}
if(originalValue == "-")
return ERROR_VALUE;
if(negative)
number*=-1;
out = new json_number(number, originalValue);
} else
return ERROR_VALUE;
return pos + (pos == ERROR_VALUE ? 0 : 1);
}
json_node* readJsonString(std::string_view stringView) {
if(stringView.empty() || stringView.length() < 2)
return nullptr;
std::string_view::size_type pos = 0;
json_node* root = nullptr;
pos = valueIdentification(stringView, pos, root);
if(pos == ERROR_VALUE || root == nullptr)
return nullptr;
return root;
}