summaryrefslogtreecommitdiff
path: root/src/literal.c
blob: 8723462d6acbc284b7f4716ae2b57191fff3048f (plain)
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
#include <ctype.h>
#include <string.h>
#include <json.h>
#include "internal.h"

enum json_parse_result parse_literal(struct json **json_out, struct raw_json *raw) {
	enum json_parse_result ret = JSON_PARSE_OK;
	struct json *json = NULL;

	bool plus_one = raw->data[raw->index] == '+' || raw->data[raw->index] == '-';
	if (plus_one || isdigit(raw->data[raw->index])) {
		if (raw->data[raw->index] == '0' && isdigit(raw->data[raw->index + 1])) {
			ret = JSON_PARSE_INVALID_NUMBER_ERR;
			goto err;
		}
		const char *start = raw->data + raw->index + plus_one;
		char *end;
		/* is probably a number here, strtodl will tell */
		double num = strtod(start, &end);
		if (end != start) {
			if (raw->data[raw->index] == '-')
				num = -num;
			json = json_new_number(num);
			raw->index = end - raw->data;
		} else {
			ret = JSON_PARSE_INVALID_NUMBER_ERR;
			goto err;
		}
	} else {
		size_t end_index;
		for (end_index = raw->index; end_index < raw->size; end_index++) {
			if (!isalpha(raw->data[end_index])) {
				break;
			}
		}
		size_t len = end_index - raw->index;
		if (len == 4 && strncmp(raw->data + raw->index, "true", len) == 0) {
			json = json_new_bool(true);
		} else if (len == 5 && strncmp(raw->data + raw->index, "false", len) == 0) {
			json = json_new_bool(false);
		} else if (len == 4 && strncmp(raw->data + raw->index, "null", len) == 0) {
			json = json_new_null();
		} else {
			ret = JSON_PARSE_INVALID_TOKEN_ERR;
		}
		raw->index = end_index;
	}

	if (!json)
		return JSON_OOM;

	*json_out = json;
	goto end;
err:
	json_delete(json);
end:
	return ret;
}