|
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
本帖最后由 DDD888 于 2021-8-28 09:10 编辑
酸菜小鱼1212 发表于 2021-8-24 14:04 
我我我……完全听不懂你在说啥……就是感觉好厉害的样子
给个具体的例子,就是将windows iis log文件里记录的user agent统计下是否是chrome, iphone, ipad, android等统计下输出,这是其中的一个函数用到了filter,我感觉我这写法很精简,题外话,我也喜欢分析数据啦 ,同时赞一下rust,这程序扫描800多兆字节的文本文件,只花了三秒时间在i7第四代cpu上,真是好快
use std::collections::HashMap;
use std::io::{self};
use std::path: ath;
use crate::hashmap_insert_increment_key::hashmap_insert_increment_key;
use crate::read_lines::read_lines;
pub fn get_user_agent_dictionary< >(
file_name: P,
line_count: &mut i32,
) -> io::Result<(HashMap<String, i32>, HashMap<String, i32>)>
where
P: AsRef< ath>,
{
let key = vec![
"iPhone",
"iPad",
"Macintosh",
"Android",
"Chrome",
"Firefox",
];
let mut user_agent_dictionary = HashMap::new();
let mut os_dictionary = HashMap::new();
if let Ok(lines) = read_lines(file_name) {
for line in lines {
if let Ok(_line) = line {
if _line.starts_with("#") {
// comment line, ignore
continue;
}
*line_count = *line_count + 1;
let vec = _line.split(" ").collect::<Vec<&str>>();
let user_agent = vec[9];
hashmap_insert_increment_key(&mut user_agent_dictionary, user_agent);
key.iter()
.filter(|value| user_agent.contains(*value))
.for_each(|value| hashmap_insert_increment_key(&mut os_dictionary, value));
}
}
}
Ok((user_agent_dictionary, os_dictionary))
}
|
评分
-
查看全部评分
|