pub struct OwnedChar { /* private fields */ }Expand description
An owned Char.
Use Char::to_owned() to obtain this.
use mut_str::get_char;
let s = "Hello, World!";
let c = get_char(s, 1).unwrap();
let owned_c = c.to_owned();
assert_eq!(owned_c, 'e');Implementations§
Source§impl OwnedChar
impl OwnedChar
Sourcepub const unsafe fn new_unchecked(b: [u8; 4]) -> Self
pub const unsafe fn new_unchecked(b: [u8; 4]) -> Self
Create a new OwnedChar without checking the validity of the buffer.
For a safe version, see OwnedChar::try_from().
§Safety
There must be one valid UTF-8 encoded character at the start of the b.
Sourcepub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
Create a new OwnedChar without checking the validity of the bytes.
For a safe version, see OwnedChar::try_from().
§Safety
There must be one valid UTF-8 encoded character at the start of bytes, which must be no longer than 4 bytes long.
Sourcepub unsafe fn buffer_mut(&mut self) -> &mut [u8; 4]
pub unsafe fn buffer_mut(&mut self) -> &mut [u8; 4]
Get the underlying buffer as a mutable array.
§Safety
The caller must ensure that when the mutable reference returned is dropped, there is a valid UTF-8 encoded character at the start of the buffer.
Sourcepub const fn into_bytes(self) -> [u8; 4]
pub const fn into_bytes(self) -> [u8; 4]
Get the underlying buffer. This is not guaranteed to be valid UTF-8!
The first self.len() bytes will be valid UTF-8.
use mut_str::OwnedChar;
let c = OwnedChar::from('🌑');
assert_eq!(c.into_bytes(), [240, 159, 140, 145]);Methods from Deref<Target = Char>§
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the length of the character in bytes.
This will be in the range 1..=4.
use mut_str::Char;
let s = "oΦ⏣🌑";
assert_eq!(Char::get(s, 0).unwrap().len(), 1);
assert_eq!(Char::get(s, 1).unwrap().len(), 2);
assert_eq!(Char::get(s, 2).unwrap().len(), 3);
assert_eq!(Char::get(s, 3).unwrap().len(), 4);Examples found in repository?
3fn main() {
4 let mut s = Box::<str>::from("oφ⏣🌑");
5
6 // Iterate over the characters printing their length and bytes
7 s.ref_iter()
8 .for_each(|c| println!("{c:?}\tlength: {}, bytes: {:?}", c.len(), c.as_bytes()));
9
10 // Iterate over each character trying to make them uppercase
11 // (Result::ok is being used to ignore the result)
12 s.mut_iter().for_each(|c| {
13 c.try_make_uppercase().ok();
14 });
15
16 println!("\n{s:?}");
17}Sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
Get a mutable pointer to the character (pointer).
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Get the character as a byte slice (slice).
use mut_str::Char;
let s = "Hello, 🌍!";
let c = Char::get(s, 1).unwrap();
assert_eq!(c.as_bytes(), &[101]);
let c = Char::get(s, 7).unwrap();
assert_eq!(c.as_bytes(), &[240, 159, 140, 141]);Examples found in repository?
3fn main() {
4 let mut s = Box::<str>::from("oφ⏣🌑");
5
6 // Iterate over the characters printing their length and bytes
7 s.ref_iter()
8 .for_each(|c| println!("{c:?}\tlength: {}, bytes: {:?}", c.len(), c.as_bytes()));
9
10 // Iterate over each character trying to make them uppercase
11 // (Result::ok is being used to ignore the result)
12 s.mut_iter().for_each(|c| {
13 c.try_make_uppercase().ok();
14 });
15
16 println!("\n{s:?}");
17}Sourcepub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the character as a str.
use mut_str::Char;
let s = "Hello, 🌍!";
let c = Char::get(s, 1).unwrap();
assert_eq!(c.as_str(), "e");
let c = Char::get(s, 7).unwrap();
assert_eq!(c.as_str(), "🌍");Sourcepub fn as_str_mut(&mut self) -> &mut str
pub fn as_str_mut(&mut self) -> &mut str
Get the character as a mutable str.
use mut_str::Char;
let mut s = Box::<str>::from("Hello, 🌍!");
let c = Char::get_mut(&mut *s, 1).unwrap();
assert_eq!(c.as_str_mut(), "e");
let c = Char::get_mut(&mut *s, 7).unwrap();
assert_eq!(c.as_str_mut(), "🌍");Sourcepub fn as_char(&self) -> char
pub fn as_char(&self) -> char
Get the character as a char.
use mut_str::Char;
let s = "Hello, 🌍!";
let c = Char::get(s, 1).unwrap();
assert_eq!(c.as_char(), 'e');
let c = Char::get(s, 7).unwrap();
assert_eq!(c.as_char(), '🌍');Sourcepub fn copy_to<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a mut Self>
pub fn copy_to<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a mut Self>
Copy the character to a byte buffer and get the str containing the inserted character.
Returns None if buffer is shorter than self.
use mut_str::Char;
let s = "Hello, World!";
let c = Char::get(s, 1).unwrap();
let mut buffer = [0; 4];
let c2 = c.copy_to(&mut buffer).unwrap();
assert_eq!(c2, c);Sourcepub fn replace<C>(&mut self, r: C) -> Result<(), LenNotEqual>
pub fn replace<C>(&mut self, r: C) -> Result<(), LenNotEqual>
Replace the character with another of the same length.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace('e').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
assert!(c.replace('a').is_err());§Errors
- If
r, when utf8 encoded, does not have the same length asself,LenNotEqualwill be returned.
Sourcepub fn replace_with_pad_space<C>(
&mut self,
r: C,
) -> Result<(), ReplacementTooLong>
pub fn replace_with_pad_space<C>( &mut self, r: C, ) -> Result<(), ReplacementTooLong>
Replace the character with another of the same length or shorter. The remaining bytes will be filled with spaces.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace_with_pad_space('e').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
assert!(c.replace_with_pad_space('a').is_ok());
assert_eq!(&*s, "ea ⏣🌑");§Errors
- If
r, when utf8 encoded, is longer thanself,ReplacementTooLongwill be returned.
Sourcepub fn replace_with_pad<C>(
&mut self,
r: C,
pad: u8,
) -> Result<(), ReplaceWithPadError>
pub fn replace_with_pad<C>( &mut self, r: C, pad: u8, ) -> Result<(), ReplaceWithPadError>
Replace the character with another of the same length or shorter.
The remaining bytes will be filled with pad.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace_with_pad('e', b'b').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
assert!(c.replace_with_pad('a', b'b').is_ok());
assert_eq!(&*s, "eab⏣🌑");§Errors
- If
padis not valid utf8,ReplaceWithPadError::InvalidPadwill be returned. - If
r, when utf8 encoded, is longer thanself,ReplaceWithPadError::ReplacementLenwill be returned.
Sourcepub fn replace_with_pad_char<C1, C2>(
&mut self,
r: C1,
pad_char: C2,
) -> Result<(), ReplaceWithPadCharError>
pub fn replace_with_pad_char<C1, C2>( &mut self, r: C1, pad_char: C2, ) -> Result<(), ReplaceWithPadCharError>
Replace the character with another of the same length or shorter.
The remaining bytes will be filled with pad, which must be one byte long.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace_with_pad_char('e', 'b').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
assert!(c.replace_with_pad_char('a', 'b').is_ok());
assert_eq!(&*s, "eab⏣🌑");§Errors
- If
pad_char, when utf8 encoded, is longer thanSelf,ReplaceWithPadCharError::PadCharTooLongwill be returned. - If
r, when utf8 encoded, is longer thanself,ReplaceWithPadCharError::ReplacementLenwill be returned.
Examples found in repository?
3fn main() {
4 let mut s = Box::<str>::from("👋🌍");
5 println!("Original string: {s:?}"); // "👋🌍"
6
7 // Get a mutable reference to the second character
8 let world = s.get_char_mut(1).unwrap();
9 println!("Original character: {world:?}"); // '🌍'
10
11 // Replace '🌍' with 'w' and '_' as padding
12 // '🌍' is 4 bytes long and 'w' is 1, so 3 '_'s will be added after.
13 world.replace_with_pad_char('w', '_').unwrap();
14 println!("Replacement character: {world:?}"); // 'w'
15
16 println!("Final string: {s:?}"); // "👋w___"
17}Sourcepub fn replace_with_pad_left_space<C>(
&mut self,
r: C,
) -> Result<&mut Self, ReplacementTooLong>
pub fn replace_with_pad_left_space<C>( &mut self, r: C, ) -> Result<&mut Self, ReplacementTooLong>
Replace the character with another of the same length or shorter, right aligned. The remaining bytes before the character will be filled with spaces.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace_with_pad_left_space('e').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
let c2 = c.replace_with_pad_left_space('a').unwrap();
assert_eq!(c2, 'a');
assert_eq!(c, ' ');
assert_eq!(&*s, "e a⏣🌑");§Errors
- If
r, when utf8 encoded, is longer thanself,ReplacementTooLongwill be returned.
Sourcepub fn replace_with_pad_left<C>(
&mut self,
r: C,
pad: u8,
) -> Result<&mut Self, ReplaceWithPadError>
pub fn replace_with_pad_left<C>( &mut self, r: C, pad: u8, ) -> Result<&mut Self, ReplaceWithPadError>
Replace the character with another of the same length or shorter, right aligned.
The remaining bytes before the character will be filled with pad.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace_with_pad_left('e', b'b').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
let c2 = c.replace_with_pad_left('a', b'b').unwrap();
assert_eq!(c2, 'a');
assert_eq!(c, 'b');
assert_eq!(&*s, "eba⏣🌑");§Errors
- If
padis not valid utf8,ReplaceWithPadError::InvalidPadwill be returned. - If
r, when utf8 encoded, is longer thanself,ReplaceWithPadError::ReplacementLenwill be returned.
Sourcepub fn replace_with_pad_left_char<C1, C2>(
&mut self,
r: C1,
pad_char: C2,
) -> Result<&mut Self, ReplaceWithPadCharError>
pub fn replace_with_pad_left_char<C1, C2>( &mut self, r: C1, pad_char: C2, ) -> Result<&mut Self, ReplaceWithPadCharError>
Replace the character with another of the same length or shorter, right aligned.
The remaining bytes before the character will be filled with char, which must be one byte long.
use mut_str::Char;
let mut s = Box::<str>::from("oΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
assert!(c.replace_with_pad_left_char('e', 'b').is_ok());
assert_eq!(&*s, "eΦ⏣🌑");
let c = Char::get_mut(&mut *s, 1).unwrap();
let c2 = c.replace_with_pad_left_char('a', 'b').unwrap();
assert_eq!(c2, 'a');
assert_eq!(c, 'b');
assert_eq!(&*s, "eba⏣🌑");§Errors
- If
pad_char, when utf8 encoded, is longer thanSelf,ReplaceWithPadCharError::PadCharTooLongwill be returned. - If
r, when utf8 encoded, is longer thanself,ReplaceWithPadCharError::ReplacementLenwill be returned.
Sourcepub fn is_ascii(&self) -> bool
pub fn is_ascii(&self) -> bool
Checks if the value is within ASCII range.
use mut_str::Char;
let s = "oΦ⏣🌑";
let c = Char::get(s, 0).unwrap();
assert!(c.is_ascii());
let c = Char::get(s, 1).unwrap();
assert!(!c.is_ascii());Sourcepub fn make_ascii_uppercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
Converts this type to its ASCII upper case equivalent in-place.
ASCII letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’, but non-ASCII letters are unchanged.
use mut_str::Char;
let mut s = Box::<str>::from("oφ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
c.make_ascii_uppercase();
let c = Char::get_mut(&mut *s, 1).unwrap();
c.make_ascii_uppercase();
assert_eq!(&*s, "Oφ⏣🌑");Sourcepub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_lowercase(&mut self)
Converts this type to its ASCII lower case equivalent in-place.
ASCII letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’, but non-ASCII letters are unchanged.
use mut_str::Char;
let mut s = Box::<str>::from("OΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
c.make_ascii_lowercase();
let c = Char::get_mut(&mut *s, 1).unwrap();
c.make_ascii_lowercase();
assert_eq!(&*s, "oΦ⏣🌑");Sourcepub fn try_make_uppercase(&mut self) -> Result<(), LenNotEqual>
pub fn try_make_uppercase(&mut self) -> Result<(), LenNotEqual>
Converts this type to its Unicode upper case equivalent in-place.
use mut_str::Char;
let mut s = Box::<str>::from("oφ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
c.try_make_uppercase().unwrap();
let c = Char::get_mut(&mut *s, 1).unwrap();
c.try_make_uppercase().unwrap();
assert_eq!(&*s, "OΦ⏣🌑");§Errors
If the character and its uppercase version is not the same length when utf8 encoded, LenNotEqual will be returned.
Examples found in repository?
3fn main() {
4 let mut s = Box::<str>::from("oφ⏣🌑");
5
6 // Iterate over the characters printing their length and bytes
7 s.ref_iter()
8 .for_each(|c| println!("{c:?}\tlength: {}, bytes: {:?}", c.len(), c.as_bytes()));
9
10 // Iterate over each character trying to make them uppercase
11 // (Result::ok is being used to ignore the result)
12 s.mut_iter().for_each(|c| {
13 c.try_make_uppercase().ok();
14 });
15
16 println!("\n{s:?}");
17}Sourcepub fn try_make_lowercase(&mut self) -> Result<(), LenNotEqual>
pub fn try_make_lowercase(&mut self) -> Result<(), LenNotEqual>
Converts this type to its Unicode lower case equivalent in-place.
use mut_str::Char;
let mut s = Box::<str>::from("OΦ⏣🌑");
let c = Char::get_mut(&mut *s, 0).unwrap();
c.try_make_lowercase().unwrap();
let c = Char::get_mut(&mut *s, 1).unwrap();
c.try_make_lowercase().unwrap();
assert_eq!(&*s, "oφ⏣🌑");§Errors
If the character and its lowercase version is not the same length when utf8 encoded, LenNotEqual will be returned.
Trait Implementations§
Source§impl BorrowMut<Char> for OwnedChar
impl BorrowMut<Char> for OwnedChar
Source§fn borrow_mut(&mut self) -> &mut Char
fn borrow_mut(&mut self) -> &mut Char
Source§impl<'a> Extend<&'a OwnedChar> for String
impl<'a> Extend<&'a OwnedChar> for String
Source§fn extend<T: IntoIterator<Item = &'a OwnedChar>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a OwnedChar>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a> Extend<&'a mut OwnedChar> for String
impl<'a> Extend<&'a mut OwnedChar> for String
Source§fn extend<T: IntoIterator<Item = &'a mut OwnedChar>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a mut OwnedChar>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Extend<OwnedChar> for String
impl Extend<OwnedChar> for String
Source§fn extend<T: IntoIterator<Item = OwnedChar>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = OwnedChar>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)