Skip to content

Commit

Permalink
Add make_trim functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Nov 18, 2024
1 parent d6287bd commit 0584542
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
84 changes: 80 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,8 @@ impl<const N: usize> StrBuf<N> {
self.inner[1] = mem::MaybeUninit::new(len[1]);
} else {
let len = len.to_ne_bytes();
unsafe {
let ptr = self.inner.as_mut_ptr();
ptr::copy_nonoverlapping(len.as_ptr(), ptr as *mut _, mem::size_of::<usize>());
}
let ptr = self.inner.as_mut_ptr();
ptr::copy_nonoverlapping(len.as_ptr(), ptr as *mut _, mem::size_of::<usize>());
}
}

Expand Down Expand Up @@ -462,6 +460,84 @@ impl<const N: usize> StrBuf<N> {
}
}

///Trims of whitespaces on the right in place.
pub fn make_trim(&mut self) {
let this = self.as_str();
let len = this.len();
let mut trim_left_count = 0usize;
let mut trim_right_count = 0usize;
let mut chars = this.chars();
while let Some(ch) = chars.next() {
if ch.is_whitespace() {
trim_left_count = trim_left_count.saturating_add(ch.len_utf8());
} else {
break;
}
}

for ch in chars.rev() {
if ch.is_whitespace() {
trim_right_count = trim_right_count.saturating_add(ch.len_utf8());
} else {
break;
}
}

let new_len = len.saturating_sub(trim_left_count).saturating_sub(trim_right_count);
if new_len != len {
unsafe {
//To make sure Miri doesn't complain, you have to derive both pointers from the same one otherwise Miri will 're-borrow' for no reason
let dest = self.as_mut_ptr();
let src = dest.add(trim_left_count) as *const _;
ptr::copy(src, dest, new_len);
self.set_len(new_len);
}
}
}

#[inline]
///Trims of whitespaces on the right in place.
pub fn make_trim_left(&mut self) {
let this = self.as_str();
let len = this.len();
let mut trim_count = 0usize;
for ch in this.chars() {
if ch.is_whitespace() {
trim_count = trim_count.saturating_add(ch.len_utf8());
} else {
break;
}
}

let new_len = len.saturating_sub(trim_count);
unsafe {
let dest = self.as_mut_ptr();
let src = dest.add(trim_count);
ptr::copy(src, dest, new_len);
self.set_len(new_len);
}
}

#[inline]
///Trims of whitespaces on the right in place.
pub fn make_trim_right(&mut self) {
let this = self.as_str();
let len = this.len();
let mut trim_count = 0usize;
for ch in this.chars().rev() {
if ch.is_whitespace() {
trim_count = trim_count.saturating_add(ch.len_utf8());
} else {
break;
}
}

unsafe {
self.set_len(len.saturating_sub(trim_count))
}
}

#[inline]
///Removes last character from the buffer, if any present
pub fn pop(&mut self) -> Option<char> {
let ch = self.chars().rev().next()?;
Expand Down
27 changes: 27 additions & 0 deletions tests/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ type SmolStr = StrBuf<6>;
type MediumStr = StrBuf<290>;
type BigStr = StrBuf<67_000>;

#[test]
fn should_trim() {
let mut string = MediumStr::from_str(" \ttest\t\n ");
string.make_trim();
assert_eq!(string, "test");
string.make_trim();
assert_eq!(string, "test");
}

#[test]
fn should_trim_right() {
let mut string = MediumStr::from_str(" \ttest\t\n ");
string.make_trim_right();
assert_eq!(string, " \ttest");
string.make_trim_right();
assert_eq!(string, " \ttest");
}

#[test]
fn should_trim_left() {
let mut string = MediumStr::from_str(" \ttest\t\n ");
string.make_trim_left();
assert_eq!(string, "test\t\n ");
string.make_trim_left();
assert_eq!(string, "test\t\n ");
}

#[test]
fn should_return_error_on_fmt_write_overflow() {
let mut buf = SmolStr::new();
Expand Down

0 comments on commit 0584542

Please sign in to comment.