Skip to content

Commit 43e4d69

Browse files
Add AsciiString::insert_str (#82)
1 parent 5aa5c3d commit 43e4d69

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/ascii_string.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ impl AsciiString {
172172
self.vec.extend(string.chars())
173173
}
174174

175+
/// Inserts the given ASCII string at the given place in this ASCII string buffer.
176+
///
177+
/// # Panics
178+
///
179+
/// Panics if `idx` is larger than the `AsciiString`'s length.
180+
///
181+
/// # Examples
182+
/// ```
183+
/// # use ascii::{AsciiString, AsAsciiStr};
184+
/// use std::str::FromStr;
185+
/// let mut s = AsciiString::from_str("abc").unwrap();
186+
/// s.insert_str(1, "def".as_ascii_str().unwrap());
187+
/// assert_eq!(&*s, "adefbc");
188+
#[inline]
189+
pub fn insert_str(&mut self, idx: usize, string: &AsciiStr) {
190+
self.vec.reserve(string.len());
191+
self.vec.splice(idx..idx, string.into_iter().copied());
192+
}
193+
175194
/// Returns the number of bytes that this ASCII string buffer can hold without reallocating.
176195
///
177196
/// # Examples

0 commit comments

Comments
 (0)