rust Vec如何实现trim< u8>?

zbsbpyhn  于 2023-05-07  发布在  其他
关注(0)|答案(4)|浏览(125)

Rust为字符串提供了一个trim方法:trim()删除前导和尾随空格。我想有一个方法,对字节串做同样的事情。它应该接受一个Vec<u8>并删除前导和尾随空格(空格,0x20和htab,0x09)。
编写trim_left()很容易,你可以使用skip_while()的迭代器:Rust Playground

fn main() {
    let a: &[u8] = b"     fo o ";
    let b: Vec<u8> = a.iter().map(|x| x.clone()).skip_while(|x| x == &0x20 || x == &0x09).collect();
    println!("{:?}", b);
}

但是要修剪正确的字符,如果在发现空白之后列表中没有其他字母,我需要向前看。

f45qwnt8

f45qwnt81#

下面的实现返回一个切片,而不是像str::trim()那样返回一个新的Vec<u8>。它也在[u8]上实现,因为它比Vec<u8>更通用(您可以从向量中廉价地获得切片,但从切片创建向量的成本更高,因为它涉及堆分配和副本)。

trait SliceExt {
    fn trim(&self) -> &Self;
}

impl SliceExt for [u8] {
    fn trim(&self) -> &[u8] {
        fn is_whitespace(c: &u8) -> bool {
            *c == b'\t' || *c == b' '
        }

        fn is_not_whitespace(c: &u8) -> bool {
            !is_whitespace(c)
        }

        if let Some(first) = self.iter().position(is_not_whitespace) {
            if let Some(last) = self.iter().rposition(is_not_whitespace) {
                &self[first..last + 1]
            } else {
                unreachable!();
            }
        } else {
            &[]
        }
    }
}

fn main() {
    let a = b"     fo o ";
    let b = a.trim();
    println!("{:?}", b);
}

如果在trim()之后确实需要一个Vec<u8>,则只需在切片上调用into()将其转换为Vec<u8>

fn main() {
    let a = b"     fo o ";
    let b: Vec<u8> = a.trim().into();
    println!("{:?}", b);
}
a0x5cqrl

a0x5cqrl2#

这是一个比其他答案简单得多的版本。

pub fn trim_ascii_whitespace(x: &[u8]) -> &[u8] {
    let from = match x.iter().position(|x| !x.is_ascii_whitespace()) {
        Some(i) => i,
        None => return &x[0..0],
    };
    let to = x.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
    &x[from..=to]
}

奇怪的是,这不是在标准库。我会认为这是一个共同的任务。
无论如何,这里是一个完整的文件/trait(带有测试!),您可以复制/粘贴。

use std::ops::Deref;

/// Trait to allow trimming ascii whitespace from a &[u8].
pub trait TrimAsciiWhitespace {
    /// Trim ascii whitespace (based on `is_ascii_whitespace()`) from the
    /// start and end of a slice.
    fn trim_ascii_whitespace(&self) -> &[u8];
}

impl<T: Deref<Target=[u8]>> TrimAsciiWhitespace for T {
    fn trim_ascii_whitespace(&self) -> &[u8] {
        let from = match self.iter().position(|x| !x.is_ascii_whitespace()) {
            Some(i) => i,
            None => return &self[0..0],
        };
        let to = self.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
        &self[from..=to]
    }
}

#[cfg(test)]
mod test {
    use super::TrimAsciiWhitespace;

    #[test]
    fn basic_trimming() {
        assert_eq!(b" A ".trim_ascii_whitespace(), b"A");
        assert_eq!(b" AB ".trim_ascii_whitespace(), b"AB");
        assert_eq!(b"A ".trim_ascii_whitespace(), b"A");
        assert_eq!(b"AB ".trim_ascii_whitespace(), b"AB");
        assert_eq!(b" A".trim_ascii_whitespace(), b"A");
        assert_eq!(b" AB".trim_ascii_whitespace(), b"AB");
        assert_eq!(b" A B ".trim_ascii_whitespace(), b"A B");
        assert_eq!(b"A B ".trim_ascii_whitespace(), b"A B");
        assert_eq!(b" A B".trim_ascii_whitespace(), b"A B");
        assert_eq!(b" ".trim_ascii_whitespace(), b"");
        assert_eq!(b"  ".trim_ascii_whitespace(), b"");
    }
}
bsxbgnwa

bsxbgnwa3#

我们所要做的就是找到第一个非空格字符的索引,一次从开始向前计数,另一次从结束向后计数。

fn is_not_whitespace(e: &u8) -> bool {
    *e != 0x20 && *e != 0x09
}

fn main() {
    let a: &[u8] = b"     fo o ";

    // find the index of first non-whitespace char
    let begin = a.iter()
        .position(is_not_whitespace);

    // find the index of the last non-whitespace char
    let end = a.iter()
        .rev()
        .position(is_not_whitespace)
        .map(|j| a.len() - j);

    // build it
    let vec = begin.and_then(|i| end.map(|j| a[i..j].iter().collect()))
        .unwrap_or(Vec::new());

    println!("{:?}", vec);
}
nhaq1z21

nhaq1z214#

结合Francis GagnéTimmmm给出的解,我们有:

/// Trait to allow trimming ascii whitespace from a &[u8].
pub trait SliceExt {
    fn trim(&self) -> &Self;
}

impl SliceExt for [u8] {
    /// https://stackoverflow.com/questions/31101915/how-to-implement-trim-for-vecu8
    /// 
    /// Trim ascii whitespace (based on is_ascii_whitespace())
    /// from the start and end of &\[u8\].
    /// 
    /// Returns &\[u8\] with leading and trailing whitespace removed.
    fn trim(&self) -> &[u8] {
        let from = match self.iter().position(|x| !x.is_ascii_whitespace()) {
            Some(i) => i,
            None => return &self[0..0],
        };
        let to = self.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
        &self[from..=to]
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn basic_trimming() {
        // cargo test -- basic_trimming
        assert_eq!(b" A ".trim(), b"A");
        assert_eq!(b" AB ".trim(), b"AB");
        assert_eq!(b"A ".trim(), b"A");
        assert_eq!(b"AB ".trim(), b"AB");
        assert_eq!(b" A".trim(), b"A");
        assert_eq!(b" AB".trim(), b"AB");
        assert_eq!(b" A B ".trim(), b"A B");
        assert_eq!(b"A B ".trim(), b"A B");
        assert_eq!(b" A B".trim(), b"A B");
        assert_eq!(b" ".trim(), b"");
        assert_eq!(b"  ".trim(), b"");
        assert_eq!(b"\nA\n".trim(), b"A");
        assert_eq!(b"\nA  B\r\n".trim(), b"A  B");
        assert_eq!(b"\r\nA  B\r\n".trim(), b"A  B");
    }
}

参见Rust Playground

相关问题