diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 610ca93494c9971f0e91b9ee2d44d6b598ac9bca..c57df2137860be6e0ed8ebd38166746189070a7d 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2107,6 +2107,7 @@ pub trait OwnedStr { fn reserve(&mut self, n: uint); fn reserve_at_least(&mut self, n: uint); fn capacity(&self) -> uint; + fn truncate(&mut self, len: uint); /// Work with the mutable byte buffer and length of a slice. /// @@ -2264,6 +2265,15 @@ fn capacity(&self) -> uint { } } + /// Shorten a string to the specified length (which must be <= the current length) + #[inline] + fn truncate(&mut self, len: uint) { + assert!(len <= self.len()); + assert!(self.is_char_boundary(len)); + unsafe { raw::set_len(self, len); } + } + + #[inline] fn as_mut_buf(&mut self, f: &fn(*mut u8, uint) -> T) -> T { let v: &mut ~[u8] = unsafe { cast::transmute(self) }; @@ -3482,6 +3492,38 @@ fn sum_len(v: &[S]) -> uint { assert_eq!(5, sum_len([~"01", ~"2", ~"34", ~""])); assert_eq!(5, sum_len([s.as_slice()])); } + + #[test] + fn test_str_truncate() { + let mut s = ~"12345"; + s.truncate(5); + assert_eq!(s.as_slice(), "12345"); + s.truncate(3); + assert_eq!(s.as_slice(), "123"); + s.truncate(0); + assert_eq!(s.as_slice(), ""); + + let mut s = ~"12345"; + let p = s.as_imm_buf(|p,_| p); + s.truncate(3); + s.push_str("6"); + let p_ = s.as_imm_buf(|p,_| p); + assert_eq!(p_, p); + } + + #[test] + #[should_fail] + fn test_str_truncate_invalid_len() { + let mut s = ~"12345"; + s.truncate(6); + } + + #[test] + #[should_fail] + fn test_str_truncate_split_codepoint() { + let mut s = ~"\u00FC"; // ΓΌ + s.truncate(1); + } } #[cfg(test)]