我有一个 Vec<u8>
我想在 \n
上拆分并逐行处理。在某些时候有一个空行,我想在一个 block 中处理整个向量的其余部分(而不是逐行)。这种事情在 HTTP 或 Git 提交对象中很常见。
例如:
key: value
otherkey: othervalue
This is the content
that is now just a big
block. I don't care about
newlines here.
有没有一种优雅的方法可以用 Rust 解析它?我可以这样拆分它:
pub fn main() {
let data: Vec<u8> = "key: value\notherkey: othervalue\n\nThis is the content\nthat is now just a big\nblock. I don't care about\nnewlines here.".as_bytes().to_owned();
for line in data.split(|&c| c == '\n' as u8) {
println!("Line: {:?}", line);
if line.len() == 0 {
// Start of message...
} else {
// Header
}
}
}
但是当我到达 \n\n
我找不到一种方式说“从这里给我剩余的 Vec
”。如果有 split()
的形式会很容易返回带索引的切片,而不是实际内容,但我似乎找不到。
有没有一种优雅的方法可以做到这一点,而不只是拆分消息然后将它们重新组合在一起?
最佳答案
您可以简单地获取每个切片的长度,跟踪当前偏移量并自己完成最后一个切片:
static DATA: &[u8] = br#"key: value
otherkey: othervalue
This is the content
that is now just a big
block. I don't care about
newlines here.
"#;
pub fn main() {
let mut offset = 0;
for line in DATA.split(|&c| c == '\n' as u8) {
offset += line.len() + 1; // Include the newline.
if line.len() == 0 {
break;
} else {
// Header
println!("{:?}", line);
}
}
let body = &DATA[offset..];
println!("{:?}", body);
}
另见:
关于split - 如何按换行符处理数据,直到找到一行中的两个换行符,然后处理所有剩余数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499047/