forked from JuliaData/Parsers.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.jl
More file actions
309 lines (308 loc) · 11.4 KB
/
Copy pathstrings.jl
File metadata and controls
309 lines (308 loc) · 11.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# this is mostly copy-pasta from Parsers.jl main xparse function
function xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options, ::Type{S}=PosLen) where {T <: AbstractString, S}
startpos = vstartpos = vpos = lastnonwhitespacepos = pos
sentstart = sentinelpos = 0
code = SUCCESS
sentinel = options.sentinel
quoted = false
if eof(source, pos, len)
code = (sentinel === missing ? SENTINEL : OK) | EOF
@goto donedone
end
b = peekbyte(source, pos)
# strip leading whitespace
while b == options.wh1 || b == options.wh2
pos += 1
incr!(source)
vpos = pos
if options.stripwhitespace
vstartpos = pos
end
if eof(source, pos, len)
code |= EOF
@goto donedone
end
b = peekbyte(source, pos)
end
# check for start of quoted field
if options.quoted
preqpos = pos
pos = checkquote(source, pos, len, options.oq)
quoted = pos > preqpos
if quoted
code = QUOTED
# since we're in quoted mode, reset vstartpos & vpos
vstartpos = vpos = pos
if eof(source, pos, len)
code |= INVALID_QUOTED_FIELD
@goto donedone
end
b = peekbyte(source, pos)
# ignore whitespace within quoted field
while b == options.wh1 || b == options.wh2
pos += 1
incr!(source)
vpos = pos
if options.stripquoted
vstartpos = pos
end
if eof(source, pos, len)
code |= INVALID_QUOTED_FIELD | EOF
@goto donedone
end
b = peekbyte(source, pos)
end
end
end
# check for sentinel values if applicable
if sentinel !== nothing && sentinel !== missing
sentstart = pos
sentinelpos = checksentinel(source, pos, len, sentinel)
end
vpos = lastnonwhitespacepos = pos
if options.quoted
# for quoted fields, find the closing quote character
if quoted
same = options.cq == options.e
while true
vpos = pos
if same && b == options.e
pos += 1
incr!(source)
if eof(source, pos, len)
code |= EOF
@goto donedone
elseif peekbyte(source, pos) != options.cq
break
end
code |= ESCAPED_STRING
pos += 1
incr!(source)
elseif b == options.e
pos += 1
incr!(source)
if eof(source, pos, len)
code |= INVALID_QUOTED_FIELD | EOF
@goto donedone
end
code |= ESCAPED_STRING
pos += 1
incr!(source)
elseif b == options.cq
pos += 1
incr!(source)
if eof(source, pos, len)
code |= EOF
@goto donedone
end
break
else
preqpos = pos
pos = checkquote(source, pos, len, options.cq)
if pos > preqpos
if eof(source, pos, len)
code |= EOF
@goto donedone
end
break
else
pos += 1
incr!(source)
end
end
if eof(source, pos, len)
code |= INVALID_QUOTED_FIELD | EOF
@goto donedone
end
# Always treat space ' ' and tab '\t' as whitespace when quoted
if options.stripquoted && b != options.wh1 && b != options.wh2 && b != UInt8(' ') && b != UInt8('\t')
lastnonwhitespacepos = pos
end
b = peekbyte(source, pos)
end
b = peekbyte(source, pos)
# ignore whitespace after quoted field
while b == options.wh1 || b == options.wh2
pos += 1
incr!(source)
if eof(source, pos, len)
code |= EOF
@goto donedone
end
b = peekbyte(source, pos)
end
end
end
if options.delim !== nothing
delim = options.delim
unquoted = Int(!quoted)
# now we check for a delimiter; if we don't find it, keep parsing until we do
while true
if !options.ignorerepeated
if delim isa UInt8
if b == delim
pos += 1
incr!(source)
code |= DELIMITED
@goto donedone
end
elseif delim isa PtrLen
predelimpos = pos
pos = checkdelim(source, pos, len, delim)
if pos > predelimpos
# found the delimiter we were looking for
code |= DELIMITED
@goto donedone
end
else
error()
end
else
if delim isa UInt8
matched = false
matchednewline = false
while true
if b == delim
matched = true
code |= DELIMITED
pos += 1
incr!(source)
elseif !matchednewline && b == UInt8('\n')
matchednewline = matched = true
pos += 1
incr!(source)
pos = checkcmtemptylines(source, pos, len, options)
code |= NEWLINE | ifelse(eof(source, pos, len), EOF, SUCCESS)
elseif !matchednewline && b == UInt8('\r')
matchednewline = matched = true
pos += 1
incr!(source)
if !eof(source, pos, len) && peekbyte(source, pos) == UInt8('\n')
pos += 1
incr!(source)
end
pos = checkcmtemptylines(source, pos, len, options)
code |= NEWLINE | ifelse(eof(source, pos, len), EOF, SUCCESS)
else
break
end
if eof(source, pos, len)
@goto donedone
end
b = peekbyte(source, pos)
end
if matched
@goto donedone
end
elseif delim isa PtrLen
matched = false
matchednewline = false
while true
predelimpos = pos
pos = checkdelim(source, pos, len, delim)
if pos > predelimpos
matched = true
code |= DELIMITED
elseif !matchednewline && b == UInt8('\n')
matchednewline = matched = true
pos += 1
incr!(source)
pos = checkcmtemptylines(source, pos, len, options)
code |= NEWLINE | ifelse(eof(source, pos, len), EOF, SUCCESS)
elseif !matchednewline && b == UInt8('\r')
matchednewline = matched = true
pos += 1
incr!(source)
if !eof(source, pos, len) && peekbyte(source, pos) == UInt8('\n')
pos += 1
incr!(source)
end
pos = checkcmtemptylines(source, pos, len, options)
code |= NEWLINE | ifelse(eof(source, pos, len), EOF, SUCCESS)
else
break
end
if eof(source, pos, len)
@goto donedone
end
b = peekbyte(source, pos)
end
if matched
@goto donedone
end
else
error()
end
end
# didn't find delimiter, but let's check for a newline character
if b == UInt8('\n')
pos += 1
incr!(source)
pos = checkcmtemptylines(source, pos, len, options)
code |= NEWLINE | ifelse(eof(source, pos, len), EOF, SUCCESS)
@goto donedone
elseif b == UInt8('\r')
pos += 1
incr!(source)
if !eof(source, pos, len) && peekbyte(source, pos) == UInt8('\n')
pos += 1
incr!(source)
end
pos = checkcmtemptylines(source, pos, len, options)
code |= NEWLINE | ifelse(eof(source, pos, len), EOF, SUCCESS)
@goto donedone
end
# didn't find delimiter nor newline, so increment and check the next byte
pos += 1
vpos += unquoted
if quoted
code |= INVALID_DELIMITER
end
if options.stripwhitespace
if !quoted && b != options.wh1 && b != options.wh2
lastnonwhitespacepos = vpos
end
end
incr!(source)
if eof(source, pos, len)
code |= EOF
@goto donedone
end
b = peekbyte(source, pos)
end
elseif !quoted
# no delimiter, so read until EOF
while !eof(source, pos, len)
pos += 1
incr!(source)
if options.stripwhitespace
b = peekbyte(source, pos)
if !quoted && b != options.wh1 && b != options.wh2
lastnonwhitespacepos = vpos
end
end
vpos += 1
end
end
@label donedone
ismissing = false
if sentinel !== nothing && sentinel !== missing && sentstart == vstartpos && sentinelpos == vpos
# if we matched a sentinel value that was as long or longer than our type value
code |= SENTINEL
ismissing = true
elseif sentinel === missing && vstartpos == vpos
code |= SENTINEL
ismissing = true
else
code |= OK
end
if eof(source, pos, len)
code |= EOF
end
if options.stripquoted || (options.stripwhitespace && !quoted)
vpos = lastnonwhitespacepos
end
poslen = PosLen(vstartpos, vpos - vstartpos, ismissing, escapedstring(code))
tlen = pos - startpos
return Result{S}(code, tlen, poslen)
end