split - 如何按换行符处理数据,直到找到一行中的两个换行符,然后处理所有剩余数据?

标签 split rust

我有一个 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/

相关文章:

java - java中字符串的正则表达式

Java Regex - 按空格分割字符串 - 忽略引号和转义引号中的空格

json - 根据用户角色返回 JSON

rust - 有没有一种安全的方法可以从 Rust 中的可变引用中临时检索拥有的值?

C# - 将可执行文件路径和参数拆分为两个字符串

java - 基于 Ç 的字符串分割

io - 如何从 Rust 1.0 中的缓冲区读取整数?

rust - 在 log4rs 中动态设置文件路径

asynchronous - 在轮询时唤醒 Rust future 是否有效?

java - 在java中将大字符串拆分为最大长度的行