26
26
unpack_slice,
27
27
28
28
// Adding things to and removing things from a string
29
+ push_str_no_overallocate,
29
30
push_str,
30
31
push_char,
31
32
pop_char,
@@ -235,13 +236,29 @@ pure fn from_chars(chs: &[const char]) -> str {
235
236
ret buf;
236
237
}
237
238
239
+ /// Appends a string slice to the back of a string, without overallocating
240
+ #[ inline( always) ]
241
+ fn push_str_no_overallocate ( & lhs: str , rhs : str /& ) {
242
+ unsafe {
243
+ let llen = lhs. len ( ) ;
244
+ let rlen = rhs. len ( ) ;
245
+ reserve ( lhs, llen + rlen) ;
246
+ do as_buf( lhs) |lbuf| {
247
+ do unpack_slice ( rhs) |rbuf, _rlen| {
248
+ let dst = ptr:: offset ( lbuf, llen) ;
249
+ ptr:: memcpy ( dst, rbuf, rlen) ;
250
+ }
251
+ }
252
+ unsafe:: set_len( lhs, llen + rlen) ;
253
+ }
254
+ }
238
255
/// Appends a string slice to the back of a string
239
256
#[ inline( always) ]
240
257
fn push_str ( & lhs: str , rhs : str /& ) {
241
258
unsafe {
242
259
let llen = lhs. len ( ) ;
243
260
let rlen = rhs. len ( ) ;
244
- reserve ( lhs, llen + rlen) ;
261
+ reserve_at_least ( lhs, llen + rlen) ;
245
262
do as_buf( lhs) |lbuf| {
246
263
do unpack_slice ( rhs) |rbuf, _rlen| {
247
264
let dst = ptr:: offset ( lbuf, llen) ;
@@ -257,7 +274,7 @@ fn push_str(&lhs: str, rhs: str/&) {
257
274
pure fn append ( +lhs : str , rhs : str /& ) -> str {
258
275
let mut v <- lhs;
259
276
unchecked {
260
- push_str ( v, rhs) ;
277
+ push_str_no_overallocate ( v, rhs) ;
261
278
}
262
279
ret v;
263
280
}
0 commit comments