Skip to content

Commit 12750c8

Browse files
committed
std: methodise str::capacity
1 parent 3ac00a9 commit 12750c8

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/libstd/str.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,18 +1094,6 @@ pub fn subslice_offset(outer: &str, inner: &str) -> uint {
10941094
}
10951095
}
10961096

1097-
1098-
/**
1099-
* Returns the number of single-byte characters the string can hold without
1100-
* reallocating
1101-
*/
1102-
pub fn capacity(s: &const ~str) -> uint {
1103-
let buf: &const ~[u8] = unsafe { cast::transmute(s) };
1104-
let vcap = vec::capacity(buf);
1105-
assert!(vcap > 0u);
1106-
vcap - 1u
1107-
}
1108-
11091097
/// Escape each char in `s` with char::escape_default.
11101098
pub fn escape_default(s: &str) -> ~str {
11111099
let mut out: ~str = ~"";
@@ -2020,6 +2008,7 @@ pub trait OwnedStr {
20202008
fn append(&self, rhs: &str) -> ~str; // FIXME #4850: this should consume self.
20212009
fn reserve(&mut self, n: uint);
20222010
fn reserve_at_least(&mut self, n: uint);
2011+
fn capacity(&self) -> uint;
20232012
20242013
fn as_bytes_with_null_consume(self) -> ~[u8];
20252014
}
@@ -2212,6 +2201,17 @@ impl OwnedStr for ~str {
22122201
self.reserve(uint::next_power_of_two(n + 1u) - 1u)
22132202
}
22142203
2204+
/**
2205+
* Returns the number of single-byte characters the string can hold without
2206+
* reallocating
2207+
*/
2208+
fn capacity(&self) -> uint {
2209+
let buf: &const ~[u8] = unsafe { cast::transmute(self) };
2210+
let vcap = vec::capacity(buf);
2211+
assert!(vcap > 0u);
2212+
vcap - 1u
2213+
}
2214+
22152215
/// Convert to a vector of bytes. This does not allocate a new
22162216
/// string, and includes the null terminator.
22172217
#[inline]

0 commit comments

Comments
 (0)