From 12750c88931f707e63f2ec19396710bf70a39ae8 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 11 Jun 2013 21:43:29 +1000 Subject: [PATCH] std: methodise str::capacity --- src/libstd/str.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 0c99dd4f9fe..fdcca339f9b 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1094,18 +1094,6 @@ pub fn subslice_offset(outer: &str, inner: &str) -> uint { } } - -/** - * Returns the number of single-byte characters the string can hold without - * reallocating - */ -pub fn capacity(s: &const ~str) -> uint { - let buf: &const ~[u8] = unsafe { cast::transmute(s) }; - let vcap = vec::capacity(buf); - assert!(vcap > 0u); - vcap - 1u -} - /// Escape each char in `s` with char::escape_default. pub fn escape_default(s: &str) -> ~str { let mut out: ~str = ~""; @@ -2020,6 +2008,7 @@ pub trait OwnedStr { fn append(&self, rhs: &str) -> ~str; // FIXME #4850: this should consume self. fn reserve(&mut self, n: uint); fn reserve_at_least(&mut self, n: uint); + fn capacity(&self) -> uint; fn as_bytes_with_null_consume(self) -> ~[u8]; } @@ -2212,6 +2201,17 @@ impl OwnedStr for ~str { self.reserve(uint::next_power_of_two(n + 1u) - 1u) } + /** + * Returns the number of single-byte characters the string can hold without + * reallocating + */ + fn capacity(&self) -> uint { + let buf: &const ~[u8] = unsafe { cast::transmute(self) }; + let vcap = vec::capacity(buf); + assert!(vcap > 0u); + vcap - 1u + } + /// Convert to a vector of bytes. This does not allocate a new /// string, and includes the null terminator. #[inline]