Skip to content

Commit 7bb6584

Browse files
committed
libcore: Rewrite str::unsafe::push_byte in pure rust.
1 parent 94c3975 commit 7bb6584

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

src/libcore/str.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export
117117

118118
#[abi = "cdecl"]
119119
extern mod rustrt {
120-
fn rust_str_push(&s: ~str, ch: u8);
121120
fn str_reserve_shared(&ss: ~str, nn: libc::size_t);
122121
}
123122

@@ -1998,12 +1997,18 @@ mod unsafe {
19981997

19991998
/// Appends a byte to a string. (Not UTF-8 safe).
20001999
unsafe fn push_byte(&s: ~str, b: u8) {
2001-
rustrt::rust_str_push(s, b);
2000+
reserve_at_least(s, s.len() + 1);
2001+
do as_buf(s) |buf, len| {
2002+
let buf: *mut u8 = ::unsafe::reinterpret_cast(buf);
2003+
*ptr::mut_offset(buf, len) = b;
2004+
}
2005+
set_len(s, s.len() + 1);
20022006
}
20032007

20042008
/// Appends a vector of bytes to a string. (Not UTF-8 safe).
20052009
unsafe fn push_bytes(&s: ~str, bytes: ~[u8]) {
2006-
for vec::each(bytes) |byte| { rustrt::rust_str_push(s, byte); }
2010+
reserve_at_least(s, s.len() + bytes.len());
2011+
for vec::each(bytes) |byte| { push_byte(s, byte); }
20072012
}
20082013

20092014
/// Removes the last byte from a string and returns it. (Not UTF-8 safe).

src/rt/rust_builtin.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,6 @@ str_reserve_shared(rust_vec_box** sp,
148148
reserve_vec_exact(task, sp, n_elts + 1);
149149
}
150150

151-
extern "C" CDECL void
152-
rust_str_push(rust_vec_box** sp, uint8_t byte) {
153-
rust_task *task = rust_get_current_task();
154-
size_t fill = (*sp)->body.fill;
155-
reserve_vec(task, sp, fill + 1);
156-
(*sp)->body.data[fill-1] = byte;
157-
(*sp)->body.data[fill] = 0;
158-
(*sp)->body.fill = fill + 1;
159-
}
160-
161151
extern "C" CDECL rust_vec*
162152
rand_seed() {
163153
size_t size = sizeof(ub4) * RANDSIZ;

src/rt/rustrt.def.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ rust_getcwd
3838
rust_get_stdin
3939
rust_get_stdout
4040
rust_get_stderr
41-
rust_str_push
4241
rust_list_files
4342
rust_log_console_on
4443
rust_log_console_off

0 commit comments

Comments
 (0)