Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EcoString::truncate #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down