Skip to content

Commit

Permalink
Add const fn options for case conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Oct 19, 2024
1 parent 2d050b5 commit d13a241
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl<const N: usize> StrBuf<N> {
}

#[inline]
#[allow(clippy::missing_transmute_annotations)]
///Returns slice to already written data.
pub const fn as_slice(&self) -> &[u8] {
//Layout is: (<ptr>, <usize>)
Expand Down Expand Up @@ -344,6 +345,24 @@ impl<const N: usize> StrBuf<N> {
}
}

#[inline(always)]
///Modifies this string to convert all its characters into ASCII lower case equivalent
pub const fn into_ascii_lowercase(mut self) -> Self {
let len = self.len();
let mut idx = 0;
loop {
if idx >= len {
break;
}

self.inner[idx] = unsafe {
mem::MaybeUninit::new(self.inner[idx].assume_init().to_ascii_lowercase())
};
idx = idx.saturating_add(1);
}
self
}

#[inline(always)]
///Converts this string to its ASCII lower case equivalent in-place.
pub fn make_ascii_lowercase(&mut self) {
Expand All @@ -352,6 +371,25 @@ impl<const N: usize> StrBuf<N> {
}
}


#[inline(always)]
///Modifies this string to convert all its characters into ASCII upper case equivalent
pub const fn into_ascii_uppercase(mut self) -> Self {
let len = self.len();
let mut idx = 0;
loop {
if idx >= len {
break;
}

self.inner[idx] = unsafe {
mem::MaybeUninit::new(self.inner[idx].assume_init().to_ascii_uppercase())
};
idx = idx.saturating_add(1);
}
self
}

#[inline(always)]
///Converts this string to its ASCII upper case equivalent in-place.
pub fn make_ascii_uppercase(&mut self) {
Expand Down
6 changes: 6 additions & 0 deletions tests/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ type SmolStr = StrBuf<5>;
fn should_correctly_convert_ascii_case() {
let mut buf = SmolStr::new();
assert_eq!(buf.push_str("ロri"), "ロri".len());

let buf_copy = buf.clone().into_ascii_uppercase();
buf.make_ascii_uppercase();
assert_eq!(buf, "ロRI");
assert_eq!(buf_copy, "ロRI");

let buf_copy = buf.clone().into_ascii_lowercase();
buf.make_ascii_lowercase();
assert_eq!(buf, "ロri");
assert_eq!(buf_copy, "ロri");
}

#[test]
Expand Down

0 comments on commit d13a241

Please sign in to comment.