-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Define non-panicking UTF encoding methods on char
#52580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -467,6 +467,37 @@ impl char { | |
} | ||
} | ||
|
||
/// Encodes this character as UTF-8 into the provided byte buffer, | ||
/// and then returns the subslice of the buffer that contains the encoded character. | ||
/// Returns `None` if buffer too short. | ||
/// | ||
/// # Examples | ||
/// | ||
/// In both of these examples, 'ß' takes two bytes to encode. | ||
/// | ||
/// ``` | ||
/// let mut b = [0; 2]; | ||
/// | ||
/// let result = 'ß'.encode_utf8(&mut b).unwrap(); | ||
/// | ||
/// assert_eq!(result, "ß"); | ||
/// | ||
/// assert_eq!(result.len(), 2); | ||
/// ``` | ||
/// | ||
/// A buffer that's too small: | ||
/// | ||
/// ``` | ||
/// let mut b = [0; 1]; | ||
/// | ||
/// assert_eq!(None, 'ß'.encode_utf8(&mut b)); | ||
/// ``` | ||
#[unstable(feature = "try_unicode_encode_char", issue = "52579")] | ||
#[inline] | ||
pub fn try_encode_utf8(self, dst: &mut [u8]) -> Option<&mut str> { | ||
if dst.len() < self.len_utf8() { None } else { Some(self.encode_utf8(dst)) } | ||
} | ||
|
||
/// Encodes this character as UTF-16 into the provided `u16` buffer, | ||
/// and then returns the subslice of the buffer that contains the encoded character. | ||
/// | ||
|
@@ -525,6 +556,37 @@ impl char { | |
} | ||
} | ||
|
||
/// Encodes this character as UTF-16 into the provided `u16` buffer, | ||
/// and then returns the subslice of the buffer that contains the encoded character. | ||
/// Returns `None` if buffer too short. | ||
/// | ||
/// # Examples | ||
/// | ||
/// In both of these examples, '𝕊' takes two `u16`s to encode. | ||
/// | ||
/// ``` | ||
/// let mut b = [0; 2]; | ||
/// | ||
/// let result = '𝕊'.encode_utf16(&mut b).unwrap(); | ||
/// | ||
/// assert_eq!(result, "𝕊"); | ||
/// | ||
/// assert_eq!(result.len(), 2); | ||
/// ``` | ||
/// | ||
/// A buffer that's too small: | ||
/// | ||
/// ``` | ||
/// let mut b = [0; 1]; | ||
/// | ||
/// assert_eq!(None, '𝕊'.encode_utf16(&mut b)); | ||
/// ``` | ||
#[unstable(feature = "try_unicode_encode_char", issue = "52579")] | ||
#[inline] | ||
pub fn try_encode_utf16(self, dst: &mut [u16]) -> Option<&mut [u16]> { | ||
if dst.len() < self.len_utf16() { None } else { Some(self.encode_utf16(dst)) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so this is essentially doing the checks twice in the happy path, maybe move the unsafe code into this function and have the panicky function match on the result and panic on None? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
|
||
/// Returns true if this `char` is an alphabetic code point, and false if not. | ||
/// | ||
/// # Examples | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for this function