Skip to content

Commit c0a9979

Browse files
committed
core: Add str::capacity
1 parent ad21976 commit c0a9979

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/libcore/str.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export
9494
char_at,
9595
reserve,
9696
reserve_at_least,
97+
capacity,
9798

9899
unsafe;
99100

@@ -1530,6 +1531,18 @@ fn reserve_at_least(&s: str, n: uint) unsafe {
15301531
reserve(s, uint::next_power_of_two(n + 1u) - 1u)
15311532
}
15321533

1534+
#[doc = "
1535+
Returns the number of single-byte characters the string can hold without
1536+
reallocating
1537+
"]
1538+
fn capacity(&&s: str) -> uint unsafe {
1539+
as_bytes(s) {|buf|
1540+
let vcap = vec::capacity(buf);
1541+
assert vcap > 0u;
1542+
vcap - 1u
1543+
}
1544+
}
1545+
15331546
#[doc = "Unsafe operations"]
15341547
mod unsafe {
15351548
export

0 commit comments

Comments
 (0)