添加预处理相关逻辑, 将筛选逻辑移进预处理逻辑, 添加根据Start, End截取功能
This commit is contained in:
41
src/prehandle.rs
Normal file
41
src/prehandle.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use std::{io::Read, path::PathBuf};
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Cursor};
|
||||
|
||||
pub fn handle(path: PathBuf,filter_str:String, start_index: u32, end_index: u32) -> Box<dyn Read>{
|
||||
let reader = BufReader::new(File::open(path).unwrap());
|
||||
let mut lines = reader.lines().collect::<Result<Vec<_>, _>>().unwrap();
|
||||
lines = filter_by_string(lines, filter_str.as_str());
|
||||
lines = take(&mut lines, start_index, end_index);
|
||||
|
||||
Box::new(Cursor::new(lines.join("\n").into_bytes())) as Box<dyn Read>
|
||||
}
|
||||
|
||||
fn filter_by_string(reader: Vec<String>, filter_str: &str) ->Vec<String>{
|
||||
if filter_str == "" {
|
||||
return reader
|
||||
}
|
||||
let mut result: Vec<String> = Vec::new();
|
||||
result.push(reader[0].clone());
|
||||
for line in reader[1..].iter(){
|
||||
if line.contains(filter_str){
|
||||
result.push(line.to_string());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fn take(reader: &mut Vec<String>, start_index: u32, end_index: u32) -> Vec<String> {
|
||||
if end_index == 0 && start_index == 0{
|
||||
return reader.to_owned();
|
||||
}
|
||||
let header = reader[0].clone();
|
||||
let mut body= Vec::new();
|
||||
body.push(header);
|
||||
if end_index == 0{
|
||||
body.append(&mut reader[start_index as usize..].to_owned());
|
||||
}else{
|
||||
body.append(&mut reader[start_index as usize..end_index as usize].to_owned());
|
||||
}
|
||||
body
|
||||
}
|
||||
Reference in New Issue
Block a user