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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// This file is a part of the Rust Data Reader Library
// Copyright 2018 Robert Carson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
use memmap::MmapOptions;
///parse_txt reads in a data file that is made up any type(s). It parses the data file finding all of the field data and saving off in its raw
///byte form. It can fail in a number of other ways related to invalid parameters or the data file having malformed fields. These errors are
///percolated up to whatever is calling this in the form of the Error type.
///One should therefore check to make sure no errors are obtained when examining the file. If a malformed field is seen the error
///does contain information about what line number of the data file has the malformed field.
///
///Input -
///
/// f is simply the location of the file.
///
/// params is ReaderParams structure. An example for what this looks like can be found in the test directory.
///
///Output -
///
/// A Result type that either contains a RawReaderResults structure or an error.
pub fn parse_txt(f: &str, params: &ReaderParams) -> Result<RawReaderResults, Error> {
//So, we have a couple different states we could be in
//nw_line = the beginning of a new line
//delim = a delimiter character
//space = any white space
//field = a field we want to keep
//sk_field = a field we want to skip
#[derive(Debug, Clone)]
enum ParseState {
NwLine,
Delim,
Space,
Field,
SkField,
}
let file = File::open(f)?;
//our comment string
//If we don't have one then we just say a comment is a newline character.
//The newline check comes first in all of these so it'll be as if the parser never
//has to worry about the comments.
let cmt = if let Some(x) = params.comments {
x
} else {
b'\n'
};
let buffer = unsafe { MmapOptions::new().map(&file)? };
//We're explicitly using the raw bytes here
// let mut reader = BufReader::with_capacity(BUF_SIZE, file);
//We are finding how many lines in our data file are actually readable and are not commented lines.
let num_lines = read_num_file_lines_mmap(&buffer[..], cmt);
//We need to rewind our file back to the start.
// reader.seek(SeekFrom::Start(0))?;
//We are initializing our ReaderResult structure
let mut results = RawReaderResults {
num_fields: 0,
num_lines: 0,
results: Vec::<u8>::new(),
index: Vec::<usize>::new(),
};
//The next portion of lines is some minor error handling to make sure our parameters we provided were valid for our data file.
//We're checking to see if we have a valid number of skipped lines for the header.
match ¶ms.skip_header {
Some(x) => {
if *x >= num_lines {
return Err(format_err!(
"Input for skip_header greater than the number of readable lines in the file"
));
}
}
None => (),
}
//Now that we know our number is valid we are setting a variable for our skipped header lines to be equal to our skippable lines.
let sk_h = if let Some(x) = params.skip_header {
x
} else {
0
};
//We're checking to see if we have a valid number of skipped lines for the footer.
match ¶ms.skip_footer {
Some(x) => {
if *x >= num_lines {
return Err(format_err!(
"Input for skip_footer greater than the number of readable lines in the file"
));
}
}
None => (),
}
//Now that we know our number is valid we are setting a variable for our skipped footer lines to be equal to our skippable lines.
let sk_f = if let Some(x) = params.skip_footer {
x
} else {
0
};
//We need to error if the number of lines we can read is equal to or less than the number of skipped header and footer lines.
if num_lines <= (sk_h + sk_f) {
return Err(format_err!("Input for skip_footer and skip_header greater than or equal to the number of readable lines in the file"));
}
//Here we're determining the number of lines that we need to read from our file.
let num_lines_read = match ¶ms.max_rows {
//If we've set a maximum number of rows. We need to first see if that number is greater than
//the number of readable none skipped lines.
//If we didn't then the maximum number is set to our number of readable non skipped lines.
Some(x) => {
let diff_lines = num_lines - sk_h - sk_f;
if diff_lines > *x {
*x
} else {
diff_lines
}
}
None => (num_lines - sk_h - sk_f),
};
//We're simply stating whether we're using whitespaces or not for our delimiter.
let delim_ws = match ¶ms.delimiter {
Delimiter::WhiteSpace => true,
Delimiter::Any(_b) => false,
};
//Our delimeter value. If we are delimiting using whitespace we set this as a space. However, we'll take into consideration tabs as well.
let delim = match ¶ms.delimiter {
Delimiter::WhiteSpace => b' ',
Delimiter::Any(b) => *b,
};
//File line number used for Error information
let mut fln = 0;
let mut index = 0;
//If we skip any header lines then we need to skip forward through the file by
//the correct number of lines when not taking into account commented lines.
//We're using the memchr crate to locate all of the most common newline characters
//It provides a nice iterator over our buffer that we can now use.
let mut newline = memchr2_iter(b'\n', b'\r', &buffer[..]);
if sk_h > 0 {
let mut count = 0;
//We loop over until we've skipped over the desired number of lines
//We first find the length of our buffer
//We fill the buffer up. Our buffer is mutable which is why it's in this block
// let buffer = reader.fill_buf().unwrap();
//We're now going to use an explicit loop.
//I know this isn't idiomatic rust, but I couldn't really see a good way of skipping my iterator
//to a location of my choosing.
let mut i = 0;
//We don't want our loop index to go past our buffer length or else bad things could occur
let length = buffer.len();
//Keeping it old school with some nice wild loops
while i < length {
//Here's where the main magic occurs
//If we come across a space or tab we move to the next item in the buffer
//If we come across a newline character we advance our iterator and move onto the
//next index essentially
//If we come across a comment character first (white spaces aren't counted) we completely skip the line
//If we come across any other character first (white spaces aren't counted) we increment our line counter
//and then skip the rest of the contents of the line.
//If we no longer have an item in our newline iterator we're done with everything in our buffer, and so
//we can exit the loop.
if (buffer[i] == b' ') | (buffer[i] == b'\t') {
i += 1;
} else if (buffer[i] == b'\n') | (buffer[i] == b'\r') | (buffer[i] == cmt) {
let val = newline.next();
i = match val {
Some(val) => val + 1,
None => length + 1,
};
fln += 1;
} else {
count += 1;
let val = newline.next();
i = match val {
Some(val) => val + 1,
None => length + 1,
};
fln += 1;
}
//Here we're checking to see if we've reached the number of lines to skip or not
//If we've skipped over the desired number of lines we can exit the loop.
if count == sk_h {
index = i;
break;
}
}
//We now need to consume everything upto "length" in our buffer, so it's marked off as no longer being needed
// reader.consume(length);
}
//We are now going to loop through everything until we either reach the last line we need or we reach EOF.
//We need to first set our state to being start of a line.
let mut state = ParseState::NwLine;
//Next we need to get a list of our columns we might be using. If we aren't we supply an empty vector, so we can easily check if the len is 0.
//While these values are 0 indexed externally, internally it's a bit easier to deal with 1-based indexing for the time being.
let cols = match ¶ms.usecols {
Some(x) => x.iter().map(|&x| x + 1).collect::<Vec<usize>>(),
None => Vec::<usize>::new(),
};
//We need to count our field variables and set this variable initially outside the main loop.
let mut field_counter = 0;
//We'll need to now the total number of fields later on and set this variable initially outside the main loop.
let mut tot_fields = 0;
//The loop here is where all of the magic happens. It's designed so that it operates based on a state. So, we're essentially running a poorly optimized
//state machine. However, it turns out that this works decent enough for our purposes as long as the optimizer is used.
//We're now going to use an explicit loop.
//I know this isn't idiomatic rust, but I couldn't really see a good way of skipping my iterator
//to a location of my choosing.
let mut i = index;
//We don't want our loop index to go past our buffer length or else bad things could occur
let length = buffer.len();
//Keeping it old school with some nice wild loops
while i < length {
//Here's where the main magic occurs
//If we come across a delimiter and we're not using white space as our delimiter we base everything we do based on our previous state.
//If we come across a space or tab we move to the next item in the buffer unless we're using whitespace as delimiter in which case we do
//everything we were doing with the delimiter case
//If we come across a newline character we advance our iterator and move onto the
//next item in the buffer. We also check that our total number of fields are what they should be and if they aren't we error out. Other things
//to note are that outside of the comment and delimiter character case this is the only other place where we'll push the end index for
//our raw data that we've come across.
//If we come across a comment character first (white spaces aren't counted) we completely skip the rest of the line.
//If we come across any other character first (white spaces aren't counted) then we're in a field and we check to see if its one we should skip or not.
//If it is then we set our state to being a skipped field and until we come across a delimiter, comment, or newline we do nothing. The other case is we
//we do want the field, and so we save the data off for later uses.
//If we no longer have an item in our newline iterator we're done with everything in our buffer, and so
//we can exit the loop. However, our state is preserved between loops.
if (buffer[i] == delim) & !delim_ws {
state = match state {
ParseState::NwLine => {
field_counter = 1;
ParseState::Delim
}
ParseState::Delim => ParseState::Delim,
ParseState::SkField => {
field_counter += 1;
ParseState::Delim
}
ParseState::Field => {
field_counter += 1;
results.index.push(results.results.len());
ParseState::Delim
}
ParseState::Space => {
field_counter += 1;
ParseState::Delim
}
};
i += 1;
} else if (buffer[i] == b' ') | (buffer[i] == b'\t') {
if delim_ws {
state = match state {
ParseState::NwLine => {
field_counter = 1;
ParseState::Delim
}
ParseState::Delim => ParseState::Delim,
ParseState::SkField => {
field_counter += 1;
ParseState::Delim
}
ParseState::Field => {
field_counter += 1;
results.index.push(results.results.len());
ParseState::Delim
}
ParseState::Space => {
field_counter += 1;
ParseState::Delim
}
};
} else {
state = match state {
ParseState::NwLine => ParseState::Space,
ParseState::Delim => ParseState::Space,
ParseState::SkField => ParseState::SkField,
ParseState::Field => ParseState::Field,
ParseState::Space => ParseState::Space,
};
}
i += 1;
} else if (buffer[i] == b'\n') | (buffer[i] == b'\r') {
let val = newline.next();
i = match val {
Some(val) => val + 1,
None => length,
};
fln += 1;
state = match state {
ParseState::NwLine => ParseState::NwLine,
ParseState::Delim => {
if delim_ws {
field_counter -= 1;
if results.num_lines == 0 {
tot_fields = field_counter;
results.num_fields = if !cols.is_empty() {
if cols.iter().any(|&x| x > field_counter) {
return Err(format_err!("Input for usecols contains a value greater than or equal to the number of fields {}", field_counter));
} else {
cols.len()
}
} else {
field_counter
};
}
if (field_counter != tot_fields) & (field_counter != 0) {
return Err(format_err!(
"Newline (delim) Number of fields,{}, provided at line {}
is different than the initial field number of {}",
field_counter,
fln,
tot_fields
));
}
field_counter = 0;
results.num_lines += 1;
} else {
return Err(format_err!(
"Number of fields provided at line {}
ends with a delimiter instead of a field or white space",
fln
));
}
ParseState::NwLine
}
ParseState::SkField => {
if results.num_lines == 0 {
tot_fields = field_counter;
results.num_fields = if !cols.is_empty() {
if cols.iter().any(|&x| x > field_counter) {
return Err(format_err!("Input for usecols contains a value greater than or equal to the number of fields {}", field_counter));
} else {
cols.len()
}
} else {
field_counter
};
}
if field_counter != tot_fields {
return Err(format_err!(
"Newline (skip field) Number of fields,{}, provided at line {}
is different than the initial field number of {}",
field_counter,
fln,
tot_fields
));
}
results.num_lines += 1;
field_counter = 0;
ParseState::NwLine
}
ParseState::Field => {
results.index.push(results.results.len());
if results.num_lines == 0 {
tot_fields = field_counter;
results.num_fields = if !cols.is_empty() {
if cols.iter().any(|&x| x > field_counter) {
return Err(format_err!("Input for usecols contains a value greater than or equal to the number of fields {}", field_counter));
} else {
cols.len()
}
} else {
field_counter
};
}
if field_counter != tot_fields {
return Err(format_err!(
"Newline (field) Number of fields,{}, provided at line {}
is different than the initial field number of {}",
field_counter,
fln,
tot_fields
));
}
results.num_lines += 1;
field_counter = 0;
ParseState::NwLine
}
ParseState::Space => ParseState::NwLine,
};
} else if buffer[i] == cmt {
let val = newline.next();
i = match val {
Some(val) => val + 1,
None => length,
};
fln += 1;
state = match state {
ParseState::NwLine => ParseState::NwLine,
ParseState::Delim => {
if delim_ws {
field_counter -= 1;
if (results.num_lines == 0) & (field_counter != 0) {
tot_fields = field_counter;
results.num_fields = if !cols.is_empty() {
if cols.iter().any(|&x| x > field_counter) {
return Err(format_err!("Input for usecols contains a value greater than or equal to the number of fields {}", field_counter));
} else {
cols.len()
}
} else {
field_counter
};
}
if (field_counter != tot_fields) & (field_counter != 0) {
return Err(format_err!(
"Cmt (delim) Number of fields,{}, provided at line {}
is different than the initial field number of {}",
field_counter,
fln,
tot_fields
));
}
if field_counter > 0 {
field_counter = 0;
results.num_lines += 1;
}
} else {
return Err(format_err!(
"Number of fields provided at line {}
ends with a delimiter instead of a field or white space",
fln
));
}
ParseState::NwLine
}
ParseState::SkField => {
if results.num_lines == 0 {
tot_fields = field_counter;
results.num_fields = if !cols.is_empty() {
if cols.iter().any(|&x| x > field_counter) {
return Err(format_err!("Input for usecols contains a value greater than or equal to the number of fields {}", field_counter));
} else {
cols.len()
}
} else {
field_counter
};
}
if field_counter != tot_fields {
return Err(format_err!(
"Cmt (skip field) Number of fields,{}, provided at line {}
is different than the initial field number of {}",
field_counter,
fln,
tot_fields
));
}
results.num_lines += 1;
field_counter = 0;
ParseState::NwLine
}
ParseState::Field => {
if results.num_lines == 0 {
tot_fields = field_counter;
results.num_fields = if !cols.is_empty() {
if cols.iter().any(|&x| x > field_counter) {
return Err(format_err!("Input for usecols contains a value greater than or equal to the number of fields {}", field_counter));
} else {
cols.len()
}
} else {
field_counter
};
}
results.index.push(results.results.len());
if field_counter != tot_fields {
return Err(format_err!(
"Cmt (field) Number of fields,{}, provided at line {}
is different than the initial field number of {}",
field_counter,
fln,
tot_fields
));
}
results.num_lines += 1;
field_counter = 0;
ParseState::NwLine
}
ParseState::Space => ParseState::NwLine,
};
} else {
state = match state {
ParseState::NwLine => {
field_counter = 1;
match &cols.len() {
0 => {
results.results.push(buffer[i]);
ParseState::Field
}
_ => {
let pos = cols.iter().position(|&x| x == field_counter);
match pos {
Some(_x) => {
results.results.push(buffer[i]);
ParseState::Field
}
None => ParseState::SkField,
}
}
}
}
ParseState::Delim => match &cols.len() {
0 => {
results.results.push(buffer[i]);
ParseState::Field
}
_ => {
let pos = cols.iter().position(|&x| x == field_counter);
match pos {
Some(_x) => {
results.results.push(buffer[i]);
ParseState::Field
}
None => ParseState::SkField,
}
}
},
ParseState::SkField => ParseState::SkField,
ParseState::Field => {
results.results.push(buffer[i]);
ParseState::Field
}
ParseState::Space => {
//The case where we start out with spaces before our 1st field at the start of a line
if field_counter == 0 {
field_counter += 1;
}
match &cols.len() {
0 => {
results.results.push(buffer[i]);
ParseState::Field
}
_ => {
let pos = cols.iter().position(|&x| x == field_counter);
match pos {
Some(_x) => {
results.results.push(buffer[i]);
ParseState::Field
}
None => ParseState::SkField,
}
}
}
}
};
i += 1;
}
//Check to see if we've read enough lines in if so break out of the loop
if results.num_lines == num_lines_read {
break;
}
}
//Assumming everything went well we save off our results.
Ok(results)
}