]> git.lizzy.rs Git - rust.git/commitdiff
auto merge of #8289 : sfackler/rust/push_byte, r=erickt
authorbors <bors@rust-lang.org>
Mon, 5 Aug 2013 15:10:55 +0000 (08:10 -0700)
committerbors <bors@rust-lang.org>
Mon, 5 Aug 2013 15:10:55 +0000 (08:10 -0700)
It was previously pushing the byte on top of the string's null
terminator. I added a test to make sure it doesn't break in the future.

src/libstd/str.rs

index 5c6895fea43c956e435948ed333596e6f57af8b2..1a913fb4e996ab4155da610eedf5aa05b2615572 100644 (file)
@@ -902,7 +902,7 @@ pub unsafe fn push_byte(s: &mut ~str, b: u8) {
         let new_len = s.len() + 1;
         s.reserve_at_least(new_len);
         do s.as_mut_buf |buf, len| {
-            *ptr::mut_offset(buf, len as int) = b;
+            *ptr::mut_offset(buf, (len-1) as int) = b;
         }
         set_len(&mut *s, new_len);
     }
@@ -2825,6 +2825,13 @@ fn test_is_whitespace() {
         assert!(!"   _   ".is_whitespace());
     }
 
+    #[test]
+    fn test_push_byte() {
+        let mut s = ~"ABC";
+        unsafe{raw::push_byte(&mut s, 'D' as u8)};
+        assert_eq!(s, ~"ABCD");
+    }
+
     #[test]
     fn test_shift_byte() {
         let mut s = ~"ABC";