英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论