diff options
| author | Lyndon Brown <jnqnfe@gmail.com> | 2018-11-10 06:17:36 +0000 |
|---|---|---|
| committer | Lyndon Brown <jnqnfe@gmail.com> | 2018-12-12 18:13:40 +0000 |
| commit | 0008e0d502fab4aee5909d23ac277701727903e8 (patch) | |
| tree | a25e55eee98c6190def755168d60a150aaff8ef3 /src/libstd/ffi | |
| parent | bd47d6825bf4090517549d33cfef10d3300b4a75 (diff) | |
| download | rust-0008e0d502fab4aee5909d23ac277701727903e8.tar.gz rust-0008e0d502fab4aee5909d23ac277701727903e8.zip | |
use actual invalid string in OsStr::to_string_lossy example
Diffstat (limited to 'src/libstd/ffi')
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 828972187ee..766142fb57f 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -536,17 +536,42 @@ impl OsStr { /// /// # Examples /// - /// Calling `to_string_lossy` on an `OsStr` with valid unicode: + /// Calling `to_string_lossy` on an `OsStr` with invalid unicode: /// /// ``` - /// use std::ffi::OsStr; - /// - /// let os_str = OsStr::new("foo"); - /// assert_eq!(os_str.to_string_lossy(), "foo"); + /// // Note, due to differences in how Unix and Windows represent strings, + /// // we are forced to complicate this example, setting up example `OsStr`s + /// // with different source data and via different platform extensions. + /// // Understand that in reality you could end up with such example invalid + /// // sequences simply through collecting user command line arguments, for + /// // example. + /// + /// #[cfg(any(unix, target_os = "redox"))] { + /// use std::ffi::OsStr; + /// use std::os::unix::ffi::OsStrExt; + /// + /// // Here, the values 0x66 and 0x6f correspond to 'f' and 'o' + /// // respectively. The value 0x80 is a lone continuation byte, invalid + /// // in a UTF-8 sequence. + /// let source = [0x66, 0x6f, 0x80, 0x6f]; + /// let os_str = OsStr::from_bytes(&source[..]); + /// + /// assert_eq!(os_str.to_string_lossy(), "fo�o"); + /// } + /// #[cfg(windows)] { + /// use std::ffi::OsString; + /// use std::os::windows::prelude::*; + /// + /// // Here the values 0x0066 and 0x006f correspond to 'f' and 'o' + /// // respectively. The value 0xD800 is a lone surrogate half, invalid + /// // in a UTF-16 sequence. + /// let source = [0x0066, 0x006f, 0xD800, 0x006f]; + /// let os_string = OsString::from_wide(&source[..]); + /// let os_str = os_string.as_os_str(); + /// + /// assert_eq!(os_str.to_string_lossy(), "fo�o"); + /// } /// ``` - /// - /// Had `os_str` contained invalid unicode, the `to_string_lossy` call might - /// have returned `"fo�"`. #[stable(feature = "rust1", since = "1.0.0")] pub fn to_string_lossy(&self) -> Cow<str> { self.inner.to_string_lossy() |
