How to implement trim for Vec<u8>?
Asked Answered
H

4

7

Rust provides a trim method for strings: str.trim() removing leading and trailing whitespace. I want to have a method that does the same for bytestrings. It should take a Vec<u8> and remove leading and trailing whitespace (space, 0x20 and htab, 0x09).

Writing a trim_left() is easy, you can just use an iterator with 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);
}

But to trim the right characters I would need to look ahead if no other letter is in the list after whitespace was found.

Hardnett answered 28/6, 2015 at 16:11 Comment(0)
E
8

Here's an implementation that returns a slice, rather than a new Vec<u8>, as str::trim() does. It's also implemented on [u8], since that's more general than Vec<u8> (you can obtain a slice from a vector cheaply, but creating a vector from a slice is more costly, since it involves a heap allocation and a copy).

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);
}

If you really need a Vec<u8> after the trim(), you can just call into() on the slice to turn it into a Vec<u8>.

fn main() {
    let a = b"     fo o ";
    let b: Vec<u8> = a.trim().into();
    println!("{:?}", b);
}
Euthanasia answered 28/6, 2015 at 17:11 Comment(2)
Using a trait and returning a slice is perfect.Hardnett
I have a slightly improved version of your code, would you mind updating your answer?Hardnett
A
5

This is a much simpler version than the other answers.

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]
}

Weird that this isn't in the standard library. I would have thought it was a common task.

Anyway here it is as a complete file/trait (with tests!) that you can copy/paste.

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"");
    }
}
Aeroscope answered 2/5, 2021 at 15:37 Comment(0)
S
0

All we have to do is find the index of the first non-whitespace character, one time counting forward from the start, and another time counting backwards from the end.

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);
}
Shiver answered 28/6, 2015 at 16:42 Comment(0)
I
0

Combining the solutions given by Francis Gagné and by Timmmm we have:

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

impl SliceExt for [u8] {
    /// https://mcmap.net/q/665748/-how-to-implement-trim-for-vec-lt-u8-gt
    /// 
    /// 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");
    }
}

See the Rust Playground

Irretrievable answered 6/5, 2023 at 14:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.