Skip to content

Commit b906a8b

Browse files
committed
std::str: remove .as_mut_buf & rewrite/simplify .push_char.
`.as_mut_buf` was used exactly once, in `.push_char` which could be written in a simpler way, using the `&mut ~[u8]` that it already retrieved. In the rare situation when someone really needs `.as_mut_buf`-like functionality (getting a `*mut u8`), they can go via `str::raw::as_owned_vec`.
1 parent 17ac2aa commit b906a8b

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

src/libstd/str.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,14 +2555,6 @@ pub trait OwnedStr {
25552555
/// The buffer does not have a null terminator.
25562556
fn into_bytes(self) -> ~[u8];
25572557

2558-
/// Work with the mutable byte buffer and length of a slice.
2559-
///
2560-
/// The buffer does not have a null terminator.
2561-
///
2562-
/// The caller must make sure any mutations to this buffer keep the string
2563-
/// valid UTF-8!
2564-
fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T;
2565-
25662558
/// Sets the length of a string
25672559
///
25682560
/// This will explicitly set the size of the string, without actually
@@ -2591,16 +2583,15 @@ impl OwnedStr for ~str {
25912583
let cur_len = self.len();
25922584
// may use up to 4 bytes.
25932585
unsafe {
2594-
raw::as_owned_vec(self).reserve_additional(4);
2586+
let v = raw::as_owned_vec(self);
2587+
v.reserve_additional(4);
25952588

25962589
// Attempt to not use an intermediate buffer by just pushing bytes
25972590
// directly onto this string.
2598-
let used = self.as_mut_buf(|buf, _| {
2599-
vec::raw::mut_buf_as_slice(buf.offset(cur_len as int), 4, |slc| {
2600-
c.encode_utf8(slc)
2601-
})
2602-
});
2603-
self.set_len(cur_len + used);
2591+
let write_ptr = v.as_mut_ptr().offset(cur_len as int);
2592+
let used = vec::raw::mut_buf_as_slice(write_ptr, 4, |slc| c.encode_utf8(slc));
2593+
2594+
v.set_len(cur_len + used);
26042595
}
26052596
}
26062597

@@ -2668,14 +2659,6 @@ impl OwnedStr for ~str {
26682659
unsafe { cast::transmute(self) }
26692660
}
26702661

2671-
#[inline]
2672-
fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T {
2673-
unsafe {
2674-
let v = raw::as_owned_vec(self);
2675-
f(v.as_mut_ptr(), v.len())
2676-
}
2677-
}
2678-
26792662
#[inline]
26802663
unsafe fn set_len(&mut self, new_len: uint) {
26812664
raw::as_owned_vec(self).set_len(new_len)

0 commit comments

Comments
 (0)