如何从 bytes crate 中修剪 BytesMut?

huangapple go评论54阅读模式
英文:

How to trim a BytesMut from bytes crate?

问题

我想要能够将BytesMut 进行剪裁,即使我有一个BytesMut,我想要能够使它trim_bytes

let some_bytes = BytesMut::from("  hello world  ");
let trim_bytes = some_bytes.some_trim_method();
// trim_bytes = BytesMut::From("hello world");   

some_trim_method() 是我一直在寻找的方法,但是在这个库中并没有这样的方法。

英文:

Suppose I have a BytesMut, I want to be able to make it trim_bytes.

let some_bytes = BytesMut::from("  hello world  ");
let trim_bytes = some_bytes.some_trim_method();
// trim_bytes = BytesMut::From("hello world");   

some_trim_method() is what I've been looking for but there isn't such method in crate.

答案1

得分: 1

你还可以为 bytes 创建自己的 trim 函数的版本。

const TAB: &u8 = &b'\t';
const SPACE: &u8 = &b' ';

fn is_whitespace(c: &u8) -> bool {
    c == TAB || c == SPACE
}

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

fn trim_bytes(s: &[u8]) -> &[u8] {
  let l = s.len();
  let (mut i, mut j, mut k) = (0, l - 1, 0);
  loop {
      if (is_not_whitespace(&s[i]) && is_not_whitespace(&s[j])) || i > j || k >= l {
          break;
      }

      if is_whitespace(&s[i]) {
          i += 1;
      }

      if is_whitespace(&s[j]) {
          j -= 1;
      }

      k += 1
  }
  return &s[i..=j];
}

fn main() {
    let result = trim_bytes(&some_bytes);
    println!("{:?}", result);
    assert_eq!(b"hello world", result);
}

或者在 byte 类型上实现 trim 方法。

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

impl TrimBytes for [u8] {
    fn trim(&self) -> &[u8] {
        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 result = some_bytes.trim();
    println!("{:?}", result);
    assert_eq!(b"hello world", result);
}
英文:

You can also create your own version of trim function for bytes

const TAB: &u8 = &b'\t';
const SPACE: &u8 = &b' ';

fn is_whitespace(c: &u8) -> bool {
    c == TAB || c == SPACE
}

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

fn trim_bytes(s: &[u8]) -> &[u8] {
  let l = s.len();
  let (mut i, mut j, mut k) = (0, l - 1, 0);
  loop {
      if (is_not_whitespace(&s[i]) && is_not_whitespace(&s[j])) || i > j || k >= l {
          break;
      }

      if is_whitespace(&s[i]) {
          i += 1;
      }

      if is_whitespace(&s[j]) {
          j -= 1;
      }

      k += 1
  }
  return &s[i..=j];
}

fn main() {
    let result = trim_bytes(&some_bytes);
    println!("{:?}", result);
    assert_eq!(b"hello world", result);
}

or implement trim method on byte type

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

impl TrimBytes for [u8] {
    fn trim(&self) -> &[u8] {
        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 result = some_bytes.trim();
    println!("{:?}", result);
    assert_eq!(b"hello world", result);
}

huangapple
  • 本文由 发表于 2023年2月24日 00:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定