From e87c62fb12e6b02cfc39fc2a16c315615714757a Mon Sep 17 00:00:00 2001 From: marcell Date: Wed, 10 Jun 2015 02:03:56 +0200 Subject: [PATCH] Modify String::push to reallocate more conservatively in case of the character's UTF-8 representation is bigger than 1 byte --- src/libcollections/string.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 7ede6545b9f..6717f2f45fa 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -468,24 +468,24 @@ impl String { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn push(&mut self, ch: char) { - if (ch as u32) < 0x80 { - self.vec.push(ch as u8); - return; - } + match ch.len_utf8() { + 1 => self.vec.push(ch as u8), + ch_len => { + let cur_len = self.len(); + // This may use up to 4 bytes. + self.vec.reserve(ch_len); - let cur_len = self.len(); - // This may use up to 4 bytes. - self.vec.reserve(4); - - unsafe { - // Attempt to not use an intermediate buffer by just pushing bytes - // directly onto this string. - let slice = slice::from_raw_parts_mut ( - self.vec.as_mut_ptr().offset(cur_len as isize), - 4 - ); - let used = ch.encode_utf8(slice).unwrap_or(0); - self.vec.set_len(cur_len + used); + unsafe { + // Attempt to not use an intermediate buffer by just pushing bytes + // directly onto this string. + let slice = slice::from_raw_parts_mut ( + self.vec.as_mut_ptr().offset(cur_len as isize), + ch_len + ); + let used = ch.encode_utf8(slice).unwrap_or(0); + self.vec.set_len(cur_len + used); + } + } } }