-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKv3Parser.cs
More file actions
350 lines (314 loc) · 11.3 KB
/
Kv3Parser.cs
File metadata and controls
350 lines (314 loc) · 11.3 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
public class Kv3Parser
{
private string content;
private int index;
private object parsedData;
public class ValueStruct
{
public object Value { get; set; }
}
public Kv3Parser()
{
content = string.Empty;
index = 0;
parsedData = null;
}
public void Parse(string content)
{
try
{
Console.WriteLine("Starting parse...");
this.content = content;
index = 0;
Console.WriteLine($"Content length: {content.Length}, Initial index: {index}");
var stopwatch = Stopwatch.StartNew();
SkipCommentsAndMetadata();
Console.WriteLine($"After SkipCommentsAndMetadata, index: {index}");
parsedData = ParseValue().Value;
Console.WriteLine("Parsing completed.");
stopwatch.Stop();
Console.WriteLine($"Parsing took {stopwatch.ElapsedMilliseconds} ms");
}
catch (Exception ex)
{
Console.WriteLine($"Parse error at index {index}: {ex.Message}");
throw;
}
}
public string GetValue(string path)
{
try
{
Console.WriteLine($"Getting value for path: {path}");
object currentValue = parsedData;
var segments = path.Split('.');
foreach (var segment in segments)
{
Console.WriteLine($"Processing segment: {segment}");
string key = segment;
int? arrayIndex = null;
int bracketPos = segment.IndexOf('[');
if (bracketPos != -1)
{
key = segment.Substring(0, bracketPos);
int endBracketPos = segment.IndexOf(']', bracketPos);
arrayIndex = int.Parse(segment.Substring(bracketPos + 1, endBracketPos - bracketPos - 1));
Console.WriteLine($"Key: {key}, Array Index: {arrayIndex}");
}
if (currentValue is Dictionary<string, ValueStruct> obj)
{
Console.WriteLine($"Current value is object with keys: {string.Join(", ", obj.Keys)}");
if (obj.TryGetValue(key, out var value))
{
currentValue = value.Value;
}
else
{
Console.WriteLine($"Key not found: {key}");
return string.Empty;
}
}
if (arrayIndex.HasValue && currentValue is List<ValueStruct> arr)
{
Console.WriteLine($"Current value is array of length: {arr.Count}");
if (arrayIndex.Value < arr.Count)
{
currentValue = arr[arrayIndex.Value].Value;
}
else
{
Console.WriteLine($"Array index out of bounds: {arrayIndex.Value}");
return string.Empty;
}
}
}
string result = currentValue is string str ? str : string.Empty;
Console.WriteLine($"Returning value: {result}");
return result;
}
catch (Exception ex)
{
Console.WriteLine($"GetValue error for path {path}: {ex.Message}");
return string.Empty;
}
}
private void SkipCommentsAndMetadata()
{
Console.WriteLine("Skipping comments and metadata...");
int iteration = 0;
while (index < content.Length && content[index] != '{')
{
if (iteration++ > 1000000) // Prevent infinite loop
{
throw new InvalidOperationException("Infinite loop detected in SkipCommentsAndMetadata");
}
index = content.IndexOf('\n', index) + 1;
if (index == 0) // If no newline found, break to avoid infinite loop
{
index = content.Length;
break;
}
}
Console.WriteLine($"Finished SkipCommentsAndMetadata, index: {index}");
}
private void SkipWhitespace()
{
int iteration = 0;
while (index < content.Length && char.IsWhiteSpace(content[index]))
{
if (iteration++ > 1000000)
{
throw new InvalidOperationException("Infinite loop detected in SkipWhitespace");
}
index++;
}
}
private void SkipComments()
{
int iteration = 0;
while (index < content.Length && content[index] == '/')
{
if (iteration++ > 1000000)
{
throw new InvalidOperationException("Infinite loop detected in SkipComments");
}
index = content.IndexOf('\n', index) + 1;
if (index == 0)
{
index = content.Length;
break;
}
}
}
private int GetKeyOrValueEnd()
{
string delimiters = "= \n{[}],";
int end = content.IndexOfAny(delimiters.ToCharArray(), index);
return end == -1 ? content.Length : end;
}
private ValueStruct ParseValue()
{
try
{
Console.WriteLine($"ParseValue at index: {index}");
SkipComments();
SkipWhitespace();
if (index >= content.Length)
{
throw new InvalidOperationException("Unexpected end of content in ParseValue");
}
if (content[index] == '{')
{
Console.WriteLine("Parsing object...");
return new ValueStruct { Value = ParseObject() };
}
else if (content[index] == '[')
{
Console.WriteLine("Parsing array...");
return new ValueStruct { Value = ParseArray() };
}
else if (content[index] == '#' && index + 1 < content.Length && content[index + 1] == '[')
{
Console.WriteLine("Parsing byte array...");
index++;
return ParseByteArray();
}
else
{
int valueStart = index;
int valueEnd = GetKeyOrValueEnd();
Console.WriteLine($"Parsing string value from {valueStart} to {valueEnd}");
index = valueEnd;
return new ValueStruct { Value = content.Substring(valueStart, valueEnd - valueStart) };
}
}
catch (Exception ex)
{
Console.WriteLine($"ParseValue error at index {index}: {ex.Message}");
throw;
}
}
private ValueStruct ParseByteArray()
{
try
{
Console.WriteLine($"ParseByteArray at index: {index}");
SkipWhitespace();
index++;
int valueStart = index;
int valueEnd = content.IndexOf(']', index);
if (valueEnd == -1)
{
throw new InvalidOperationException("Unclosed byte array");
}
string rawByteData = content.Substring(valueStart, valueEnd - valueStart);
Console.WriteLine($"Raw byte data: {rawByteData.Substring(0, Math.Min(rawByteData.Length, 50))}...");
var bytes = rawByteData.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string cleanedByteData = string.Join(" ", bytes);
index = valueEnd + 1;
return new ValueStruct { Value = cleanedByteData };
}
catch (Exception ex)
{
Console.WriteLine($"ParseByteArray error at index {index}: {ex.Message}");
throw;
}
}
private object ParseObject()
{
try
{
Console.WriteLine($"ParseObject at index: {index}");
var obj = new Dictionary<string, ValueStruct>();
SkipWhitespace();
index++;
int iteration = 0;
while (index < content.Length)
{
if (iteration++ > 1000000)
{
throw new InvalidOperationException("Infinite loop detected in ParseObject");
}
SkipComments();
SkipWhitespace();
if (content[index] == '}')
{
index++;
Console.WriteLine($"Finished parsing object with {obj.Count} keys");
return obj;
}
int keyStart = index;
int keyEnd = GetKeyOrValueEnd();
if (keyEnd == index)
{
throw new InvalidOperationException("Invalid key at index " + index);
}
string key = content.Substring(keyStart, keyEnd - keyStart);
Console.WriteLine($"Parsed key: {key}");
index = keyEnd;
SkipWhitespace();
if (index < content.Length && content[index] == '=')
{
index++;
}
var value = ParseValue();
obj[key] = value;
SkipWhitespace();
if (index < content.Length && content[index] == ',')
{
index++;
}
}
throw new InvalidOperationException("Unclosed object at index " + index);
}
catch (Exception ex)
{
Console.WriteLine($"ParseObject error at index {index}: {ex.Message}");
throw;
}
}
private object ParseArray()
{
try
{
Console.WriteLine($"ParseArray at index: {index}");
var arr = new List<ValueStruct>();
SkipWhitespace();
index++;
int iteration = 0;
while (index < content.Length)
{
if (iteration++ > 1000000)
{
throw new InvalidOperationException("Infinite loop detected in ParseArray");
}
SkipComments();
SkipWhitespace();
if (content[index] == ']')
{
index++;
Console.WriteLine($"Finished parsing array with {arr.Count} elements");
return arr;
}
var value = ParseValue();
arr.Add(value);
SkipWhitespace();
if (index < content.Length && content[index] == ',')
{
index++;
}
}
throw new InvalidOperationException("Unclosed array at index " + index);
}
catch (Exception ex)
{
Console.WriteLine($"ParseArray error at index {index}: {ex.Message}");
throw;
}
}
}