Skip to content

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/libcore/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this function

}

/// Encodes this character as UTF-16 into the provided `u16` buffer,
/// and then returns the subslice of the buffer that contains the encoded character.
///
Expand Down Expand Up @@ -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)) }
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down