Skip to content

Commit

Permalink
Auto merge of #31608 - frewsxcv:osstring-simple-functions, r=alexcric…
Browse files Browse the repository at this point in the history
…hton

#29453
  • Loading branch information
bors committed Feb 20, 2016
2 parents d3ffad7 + 8c3655b commit 4f3d588
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 0 deletions.
178 changes: 178 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,58 @@ impl OsString {
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
}

/// Creates a new `OsString` with the given capacity. The string will be
/// able to hold exactly `capacity` bytes without reallocating. If
/// `capacity` is 0, the string will not allocate.
///
/// See main `OsString` documentation information about encoding.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn with_capacity(capacity: usize) -> OsString {
OsString {
inner: Buf::with_capacity(capacity)
}
}

/// Truncates the `OsString` to zero length.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn clear(&mut self) {
self.inner.clear()
}

/// Returns the number of bytes this `OsString` can hold without
/// reallocating.
///
/// See `OsString` introduction for information about encoding.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}

/// Reserves capacity for at least `additional` more bytes to be inserted
/// in the given `OsString`. The collection may reserve more space to avoid
/// frequent reallocations.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}

/// Reserves the minimum capacity for exactly `additional` more bytes to be
/// inserted in the given `OsString`. Does nothing if the capacity is
/// already sufficient.
///
/// Note that the allocator may give the collection more space than it
/// requests. Therefore capacity can not be relied upon to be precisely
/// minimal. Prefer reserve if future insertions are expected.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -277,6 +329,22 @@ impl OsStr {
self.to_bytes().and_then(|b| CString::new(b).ok())
}

/// Checks whether the `OsStr` is empty.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn is_empty(&self) -> bool {
self.inner.inner.is_empty()
}

/// Returns the number of bytes in this `OsStr`.
///
/// See `OsStr` introduction for information about encoding.
#[unstable(feature = "osstring_simple_functions",
reason = "recently added", issue = "29453")]
pub fn len(&self) -> usize {
self.inner.inner.len()
}

/// Gets the underlying byte representation.
///
/// Note: it is *crucial* that this API is private, to avoid
Expand Down Expand Up @@ -414,3 +482,113 @@ impl AsInner<Slice> for OsStr {
&self.inner
}
}

#[cfg(test)]
mod tests {
use super::*;
use sys_common::{AsInner, IntoInner};

#[test]
fn test_os_string_with_capacity() {
let os_string = OsString::with_capacity(0);
assert_eq!(0, os_string.inner.into_inner().capacity());

let os_string = OsString::with_capacity(10);
assert_eq!(10, os_string.inner.into_inner().capacity());

let mut os_string = OsString::with_capacity(0);
os_string.push("abc");
assert!(os_string.inner.into_inner().capacity() >= 3);
}

#[test]
fn test_os_string_clear() {
let mut os_string = OsString::from("abc");
assert_eq!(3, os_string.inner.as_inner().len());

os_string.clear();
assert_eq!(&os_string, "");
assert_eq!(0, os_string.inner.as_inner().len());
}

#[test]
fn test_os_string_capacity() {
let os_string = OsString::with_capacity(0);
assert_eq!(0, os_string.capacity());

let os_string = OsString::with_capacity(10);
assert_eq!(10, os_string.capacity());

let mut os_string = OsString::with_capacity(0);
os_string.push("abc");
assert!(os_string.capacity() >= 3);
}

#[test]
fn test_os_string_reserve() {
let mut os_string = OsString::new();
assert_eq!(os_string.capacity(), 0);

os_string.reserve(2);
assert!(os_string.capacity() >= 2);

for _ in 0..16 {
os_string.push("a");
}

assert!(os_string.capacity() >= 16);
os_string.reserve(16);
assert!(os_string.capacity() >= 32);

os_string.push("a");

os_string.reserve(16);
assert!(os_string.capacity() >= 33)
}

#[test]
fn test_os_string_reserve_exact() {
let mut os_string = OsString::new();
assert_eq!(os_string.capacity(), 0);

os_string.reserve_exact(2);
assert!(os_string.capacity() >= 2);

for _ in 0..16 {
os_string.push("a");
}

assert!(os_string.capacity() >= 16);
os_string.reserve_exact(16);
assert!(os_string.capacity() >= 32);

os_string.push("a");

os_string.reserve_exact(16);
assert!(os_string.capacity() >= 33)
}

#[test]
fn test_os_str_is_empty() {
let mut os_string = OsString::new();
assert!(os_string.is_empty());

os_string.push("abc");
assert!(!os_string.is_empty());

os_string.clear();
assert!(os_string.is_empty());
}

#[test]
fn test_os_str_len() {
let mut os_string = OsString::new();
assert_eq!(0, os_string.len());

os_string.push("abc");
assert_eq!(3, os_string.len());

os_string.clear();
assert_eq!(0, os_string.len());
}
}
14 changes: 14 additions & 0 deletions src/libstd/sys/common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ impl Wtf8Buf {
Wtf8Buf { bytes: <[_]>::to_vec(str.as_bytes()) }
}

pub fn clear(&mut self) {
self.bytes.clear()
}

/// Creates a WTF-8 string from a potentially ill-formed UTF-16 slice of 16-bit code units.
///
/// This is lossless: calling `.encode_wide()` on the resulting string
Expand Down Expand Up @@ -234,6 +238,11 @@ impl Wtf8Buf {
self.bytes.reserve(additional)
}

#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.bytes.reserve_exact(additional)
}

/// Returns the number of bytes that this string buffer can hold without reallocating.
#[inline]
pub fn capacity(&self) -> usize {
Expand Down Expand Up @@ -443,6 +452,11 @@ impl Wtf8 {
self.bytes.len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}

/// Returns the code point at `position` if it is in the ASCII range,
/// or `b'\xFF' otherwise.
///
Expand Down
41 changes: 41 additions & 0 deletions src/libstd/sys/unix/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use vec::Vec;
use str;
use string::String;
use mem;
use sys_common::{AsInner, IntoInner};

#[derive(Clone, Hash)]
pub struct Buf {
Expand All @@ -39,11 +40,51 @@ impl Debug for Buf {
}
}

impl IntoInner<Vec<u8>> for Buf {
fn into_inner(self) -> Vec<u8> {
self.inner
}
}

impl AsInner<[u8]> for Buf {
fn as_inner(&self) -> &[u8] {
&self.inner
}
}


impl Buf {
pub fn from_string(s: String) -> Buf {
Buf { inner: s.into_bytes() }
}

#[inline]
pub fn with_capacity(capacity: usize) -> Buf {
Buf {
inner: Vec::with_capacity(capacity)
}
}

#[inline]
pub fn clear(&mut self) {
self.inner.clear()
}

#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}

#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}

#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}

pub fn as_slice(&self) -> &Slice {
unsafe { mem::transmute(&*self.inner) }
}
Expand Down
22 changes: 22 additions & 0 deletions src/libstd/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl Debug for Slice {
}

impl Buf {
pub fn with_capacity(capacity: usize) -> Buf {
Buf {
inner: Wtf8Buf::with_capacity(capacity)
}
}

pub fn clear(&mut self) {
self.inner.clear()
}

pub fn capacity(&self) -> usize {
self.inner.capacity()
}

pub fn from_string(s: String) -> Buf {
Buf { inner: Wtf8Buf::from_string(s) }
}
Expand All @@ -56,6 +70,14 @@ impl Buf {
pub fn push_slice(&mut self, s: &Slice) {
self.inner.push_wtf8(&s.inner)
}

pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}

pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
}

impl Slice {
Expand Down

0 comments on commit 4f3d588

Please sign in to comment.