From e1e594af6bcf8dc3cef05524a1b46c6cc64dddc8 Mon Sep 17 00:00:00 2001 From: Ronno Das Date: Wed, 19 Feb 2025 11:37:45 +0400 Subject: [PATCH 1/2] Add `EcoString::truncate` --- src/string.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/string.rs b/src/string.rs index 4be3e76..bae0a82 100644 --- a/src/string.rs +++ b/src/string.rs @@ -168,6 +168,42 @@ impl EcoString { self.0.clear(); } + /// Shortens the string to the specified length. + /// + /// If `new_len` is greater than or equal to the string's current length, + /// this has no effect. + /// + /// # Panics + /// + /// Panics if `new_len` does not lie on a [`char`] boundary. + /// + /// # Examples + /// + /// ``` + /// use ecow::EcoString; + /// + /// let mut s = EcoString::from("hello"); + /// + /// s.truncate(2); + /// + /// assert_eq!("he", s); + /// ``` + /// + /// ```should_panic + /// use ecow::EcoString; + /// + /// let mut s = EcoString::from("Poincaré duality"); + /// + /// s.truncate(8); + /// ``` + #[inline] + pub fn truncate(&mut self, new_len: usize) { + if new_len <= self.len() { + assert!(self.is_char_boundary(new_len)); + self.0.truncate(new_len) + } + } + /// Replaces all matches of a string with another string. /// /// This is a bit less general that [`str::replace`] because the `Pattern` From 200c27bc7ed8d474b3ed093cccc85075354680c4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 7 Apr 2025 19:44:00 +0200 Subject: [PATCH 2/2] Since the API just mirrors std, we don't quite as much docs --- src/string.rs | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/string.rs b/src/string.rs index bae0a82..2f136f0 100644 --- a/src/string.rs +++ b/src/string.rs @@ -173,34 +173,12 @@ impl EcoString { /// If `new_len` is greater than or equal to the string's current length, /// this has no effect. /// - /// # Panics - /// /// Panics if `new_len` does not lie on a [`char`] boundary. - /// - /// # Examples - /// - /// ``` - /// use ecow::EcoString; - /// - /// let mut s = EcoString::from("hello"); - /// - /// s.truncate(2); - /// - /// assert_eq!("he", s); - /// ``` - /// - /// ```should_panic - /// use ecow::EcoString; - /// - /// let mut s = EcoString::from("Poincaré duality"); - /// - /// s.truncate(8); - /// ``` #[inline] pub fn truncate(&mut self, new_len: usize) { if new_len <= self.len() { assert!(self.is_char_boundary(new_len)); - self.0.truncate(new_len) + self.0.truncate(new_len); } }