-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
98 lines (85 loc) · 2.01 KB
/
str.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "str.h"
JsonValue *match_string(char *json, int *index, int size)
{
JsonValue *result = create_json_value();
result->start = *index;
result->end = *index;
result->type = JSON_STRING;
result->error->code = "";
// should start with a double quote
if (json[*index] != '"')
{
result->error->code = "string-no-opening-quote";
return result;
}
result->partial = 1;
int forbidden_len = strlen(FORBIDDEN);
int forbidden_chars_len = strlen(FORBIDDEN_CHARS);
int stop = 0;
int i = 0;
int j = 0;
char ch = '\0';
while (result->end < size)
{
result->end++;
ch = json[result->end];
for (i = 0; i < forbidden_len; i++)
{
if (ch == FORBIDDEN[i])
{
if (ch == '\\')
{
// should match escape character
for (j = 0; j < forbidden_chars_len; j++)
{
if (json[result->end + 1] == FORBIDDEN_CHARS[j])
{
// escaped successfully
result->end++;
break;
}
}
}
else if (ch == '"')
{
stop = 1;
free_json_error(result->error);
result->error = NULL;
break;
}
else
{
result->error->code = "string-no-unescaped-forbidden-char";
result->error->index = result->end;
stop = 1;
}
break;
}
}
if (stop == 1)
{
break;
}
}
// last character should be a double quote
if (json[result->end] != '"' && strcmp(result->error->code, "") == 0)
{
if (result->error != NULL)
{
result->error = create_json_error(result->end, "string-no-closing-quote");
}
else
{
result->error->code = "string-no-closing-quote";
}
}
if (result->error == NULL)
{
// copy the value of the string from start to end in a new string
result->string = slice_string(json, result->start + 1, result->end);
}
return result;
}