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?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() {
let mut s = Box::<str>::from("oφ⏣🌑");
// Iterate over the characters printing their length and bytes
s.ref_iter()
.for_each(|c| println!("{c:?}\tlength: {}, bytes: {:?}", c.len(), c.as_bytes()));
// Iterate over each character trying to make them uppercase
// (Result::ok is being used to ignore the result)
s.mut_iter().for_each(|c| {
c.try_make_uppercase().ok();
});
println!("\n{s:?}");
}
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?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() {
let mut s = Box::<str>::from("oφ⏣🌑");
// Iterate over the characters printing their length and bytes
s.ref_iter()
.for_each(|c| println!("{c:?}\tlength: {}, bytes: {:?}", c.len(), c.as_bytes()));
// Iterate over each character trying to make them uppercase
// (Result::ok is being used to ignore the result)
s.mut_iter().for_each(|c| {
c.try_make_uppercase().ok();
});
println!("\n{s:?}");
}
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
,LenNotEqual
will 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
,ReplacementTooLong
will 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
pad
is not valid utf8,ReplaceWithPadError::InvalidPad
will be returned. - If
r
, when utf8 encoded, is longer thanself
,ReplaceWithPadError::ReplacementLen
will 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::PadCharTooLong
will be returned. - If
r
, when utf8 encoded, is longer thanself
,ReplaceWithPadCharError::ReplacementLen
will be returned.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() {
let mut s = Box::<str>::from("👋🌍");
println!("Original string: {s:?}"); // "👋🌍"
// Get a mutable reference to the second character
let world = s.get_char_mut(1).unwrap();
println!("Original character: {world:?}"); // '🌍'
// Replace '🌍' with 'w' and '_' as padding
// '🌍' is 4 bytes long and 'w' is 1, so 3 '_'s will be added after.
world.replace_with_pad_char('w', '_').unwrap();
println!("Replacement character: {world:?}"); // 'w'
println!("Final string: {s:?}"); // "👋w___"
}
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
,ReplacementTooLong
will 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
pad
is not valid utf8,ReplaceWithPadError::InvalidPad
will be returned. - If
r
, when utf8 encoded, is longer thanself
,ReplaceWithPadError::ReplacementLen
will 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::PadCharTooLong
will be returned. - If
r
, when utf8 encoded, is longer thanself
,ReplaceWithPadCharError::ReplacementLen
will 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?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() {
let mut s = Box::<str>::from("oφ⏣🌑");
// Iterate over the characters printing their length and bytes
s.ref_iter()
.for_each(|c| println!("{c:?}\tlength: {}, bytes: {:?}", c.len(), c.as_bytes()));
// Iterate over each character trying to make them uppercase
// (Result::ok is being used to ignore the result)
s.mut_iter().for_each(|c| {
c.try_make_uppercase().ok();
});
println!("\n{s:?}");
}
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 PartialEq<Char> for char
impl PartialEq<Char> for char
source§impl PartialEq<Char> for str
impl PartialEq<Char> for str
source§impl PartialEq<OwnedChar> for Char
impl PartialEq<OwnedChar> for Char
source§impl PartialEq<char> for &Char
impl PartialEq<char> for &Char
source§impl PartialEq<char> for &mut Char
impl PartialEq<char> for &mut Char
source§impl PartialEq<char> for Char
impl PartialEq<char> for Char
source§impl PartialEq for Char
impl PartialEq for Char
source§impl PartialOrd<Char> for char
impl PartialOrd<Char> for char
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Char> for str
impl PartialOrd<Char> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<OwnedChar> for Char
impl PartialOrd<OwnedChar> for Char
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<char> for Char
impl PartialOrd<char> for Char
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<str> for Char
impl PartialOrd<str> for Char
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd for Char
impl PartialOrd for Char
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more