From 7327029f3860ba7bbda1bde27e8faa4c8f425b31 Mon Sep 17 00:00:00 2001 From: Patrick Norton Date: Sun, 25 Jul 2021 08:06:54 -0400 Subject: [PATCH 1/3] Added AsciiString::insert_str --- src/ascii_string.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ascii_string.rs b/src/ascii_string.rs index 740abf0..a351c59 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -172,6 +172,21 @@ impl AsciiString { self.vec.extend(string.chars()) } + /// Inserts the given ASCII string at the given place in this ASCII string buffer. + /// + /// # Examples + /// ``` + /// # use ascii::{AsciiString, AsAsciiStr}; + /// use std::str::FromStr; + /// let mut s = AsciiString::from_str("abc").unwrap(); + /// s.insert_str(1, "def".as_ascii_str().unwrap()); + /// assert_eq!(&*s, "adefbc"); + #[inline] + pub fn insert_str(&mut self, idx: usize, string: &AsciiStr) { + self.vec.reserve(string.len()); + self.vec.splice(idx..idx, string); + } + /// Returns the number of bytes that this ASCII string buffer can hold without reallocating. /// /// # Examples From b904aca8f93ef2673566210fa332e21d1e3de4de Mon Sep 17 00:00:00 2001 From: Patrick Norton Date: Tue, 31 May 2022 05:07:16 -0600 Subject: [PATCH 2/3] Added panic docs --- src/ascii_string.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ascii_string.rs b/src/ascii_string.rs index a351c59..014d72a 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -174,6 +174,10 @@ impl AsciiString { /// Inserts the given ASCII string at the given place in this ASCII string buffer. /// + /// # Panics + /// + /// Panics if `idx` is larger than the `AsciiString`'s length. + /// /// # Examples /// ``` /// # use ascii::{AsciiString, AsAsciiStr}; From 56e806967bd98f1d7130eff32aa9d4c46eb90a63 Mon Sep 17 00:00:00 2001 From: Patrick Norton Date: Tue, 31 May 2022 05:10:48 -0600 Subject: [PATCH 3/3] Fixed type error --- src/ascii_string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ascii_string.rs b/src/ascii_string.rs index 014d72a..8b713e5 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -188,7 +188,7 @@ impl AsciiString { #[inline] pub fn insert_str(&mut self, idx: usize, string: &AsciiStr) { self.vec.reserve(string.len()); - self.vec.splice(idx..idx, string); + self.vec.splice(idx..idx, string.into_iter().copied()); } /// Returns the number of bytes that this ASCII string buffer can hold without reallocating.