-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
223 lines (179 loc) · 6.61 KB
/
parse.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
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
#include "parse.h"
#include <errno.h>
#include <string.h>
#include <wctype.h>
struct lookup {
/// Labels may use American or British English.
/// Labels may use plurals.
/// Labels may refer to SI or non-SI units in common use.
const wchar_t *label;
/// Unit.
enum unit unit;
};
/// Lookup table used for parsing user description of a unit.
static struct lookup labels[] = {
{ L"", PresentationUnitNone },
{ L"km", PresentationUnitKilometre },
{ L"kilometre", PresentationUnitKilometre },
{ L"kilometres", PresentationUnitKilometre },
{ L"kilometer", PresentationUnitKilometre },
{ L"kilometers", PresentationUnitKilometre },
{ L"m", PresentationUnitMetre },
{ L"metre", PresentationUnitMetre },
{ L"metres", PresentationUnitMetre },
{ L"meter", PresentationUnitMetre },
{ L"meters", PresentationUnitMetre },
{ L"cm", PresentationUnitCentimetre },
{ L"centimetre", PresentationUnitCentimetre },
{ L"centimetres", PresentationUnitCentimetre },
{ L"centimeter", PresentationUnitCentimetre },
{ L"centimeters", PresentationUnitCentimetre },
{ L"mm", PresentationUnitMillimetre },
{ L"millimetre", PresentationUnitMillimetre },
{ L"millimetres", PresentationUnitMillimetre },
{ L"millimeter", PresentationUnitMillimetre },
{ L"millimeters", PresentationUnitMillimetre },
// Not SI, but commonly used.
{ L"mi", PresentationUnitMile },
{ L"mile", PresentationUnitMile },
{ L"miles", PresentationUnitMile },
// Not SI, but commonly used.
{ L"yd", PresentationUnitYard },
{ L"yds", PresentationUnitYard },
{ L"yard", PresentationUnitYard },
{ L"yards", PresentationUnitYard },
// Not SI, but commonly used.
{ L"'", PresentationUnitFeet },
{ L"ft", PresentationUnitFeet },
{ L"feet", PresentationUnitFeet },
{ L"foot", PresentationUnitFeet },
// Not SI, but commonly used.
{ L"'\"", PresentationUnitFeetAndInches },
// Not SI, but commonly used.
{ L"\"", PresentationUnitInch },
{ L"in", PresentationUnitInch },
{ L"inch", PresentationUnitInch },
{ L"inches", PresentationUnitInch },
{ L"kg", PresentationUnitKilogram },
{ L"kilogram", PresentationUnitKilogram },
{ L"kilograms", PresentationUnitKilogram },
{ L"g", PresentationUnitGram },
{ L"gram", PresentationUnitGram },
{ L"grams", PresentationUnitGram },
{ L"mg", PresentationUnitMilligram },
{ L"milligram", PresentationUnitMilligram },
{ L"milligrams", PresentationUnitMilligram },
// Not SI, but commonly used.
{ L"lb", PresentationUnitPound },
{ L"lbs", PresentationUnitPound },
{ L"pound", PresentationUnitPound },
{ L"pounds", PresentationUnitPound },
// Not SI, but commonly used.
{ L"oz", PresentationUnitOunce },
{ L"ounce", PresentationUnitOunce },
{ L"ounces", PresentationUnitOunce },
{ L"K", PresentationUnitKelvin },
// Less common.
{ L"°K", PresentationUnitKelvin },
{ L"Kelvin", PresentationUnitKelvin },
{ L"degrees Kelvin", PresentationUnitKelvin },
{ L"°C", PresentationUnitDegreesCelsius },
{ L"'C", PresentationUnitDegreesCelsius },
{ L"°Celsius", PresentationUnitDegreesCelsius },
{ L"'Celsius", PresentationUnitDegreesCelsius },
{ L"degrees Celsius", PresentationUnitDegreesCelsius },
{ L"°F", PresentationUnitDegreesFahrenheit },
{ L"'F", PresentationUnitDegreesFahrenheit },
{ L"°Fahrenheit", PresentationUnitDegreesFahrenheit },
{ L"'Fahrenheit", PresentationUnitDegreesFahrenheit },
{ L"degrees Fahrenheit", PresentationUnitDegreesFahrenheit },
{ L"Pa", PresentationUnitPascal },
{ L"pascal", PresentationUnitPascal },
{ L"hPa", PresentationUnitHectoPascal },
{ L"hectopascal", PresentationUnitHectoPascal },
{ L"hectopascals", PresentationUnitHectoPascal },
{ L"kPa", PresentationUnitKiloPascal },
{ L"kilopascal", PresentationUnitKiloPascal },
{ L"kilopascals", PresentationUnitKiloPascal },
{ L"mmHg", PresentationUnitMillimetreMercury },
{ L"millimeter of mercury", PresentationUnitMillimetreMercury },
{ L"millimeters of mercury", PresentationUnitMillimetreMercury },
{ L"inHg", PresentationUnitInchesMercury },
{ L"in Hg", PresentationUnitInchesMercury },
{ L"\"Hg", PresentationUnitInchesMercury },
{ L"\" Hg", PresentationUnitInchesMercury },
{ L"inch of mercury", PresentationUnitInchesMercury },
{ L"inches of mercury", PresentationUnitInchesMercury },
};
/// @return True if end of word.
static bool is_eow(const wchar_t *s)
{
return !s || !*s || iswspace(*s) || iswdigit(*s);
}
/// @return True if string @c s matches, or begins with, string @c w.
static bool accept_word(wchar_t *s, const wchar_t *w, wchar_t **p)
{
if (!wcsncmp(s, w, wcslen(w))) {
s += wcslen(w);
if (is_eow(s)) {
if (p) {
*p = s;
}
return true;
}
}
return false;
}
enum unit parse_unit_label(wchar_t *s, wchar_t **p)
{
// Skip whitespace.
while (iswspace(*s)) {
s++;
}
*p = s;
if (!*s) {
return PresentationUnitNone;
}
size_t candidate = 0;
size_t candidates = 0;
size_t length = 0;
for (size_t i = 0; i < sizeof(labels) / sizeof(*labels); ++i) {
if (accept_word(s, labels[i].label, NULL)) {
// Find longest match.
if (wcslen(labels[i].label) > length) {
candidate = i;
candidates++;;
length = wcslen(labels[i].label);
}
}
}
if (candidates) {
accept_word(s, labels[candidate].label, p);
return labels[candidate].unit;
}
return PresentationUnitUnknown;
}
struct value parse_quantity_and_unit(const wchar_t *s, wchar_t **p)
{
struct value v;
memset(&v, 0, sizeof(v));
v.quantity = wcstod(s, p);
if (*p != s) {
v.has_quantity = true;
v.input_unit = parse_unit_label(*p, p);
v.quantity = unit_to_base_unit(v.quantity, v.input_unit, &v.base_unit);
if (v.input_unit == PresentationUnitFeet) {
// Exceptionally, we make a second conversion for potential inches.
struct value w = parse_quantity_and_unit(*p, p);
if (w.input_unit == PresentationUnitNone) {
// Assume that Y in X'Y is inches.
w.input_unit = PresentationUnitInch;
w.quantity = unit_to_base_unit(w.quantity, PresentationUnitInch, &w.base_unit);
}
if (w.input_unit == PresentationUnitInch) {
v.quantity += w.quantity;
}
}
}
return v;
}