brique/
parse_test_csv.rs

1use std::fs::read;
2
3use crate::matrix::Matrix;
4
5// 44 -> ,
6// 10 -> \n
7// 32 -> space
8pub fn parse_test_csv(file_name: String) -> Vec<Matrix> {
9    let binary = read(file_name).unwrap();
10
11    let rows: Vec<_> = binary.split(|&v| v == 10 as u8).collect();
12    let mut extracted_data: Vec<f64> = vec![];
13
14    let mut height: usize = 0;
15    let mut width: usize = 0;
16
17    let mut output_matrices: Vec<Matrix> = vec![];
18
19    for r in rows {
20        if is_line_empty(r) {
21            output_matrices.push(Matrix::init(height, width, extracted_data.clone()));
22
23            extracted_data = vec![];
24            width = 0;
25            height = 0;
26            continue;
27        }
28
29        if r.len() > 0 {
30            let mut tmp: Vec<f64> = tokenizer_f64(std::str::from_utf8(r).unwrap());
31            if width == 0 {
32                width = tmp.len();
33            } else {
34                assert_eq!(tmp.len(), width, "Error, not the same width");
35            }
36            extracted_data.append(&mut tmp);
37            height += 1;
38        }
39    }
40
41    output_matrices
42}
43
44pub fn tokenizer_f64(line: &str) -> Vec<f64> {
45    line.split(",")
46        .filter(|s| !s.is_empty())
47        .filter_map(|s| match s.parse::<f64>() {
48            Ok(res) => Some(res),
49            Err(e) => panic!("CSV tockenizer error, {:?}", e),
50        })
51        .collect::<Vec<_>>()
52}
53
54pub fn is_line_empty(line: &[u8]) -> bool {
55    let mut output = true;
56    for byte in line {
57        if *byte != 44 as u8 && *byte != 10 as u8 && *byte != 32 as u8 {
58            output = false;
59            break;
60        }
61    }
62
63    output
64}