pub struct Char { /* private fields */ }Expand description
A UTF-8 encoded character.
This type can only be obtained as a reference or mutable reference similarly to str.
use mut_str::Char;
let s = "Hello, World!";
let c = Char::get(s, 1).unwrap();
assert_eq!(c, 'e');Implementations§
Source§impl Char
impl Char
Sourcepub const unsafe fn new_unchecked(p: *const u8) -> *const Self
pub const unsafe fn new_unchecked(p: *const u8) -> *const Self
Create a new character reference from a pointer to a character.
§Safety
p must be a pointer to the first byte of a valid UTF-8 character.
Sourcepub const unsafe fn new_unchecked_mut(p: *mut u8) -> *mut Self
pub const unsafe fn new_unchecked_mut(p: *mut u8) -> *mut Self
Create a new mutable character reference from a mutable pointer to a character.
§Safety
p must be a mutable pointer to the first byte of a valid UTF-8 character that
can be mutated. String literals cannot be mutated.
Sourcepub fn get(s: &str, i: usize) -> Option<&Self>
pub fn get(s: &str, i: usize) -> Option<&Self>
Get a character reference from a str and an index.
use mut_str::Char;
let s = "Hello, World!";
let c = Char::get(s, 1).unwrap();
assert_eq!(c, 'e');Sourcepub fn get_mut(s: &mut str, i: usize) -> Option<&mut Self>
pub fn get_mut(s: &mut str, i: usize) -> Option<&mut Self>
Get a mutable character reference from a mutable str and an index.
use mut_str::Char;
let mut s = Box::<str>::from("Hello, World!");
let c = Char::get_mut(&mut *s, 1).unwrap();
assert_eq!(c, 'e');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 const fn is_ascii(&self) -> bool
pub const 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 BorrowMut<str> for Char
impl BorrowMut<str> for Char
Source§fn borrow_mut(&mut self) -> &mut str
fn borrow_mut(&mut self) -> &mut str
Source§impl<'a> Extend<&'a Char> for String
impl<'a> Extend<&'a Char> for String
Source§fn extend<T: IntoIterator<Item = &'a Char>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a Char>>(&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 Char> for String
impl<'a> Extend<&'a mut Char> for String
Source§fn extend<T: IntoIterator<Item = &'a mut Char>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a mut Char>>(&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> FromIterator<&'a Char> for String
impl<'a> FromIterator<&'a Char> for String
Source§impl<'a> FromIterator<&'a mut Char> for String
impl<'a> FromIterator<&'a mut Char> for String
Source§impl Ord for Char
impl Ord for Char
Source§impl PartialOrd<Char> for char
impl PartialOrd<Char> for char
Source§impl PartialOrd<Char> for str
impl PartialOrd<Char> for str
Source§impl PartialOrd<OwnedChar> for Char
impl PartialOrd<OwnedChar> for Char
Source§impl PartialOrd<char> for Char
impl PartialOrd<char> for Char
Source§impl PartialOrd<str> for Char
impl PartialOrd<str> for Char
Source§impl PartialOrd for Char
impl PartialOrd for Char
Source§impl ToOwned for Char
Available on crate feature alloc only.
impl ToOwned for Char
alloc only.