-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_csv.h
More file actions
238 lines (178 loc) · 3.62 KB
/
Copy pathlc_csv.h
File metadata and controls
238 lines (178 loc) · 3.62 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef LC_CSV_H
#define LC_CSV_H
#ifndef LC_CSV_API
#define LC_CSV_API extern
#endif
///
/// RFC 4180:
/// https://datatracker.ietf.org/doc/html/rfc4180.html
///
#ifndef LC_HAVE_SV
#define LC_HAVE_SV
#define LEN(X) (sizeof(X) / sizeof(X)[0])
#include <stddef.h>
struct lc_sv {
size_t length;
const char *s;
};
#define sv(...) ((struct lc_sv){__VA_ARGS__})
#define sv_c(str) sv(.length = LEN(str) - 1, .s = str)
#endif
#ifndef lc_unreachable
#if __STDC_VERSION__ < 202311L
#if LC_CC_GNU
#define lc_unreachable __builtin_unreachable
#else
#define lc_unreachable() *((void *volatile *volatile)NULL)
#endif
#else
#define lc_unreachable unreachable
#endif
#endif
#include <stdbool.h>
struct lc_csv {
struct {
struct lc_sv buf;
} state;
struct {
char delimiter;
/// single line comment token if non-zero
/// Only parsed at start of line
char comment;
} config;
};
struct lc_csv_row {
struct {
struct lc_sv buf;
} state;
};
LC_CSV_API bool lc_csv_iter(struct lc_csv *csv, struct lc_csv_row *row);
LC_CSV_API bool lc_csv_row_iter(
struct lc_csv *csv,
struct lc_csv_row *row,
size_t n, char out[restrict n],
struct lc_sv *field
);
#ifdef LC_IMPLEMENTATION
#ifndef IN_RANGE
#define IN_RANGE(X, LO, HI) (((LO) <= (X)) & ((X) <= (HI)))
#endif
LC_CSV_API bool
lc_csv_iter(struct lc_csv *csv, struct lc_csv_row *row)
{
const char *p = csv->state.buf.s;
const char *e = csv->state.buf.length + p;
const char *o = p;
if (p >= e)
return false;
skip_comment:
if (csv->config.comment && csv->config.comment == *p) {
skip_comment_next:
if (++p >= e)
return false;
if (IN_RANGE(p[0], 0x0D, 0x0D) & IN_RANGE(p[1], 0x0A, 0x0A)) {
p += 2;
goto skip_comment;
}
goto skip_comment_next;
}
csv->state.buf.s = p;
csv->state.buf.length = e - p;
quote_outside:
if (p >= e)
goto eof;
if (IN_RANGE(*p, 0x22, 0x22)) {
p++; goto quote_inside;
}
if (IN_RANGE(p[0], 0x0D, 0x0D) & IN_RANGE(p[1], 0x0A, 0x0A)) {
o = p + 2;
goto eol;
}
p++; goto quote_outside;
quote_inside:
if (p >= e)
return false;
if (IN_RANGE(p[0], 0x22, 0x22)) {
if (IN_RANGE(p[1], 0x22, 0x22)) {
p++; p++; goto quote_inside;
}
p++; goto quote_outside;
}
p++; goto quote_inside;
eof:
o = p;
eol:
row->state.buf.s = csv->state.buf.s;
row->state.buf.length = p - csv->state.buf.s;
csv->state.buf.length = e - o;
csv->state.buf.s = o;
return true;
}
LC_CSV_API bool
lc_csv_row_iter(
struct lc_csv *csv,
struct lc_csv_row *row,
size_t n, char out[restrict n],
struct lc_sv *field
)
{
const char *p = row->state.buf.s;
const char *e = row->state.buf.length + p;
char *o = out;
if (p >= e)
return false;
if (!csv->config.delimiter)
csv->config.delimiter = ',';
if (csv->config.delimiter == *p)
goto done;
if (IN_RANGE(*p, 0x22, 0x22)) {
p++;
goto quoted_begin;
}
if (IN_RANGE(*p, 0x20, 0x7E))
goto field_begin;
lc_unreachable();
quoted_begin:
quoted:
if (IN_RANGE(*p, 0x22, 0x22)) {
p++;
goto quoted_end;
}
if (IN_RANGE(*p, 0x20, 0x7E) | IN_RANGE(*p, 0x0D, 0x0D) | IN_RANGE(*p, 0x0A, 0x0A)) {
*o++ = *p++;
goto quoted;
}
lc_unreachable();
quoted_end:
if (IN_RANGE(*p, 0x22, 0x22)) {
*o++ = *p++;
goto quoted;
}
if (csv->config.delimiter == *p)
goto done;
goto done_eof;
lc_unreachable();
field_begin:
field:
if (csv->config.delimiter == *p)
goto field_end;
if (p >= e)
goto done_eof;
if (IN_RANGE(*p, 0x20, 0x7E)) {
*o++ = *p++;
goto field;
}
lc_unreachable();
field_end:
goto done;
done:
++p;
done_eof:
field->s = out;
field->length = o - out;
row->state.buf.s = p;
row->state.buf.length = e - p;
return true;
}
#endif
#endif