-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.hh
88 lines (81 loc) · 1.58 KB
/
parse.hh
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
#pragma once
typedef std::pair<const char*, int> Token;
inline bool operator==(Token b, const char* a)
{
return strlen(a) == b.second && memcmp(a, b.first, b.second) == 0;
}
inline bool is_integer(Token a)
{
if (a.second > 1 && a.first[0] == '-')
{
a.first += 1;
a.second -= 1;
}
if (a.second > 9) return false;
FOR(i, a.second) if (!isdigit(a.first[i])) return false;
return true;
}
inline int parse_int(Token a)
{
bool negative = false;
if (a.second > 1 && a.first[0] == '-')
{
a.first += 1;
a.second -= 1;
negative = true;
}
int v = 0;
FOR(i, a.second) v = v * 10 + a.first[i] - '0';
return negative ? -v : v;
}
inline bool is_real(Token a)
{
if (a.second > 1 && a.first[0] == '-')
{
a.first += 1;
a.second -= 1;
}
FOR(i, a.second) if (!isdigit(a.first[i]) && a.first[i] != '.') return false;
int dots = 0;
FOR(i, a.second) if (a.first[i] == '.') dots += 1;
return dots <= 1;
}
inline double parse_real(Token a)
{
bool negative = false;
if (a.second > 1 && a.first[0] == '-')
{
a.first += 1;
a.second -= 1;
negative = true;
}
double v = 0;
double e = 0.1;
bool dot = false;
FOR(i, a.second)
{
if (a.first[i] == '.')
{
dot = true;
}
else if (!dot)
{
v = v * 10 + (a.first[i] - '0');
}
else
{
v += e * (a.first[i] - '0');
e /= 10;
}
}
return negative ? -v : v;
}
// TODO: support ' and "
inline void tokenize(const char* text, int length, std::vector<Token>& tokens)
{
FOR(i, length)
{
if (text[i] == ' ') continue;
if (i == 0 || text[i-1] == ' ') tokens.push_back({ text + i, 1 }); else tokens.back().second += 1;
}
}