-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
103 lines (84 loc) · 2.05 KB
/
array.c
File metadata and controls
103 lines (84 loc) · 2.05 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
#include <stdlib.h>
#include <string.h>
#include "array.h"
#include "obj.h"
#include "boolean.h"
#include "number.h"
#include "null.h"
#include "str.h"
#include "common.h"
JsonValue *match_array(char *json, int *index, int size)
{
JsonValue *result = create_json_value();
result->start = *index;
result->end = *index;
result->type = JSON_ARRAY;
result->array = create_json_array();
result->error->code = "";
// should start with a left square bracket
if (json[*index] != '[')
{
result->error->code = "array-no-opening-bracket";
return result;
}
result->partial = 1;
result->end++;
JsonValue *item = NULL;
int expect_item = 0;
char ch = '\0';
while (result->end < size)
{
skip_empty(json, &result->end, size);
ch = json[result->end];
if (item == NULL || expect_item)
{
// try and match a closing bracket
if (expect_item == 0 && ch == ']')
{
free_json_error(result->error);
result->error = NULL;
break;
}
// try and match an item
item = match_any(json, &result->end, size);
if (item->error == NULL)
{
// add item to array
append_to_json_array(result->array, item);
// for numbers, last index is exclusive
result->end = item->end + (item->type == JSON_NUMBER ? 0 : 1);
expect_item = 0;
continue;
}
result->error = item->error;
break;
}
// array can end after item
if (ch == ']')
{
free_json_error(result->error);
result->error = NULL;
break;
}
if (ch != ',')
{
result->error->code = "array-missing-comma";
result->error->index = result->end;
break;
}
// should be a comma
item = NULL;
expect_item = 1;
result->end++;
}
// re-check last index is a closing bracket
if (result->error != NULL)
{
if (json[result->end] != ']' && strcmp(result->error->code, "") == 0)
{
result->error->code = "array-no-closing-bracket";
result->error->index = result->end;
}
}
return result;
}