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`.
This commit is contained in:
parent
17ac2aa523
commit
b906a8b256
1 changed files with 6 additions and 23 deletions
|
@ -2555,14 +2555,6 @@ pub trait OwnedStr {
|
||||||
/// The buffer does not have a null terminator.
|
/// The buffer does not have a null terminator.
|
||||||
fn into_bytes(self) -> ~[u8];
|
fn into_bytes(self) -> ~[u8];
|
||||||
|
|
||||||
/// Work with the mutable byte buffer and length of a slice.
|
|
||||||
///
|
|
||||||
/// The buffer does not have a null terminator.
|
|
||||||
///
|
|
||||||
/// The caller must make sure any mutations to this buffer keep the string
|
|
||||||
/// valid UTF-8!
|
|
||||||
fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T;
|
|
||||||
|
|
||||||
/// Sets the length of a string
|
/// Sets the length of a string
|
||||||
///
|
///
|
||||||
/// This will explicitly set the size of the string, without actually
|
/// This will explicitly set the size of the string, without actually
|
||||||
|
@ -2591,16 +2583,15 @@ impl OwnedStr for ~str {
|
||||||
let cur_len = self.len();
|
let cur_len = self.len();
|
||||||
// may use up to 4 bytes.
|
// may use up to 4 bytes.
|
||||||
unsafe {
|
unsafe {
|
||||||
raw::as_owned_vec(self).reserve_additional(4);
|
let v = raw::as_owned_vec(self);
|
||||||
|
v.reserve_additional(4);
|
||||||
|
|
||||||
// Attempt to not use an intermediate buffer by just pushing bytes
|
// Attempt to not use an intermediate buffer by just pushing bytes
|
||||||
// directly onto this string.
|
// directly onto this string.
|
||||||
let used = self.as_mut_buf(|buf, _| {
|
let write_ptr = v.as_mut_ptr().offset(cur_len as int);
|
||||||
vec::raw::mut_buf_as_slice(buf.offset(cur_len as int), 4, |slc| {
|
let used = vec::raw::mut_buf_as_slice(write_ptr, 4, |slc| c.encode_utf8(slc));
|
||||||
c.encode_utf8(slc)
|
|
||||||
})
|
v.set_len(cur_len + used);
|
||||||
});
|
|
||||||
self.set_len(cur_len + used);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2668,14 +2659,6 @@ impl OwnedStr for ~str {
|
||||||
unsafe { cast::transmute(self) }
|
unsafe { cast::transmute(self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T {
|
|
||||||
unsafe {
|
|
||||||
let v = raw::as_owned_vec(self);
|
|
||||||
f(v.as_mut_ptr(), v.len())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn set_len(&mut self, new_len: uint) {
|
unsafe fn set_len(&mut self, new_len: uint) {
|
||||||
raw::as_owned_vec(self).set_len(new_len)
|
raw::as_owned_vec(self).set_len(new_len)
|
||||||
|
|
Loading…
Reference in a new issue