1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A type that can represent all platform-native strings, but is cheaply
12 //! interconvertable with Rust strings.
14 //! The need for this type arises from the fact that:
16 //! * On Unix systems, strings are often arbitrary sequences of non-zero
17 //! bytes, in many cases interpreted as UTF-8.
19 //! * On Windows, strings are often arbitrary sequences of non-zero 16-bit
20 //! values, interpreted as UTF-16 when it is valid to do so.
22 //! * In Rust, strings are always valid UTF-8, but may contain zeros.
24 //! The types in this module bridge this gap by simultaneously representing Rust
25 //! and platform-native string values, and in particular allowing a Rust string
26 //! to be converted into an "OS" string with no cost.
28 //! **Note**: At the moment, these types are extremely bare-bones, usable only
29 //! for conversion to/from various other string types. Eventually these types
30 //! will offer a full-fledged string API.
32 #![unstable(feature = "os",
33 reason
= "recently added as part of path/io reform")]
37 use borrow
::{Borrow, ToOwned}
;
38 use fmt
::{self, Debug}
;
40 use string
::{String, CowString}
;
43 use hash
::{Hash, Hasher}
;
44 #[cfg(stage0)] use hash::Writer;
45 use old_path
::{Path, GenericPath}
;
47 use sys
::os_str
::{Buf, Slice}
;
48 use sys_common
::{AsInner, IntoInner, FromInner}
;
51 /// Owned, mutable OS strings.
57 /// Slices into OS strings.
63 /// Constructs an `OsString` at no cost by consuming a `String`.
64 pub fn from_string(s
: String
) -> OsString
{
65 OsString { inner: Buf::from_string(s) }
68 /// Constructs an `OsString` by copying from a `&str` slice.
70 /// Equivalent to: `OsString::from_string(String::from_str(s))`.
71 pub fn from_str(s
: &str) -> OsString
{
72 OsString { inner: Buf::from_str(s) }
75 /// Constructs a new empty `OsString`.
76 pub fn new() -> OsString
{
77 OsString { inner: Buf::from_string(String::new()) }
80 /// Convert the `OsString` into a `String` if it contains valid Unicode data.
82 /// On failure, ownership of the original `OsString` is returned.
83 pub fn into_string(self) -> Result
<String
, OsString
> {
84 self.inner
.into_string().map_err(|buf
| OsString { inner: buf}
)
87 /// Extend the string with the given `&OsStr` slice.
88 pub fn push_os_str(&mut self, s
: &OsStr
) {
89 self.inner
.push_slice(&s
.inner
)
93 impl ops
::Index
<ops
::RangeFull
> for OsString
{
97 fn index(&self, _index
: &ops
::RangeFull
) -> &OsStr
{
98 unsafe { mem::transmute(self.inner.as_slice()) }
102 impl ops
::Deref
for OsString
{
106 fn deref(&self) -> &OsStr
{
111 impl Debug
for OsString
{
112 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
113 fmt
::Debug
::fmt(&**self, formatter
)
117 impl PartialEq
for OsString
{
118 fn eq(&self, other
: &OsString
) -> bool
{
123 impl PartialEq
<str> for OsString
{
124 fn eq(&self, other
: &str) -> bool
{
129 impl PartialEq
<OsString
> for str {
130 fn eq(&self, other
: &OsString
) -> bool
{
135 impl Eq
for OsString {}
137 impl PartialOrd
for OsString
{
139 fn partial_cmp(&self, other
: &OsString
) -> Option
<cmp
::Ordering
> {
140 (&**self).partial_cmp(&**other
)
143 fn lt(&self, other
: &OsString
) -> bool { &**self < &**other }
145 fn le(&self, other
: &OsString
) -> bool { &**self <= &**other }
147 fn gt(&self, other
: &OsString
) -> bool { &**self > &**other }
149 fn ge(&self, other
: &OsString
) -> bool { &**self >= &**other }
152 impl PartialOrd
<str> for OsString
{
154 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
155 (&**self).partial_cmp(other
)
159 impl Ord
for OsString
{
161 fn cmp(&self, other
: &OsString
) -> cmp
::Ordering
{
162 (&**self).cmp(&**other
)
167 impl<'a
, S
: Hasher
+ Writer
> Hash
<S
> for OsString
{
169 fn hash(&self, state
: &mut S
) {
170 (&**self).hash(state
)
174 #[stable(feature = "rust1", since = "1.0.0")]
175 impl Hash
for OsString
{
177 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
178 (&**self).hash(state
)
183 /// Coerce directly from a `&str` slice to a `&OsStr` slice.
184 pub fn from_str(s
: &str) -> &OsStr
{
185 unsafe { mem::transmute(Slice::from_str(s)) }
188 /// Yield a `&str` slice if the `OsStr` is valid unicode.
190 /// This conversion may entail doing a check for UTF-8 validity.
191 pub fn to_str(&self) -> Option
<&str> {
195 /// Convert an `OsStr` to a `CowString`.
197 /// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
198 pub fn to_string_lossy(&self) -> CowString
{
199 self.inner
.to_string_lossy()
202 /// Copy the slice into an owned `OsString`.
203 pub fn to_os_string(&self) -> OsString
{
204 OsString { inner: self.inner.to_owned() }
207 /// Get the underlying byte representation.
209 /// Note: it is *crucial* that this API is private, to avoid
210 /// revealing the internal, platform-specific encodings.
211 fn bytes(&self) -> &[u8] {
212 unsafe { mem::transmute(&self.inner) }
216 impl PartialEq
for OsStr
{
217 fn eq(&self, other
: &OsStr
) -> bool
{
218 self.bytes().eq(other
.bytes())
222 impl PartialEq
<str> for OsStr
{
223 fn eq(&self, other
: &str) -> bool
{
224 *self == *OsStr
::from_str(other
)
228 impl PartialEq
<OsStr
> for str {
229 fn eq(&self, other
: &OsStr
) -> bool
{
230 *other
== *OsStr
::from_str(self)
236 impl PartialOrd
for OsStr
{
238 fn partial_cmp(&self, other
: &OsStr
) -> Option
<cmp
::Ordering
> {
239 self.bytes().partial_cmp(other
.bytes())
242 fn lt(&self, other
: &OsStr
) -> bool { self.bytes().lt(other.bytes()) }
244 fn le(&self, other
: &OsStr
) -> bool { self.bytes().le(other.bytes()) }
246 fn gt(&self, other
: &OsStr
) -> bool { self.bytes().gt(other.bytes()) }
248 fn ge(&self, other
: &OsStr
) -> bool { self.bytes().ge(other.bytes()) }
251 impl PartialOrd
<str> for OsStr
{
253 fn partial_cmp(&self, other
: &str) -> Option
<cmp
::Ordering
> {
254 self.partial_cmp(OsStr
::from_str(other
))
258 // FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
259 // have more flexible coherence rules.
263 fn cmp(&self, other
: &OsStr
) -> cmp
::Ordering { self.bytes().cmp(other.bytes()) }
267 impl<'a
, S
: Hasher
+ Writer
> Hash
<S
> for OsStr
{
269 fn hash(&self, state
: &mut S
) {
270 self.bytes().hash(state
)
274 #[stable(feature = "rust1", since = "1.0.0")]
275 impl Hash
for OsStr
{
277 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
278 self.bytes().hash(state
)
282 impl Debug
for OsStr
{
283 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
284 self.inner
.fmt(formatter
)
288 impl Borrow
<OsStr
> for OsString
{
289 fn borrow(&self) -> &OsStr { &self[..] }
292 impl ToOwned
for OsStr
{
293 type Owned
= OsString
;
294 fn to_owned(&self) -> OsString { self.to_os_string() }
297 impl<'a
, T
: AsOsStr
+ ?Sized
> AsOsStr
for &'a T
{
298 fn as_os_str(&self) -> &OsStr
{
303 impl AsOsStr
for OsStr
{
304 fn as_os_str(&self) -> &OsStr
{
309 impl AsOsStr
for OsString
{
310 fn as_os_str(&self) -> &OsStr
{
315 impl AsOsStr
for str {
316 fn as_os_str(&self) -> &OsStr
{
317 OsStr
::from_str(self)
321 impl AsOsStr
for String
{
322 fn as_os_str(&self) -> &OsStr
{
323 OsStr
::from_str(&self[..])
328 impl AsOsStr
for Path
{
329 fn as_os_str(&self) -> &OsStr
{
330 unsafe { mem::transmute(self.as_vec()) }
335 impl AsOsStr
for Path
{
336 fn as_os_str(&self) -> &OsStr
{
337 // currently .as_str() is actually infallible on windows
338 OsStr
::from_str(self.as_str().unwrap())
342 impl FromInner
<Buf
> for OsString
{
343 fn from_inner(buf
: Buf
) -> OsString
{
344 OsString { inner: buf }
348 impl IntoInner
<Buf
> for OsString
{
349 fn into_inner(self) -> Buf
{
354 impl AsInner
<Slice
> for OsStr
{
355 fn as_inner(&self) -> &Slice
{