From 32cb00444badbdfe0012fbdacffd40693ea45c38 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Mon, 6 Jan 2025 20:09:33 +0100 Subject: [PATCH] utils: remove AsciiTrim trait --- src/gfx_apis/gl/ext.rs | 15 ++++++--------- src/libinput.rs | 8 ++++++-- src/utils.rs | 1 - src/utils/pid_info.rs | 4 ++-- src/utils/trim.rs | 33 --------------------------------- src/video/drm/sys.rs | 4 ++-- src/xkbcommon.rs | 10 ++++++---- 7 files changed, 22 insertions(+), 53 deletions(-) delete mode 100644 src/utils/trim.rs diff --git a/src/gfx_apis/gl/ext.rs b/src/gfx_apis/gl/ext.rs index 7eaa2699..3bfbfa65 100644 --- a/src/gfx_apis/gl/ext.rs +++ b/src/gfx_apis/gl/ext.rs @@ -1,12 +1,9 @@ use { - crate::{ - gfx_apis::gl::{ - egl::sys::{EGLDisplay, EGL_EXTENSIONS}, - gl::sys::GL_EXTENSIONS, - sys::{EGL, GLESV2}, - RenderError, - }, - utils::trim::AsciiTrim, + crate::gfx_apis::gl::{ + egl::sys::{EGLDisplay, EGL_EXTENSIONS}, + gl::sys::GL_EXTENSIONS, + sys::{EGL, GLESV2}, + RenderError, }, ahash::AHashSet, bstr::ByteSlice, @@ -21,7 +18,7 @@ unsafe fn get_extensions(ext: *const c::c_char) -> Option> { let mut res = AHashSet::new(); let ext = unsafe { CStr::from_ptr(ext).to_bytes() }; for part in ext.split_str(" ") { - let name = part.trim(); + let name = part.trim_ascii(); if name.len() > 0 { if let Ok(s) = str::from_utf8(name) { res.insert(s.to_string()); diff --git a/src/libinput.rs b/src/libinput.rs index 44512f3c..64a26967 100644 --- a/src/libinput.rs +++ b/src/libinput.rs @@ -22,7 +22,7 @@ use { }, }, udev::UdevError, - utils::{errorfmt::ErrorFmt, oserror::OsError, ptr_ext::PtrExt, trim::AsciiTrim}, + utils::{errorfmt::ErrorFmt, oserror::OsError, ptr_ext::PtrExt}, }, bstr::ByteSlice, isnt::std_1::primitive::IsntConstPtrExt, @@ -182,5 +182,9 @@ unsafe extern "C" fn jay_libinput_log_handler( LIBINPUT_LOG_PRIORITY_ERROR => log::Level::Error, _ => log::Level::Error, }; - log::log!(priority, "libinput: {}", str.to_bytes().trim().as_bstr()); + log::log!( + priority, + "libinput: {}", + str.to_bytes().trim_ascii().as_bstr() + ); } diff --git a/src/utils.rs b/src/utils.rs index eb098c6e..96d65756 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -57,7 +57,6 @@ pub mod timer; pub mod toplevel_identifier; pub mod transform_ext; pub mod tri; -pub mod trim; pub mod unlink_on_drop; pub mod vec_ext; pub mod vecdeque_ext; diff --git a/src/utils/pid_info.rs b/src/utils/pid_info.rs index 4e929e22..f5197a96 100644 --- a/src/utils/pid_info.rs +++ b/src/utils/pid_info.rs @@ -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() diff --git a/src/utils/trim.rs b/src/utils/trim.rs deleted file mode 100644 index 0c6ba2d2..00000000 --- a/src/utils/trim.rs +++ /dev/null @@ -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 - } -} diff --git a/src/video/drm/sys.rs b/src/video/drm/sys.rs index 7e58c234..19dcae09 100644 --- a/src/video/drm/sys.rs +++ b/src/video/drm/sys.rs @@ -3,7 +3,7 @@ use { crate::{ - utils::{bitflags::BitflagsExt, oserror::OsError, trim::AsciiTrim}, + utils::{bitflags::BitflagsExt, oserror::OsError}, video::drm::{ DrmBlob, DrmCardResources, DrmConnector, DrmConnectorInfo, DrmCrtc, DrmEncoder, DrmEncoderInfo, DrmError, DrmFb, DrmModeInfo, DrmPlane, DrmPlaneInfo, DrmProperty, @@ -163,7 +163,7 @@ pub fn get_device_name_from_fd2(fd: c::c_int) -> Result { break; } if let Some(pf) = buf.strip_prefix(b"DEVNAME=") { - return Ok(uapi::format_ustr!("/dev/{}", pf.trim_end().as_bstr())); + return Ok(uapi::format_ustr!("/dev/{}", pf.trim_ascii_end().as_bstr())); } } Err(OsError(c::ENOENT)) diff --git a/src/xkbcommon.rs b/src/xkbcommon.rs index 9e8ce87c..a1ca9252 100644 --- a/src/xkbcommon.rs +++ b/src/xkbcommon.rs @@ -6,9 +6,7 @@ include!(concat!(env!("OUT_DIR"), "/xkbcommon_tys.rs")); pub use consts::*; use { - crate::utils::{ - errorfmt::ErrorFmt, oserror::OsError, ptr_ext::PtrExt, trim::AsciiTrim, vecset::VecSet, - }, + crate::utils::{errorfmt::ErrorFmt, oserror::OsError, ptr_ext::PtrExt, vecset::VecSet}, bstr::{BStr, ByteSlice}, isnt::std_1::primitive::IsntConstPtrExt, std::{ @@ -423,5 +421,9 @@ unsafe extern "C" fn jay_xkbcommon_log_handler( XKB_LOG_LEVEL_DEBUG => log::Level::Debug, _ => log::Level::Error, }; - log::log!(level, "xkbcommon: {}", buf.to_bytes().trim_end().as_bstr()); + log::log!( + level, + "xkbcommon: {}", + buf.to_bytes().trim_ascii_end().as_bstr(), + ); }