1
0
Fork 0
forked from wry/wry

utils: remove AsciiTrim trait

This commit is contained in:
Julian Orth 2025-01-06 20:09:33 +01:00
parent 0d1aeeab45
commit 32cb00444b
7 changed files with 22 additions and 53 deletions

View file

@ -1,5 +1,5 @@
use {
crate::utils::{errorfmt::ErrorFmt, oserror::OsError, trim::AsciiTrim},
crate::utils::{errorfmt::ErrorFmt, oserror::OsError},
bstr::ByteSlice,
uapi::{c, OwnedFd},
};
@ -12,7 +12,7 @@ pub struct PidInfo {
pub fn get_pid_info(uid: c::uid_t, pid: c::pid_t) -> PidInfo {
let comm = match std::fs::read(format!("/proc/{}/comm", pid)) {
Ok(name) => name.trim().as_bstr().to_string(),
Ok(name) => name.trim_ascii_end().as_bstr().to_string(),
Err(e) => {
log::warn!("Could not read `comm` of pid {}: {}", pid, ErrorFmt(e));
"Unknown".to_string()

View file

@ -1,33 +0,0 @@
pub trait AsciiTrim {
fn trim(&self) -> &[u8];
fn trim_start(&self) -> &[u8];
fn trim_end(&self) -> &[u8];
}
impl AsciiTrim for [u8] {
fn trim(&self) -> &[u8] {
self.trim_start().trim_end()
}
fn trim_start(&self) -> &[u8] {
let mut s = self;
while let Some((b, r)) = s.split_first() {
if !matches!(*b, b' ' | b'\t' | b'\n') {
break;
}
s = r;
}
s
}
fn trim_end(&self) -> &[u8] {
let mut s = self;
while let Some((b, r)) = s.split_last() {
if !matches!(*b, b' ' | b'\t' | b'\n') {
break;
}
s = r;
}
s
}
}