summaryrefslogtreecommitdiffstats
path: root/library/std/src/sys_common/wtf8.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/sys_common/wtf8.rs')
-rw-r--r--library/std/src/sys_common/wtf8.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/library/std/src/sys_common/wtf8.rs b/library/std/src/sys_common/wtf8.rs
index ff96c35fb..c9d3e13cf 100644
--- a/library/std/src/sys_common/wtf8.rs
+++ b/library/std/src/sys_common/wtf8.rs
@@ -570,7 +570,7 @@ impl Wtf8 {
/// Since the byte slice is not checked for valid WTF-8, this functions is
/// marked unsafe.
#[inline]
- unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
+ pub unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
mem::transmute(value)
}
@@ -614,19 +614,20 @@ impl Wtf8 {
Wtf8CodePoints { bytes: self.bytes.iter() }
}
+ /// Access raw bytes of WTF-8 data
+ #[inline]
+ pub fn as_bytes(&self) -> &[u8] {
+ &self.bytes
+ }
+
/// Tries to convert the string to UTF-8 and return a `&str` slice.
///
/// Returns `None` if the string contains surrogates.
///
/// This does not copy the data.
#[inline]
- pub fn as_str(&self) -> Option<&str> {
- // Well-formed WTF-8 is also well-formed UTF-8
- // if and only if it contains no surrogate.
- match self.next_surrogate(0) {
- None => Some(unsafe { str::from_utf8_unchecked(&self.bytes) }),
- Some(_) => None,
- }
+ pub fn as_str(&self) -> Result<&str, str::Utf8Error> {
+ str::from_utf8(&self.bytes)
}
/// Creates an owned `Wtf8Buf` from a borrowed `Wtf8`.