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
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn doc_test() {
let mut s1 = String::new();
let callback = |s : String| {
if s != String::from("foo"){
panic!();
}
s1 = s;
};
assert_eq!(string_parser("./text", "'", end_filter, callback).unwrap(), ());
fn end_filter(c : Vec<char>) -> bool{
for char in &c {
print!("{}", char);
}
print!("\n");
if c.last().unwrap() == &'\'' {
println!("end filter");
return true;
}
else {
return false;
}
}
}
}
pub fn string_parser(path : &str,text : &str, end_filter : impl Fn(Vec<char>) -> bool ,mut callback : impl FnMut(String)) -> Result<(), io::Error> {
let mut inside : bool = false;
let mut first : bool = true;
let mut string_buffer = String::new();
let mut file_buf = String::new();
let f = File::open(path)?;
let mut f = BufReader::new(f);
f.read_to_string(&mut file_buf)?;
let mut buff : Vec<char> = vec![' '; text.len()];
for c in file_buf.chars() {
let mut i : usize = 0;
while i < buff.len() -1 {
buff[i] = buff[i+1];
i += 1;
}
buff[i] = c;
i = 0;
if inside && !end_filter(buff.clone()){
string_buffer.push(c);
}
else if inside && !first {
inside = false;
callback(string_buffer.clone());
string_buffer.clear();
}
else {
while i < buff.len(){
if buff[i] != text.chars().nth(i).unwrap() {
break;
}
i += 1;
}
if i == text.len() {
inside = true;
first = false;
}
}
}
Ok(())
}