[−][src]Struct multisplice::Multisplice
A multisplice operation.
Methods
impl<'a> Multisplice<'a>
[src]
pub fn new(source: &'a str) -> Self
[src]
Create a "multisplicer" for the given string.
pub fn splice(
&mut self,
start: usize,
end: usize,
value: impl Into<Cow<'a, str>>
)
[src]
&mut self,
start: usize,
end: usize,
value: impl Into<Cow<'a, str>>
)
Replace the characters from index start
up to (but not including) index end
by the
string value
.
If the replacement lifetime outlives the input string, you can pass in cheap &str references.
Else, pass in an owned String using replacement.to_string()
.
Example
use multisplice::Multisplice; let mut splicer = Multisplice::new("a b c d e"); splicer.splice(2, 3, "beep"); { let replacement = "boop".to_string(); splicer.splice(6, 7, replacement); } assert_eq!(splicer.to_string(), "a beep c boop e");
pub fn splice_range(
&mut self,
range: impl RangeBounds<usize>,
value: impl Into<Cow<'a, str>>
)
[src]
&mut self,
range: impl RangeBounds<usize>,
value: impl Into<Cow<'a, str>>
)
Replace the characters in the range range
by the string value
.
If the replacement lifetime outlives the input string, you can pass in cheap &str references.
Else, pass in an owned String using replacement.to_string()
.
Example
use multisplice::Multisplice; let mut splicer = Multisplice::new("a b c d e"); splicer.splice_range(2..3, "beep"); { let replacement = "boop".to_string(); splicer.splice_range(6.., replacement); } assert_eq!(splicer.to_string(), "a beep c boop");
pub fn slice(&self, start: usize, end: usize) -> Cow<'a, str>
[src]
Get a part of the spliced string, using indices start
to end
(exclusive) from the
original string.
If the start
or end
indices are in the middle of a spliced range, the full value of the
splice is included in the return value. For example, when indices 1-10 were replaced with a
value "Hello World", requesting a slice of indices 7-20 will return the entire "Hello
World" string followed by indices 11-20.
Example
use multisplice::Multisplice; use std::borrow::Cow; let mut splicer = Multisplice::new("a b c d e"); splicer.splice(2, 3, "beep"); splicer.splice(6, 7, "boop"); assert_eq!(splicer.slice(2, 5), "beep c"); // Does not allocate a new String if there were no changes assert_eq!(splicer.slice(3, 6), Cow::Borrowed(" c "));
use multisplice::Multisplice; let mut splicer = Multisplice::new("a b c d e"); splicer.splice(2, 7, "beep and boop"); assert_eq!(splicer.to_string(), "a beep and boop e"); // Slicing in the middle of a spliced range: assert_eq!(splicer.slice(0, 5), "a beep and boop"); assert_eq!(splicer.slice(6, 9), "beep and boop e");
pub fn slice_range(&self, range: impl RangeBounds<usize>) -> Cow<'a, str>
[src]
Slice using range syntax.
use multisplice::Multisplice; let source = "a b c d e"; let mut splicer = Multisplice::new(source); splicer.splice(2, 3, "beep"); splicer.splice(6, 7, "boop"); assert_eq!(splicer.slice_range((..)), "a beep c boop e"); assert_eq!(splicer.slice_range((2..)), "beep c boop e"); assert_eq!(splicer.slice_range((3..7)), " c boop"); assert_eq!(splicer.slice_range((4..=6)), "c boop");
Trait Implementations
impl<'_> ToString for Multisplice<'_>
[src]
impl<'a> Debug for Multisplice<'a>
[src]
Auto Trait Implementations
impl<'a> Send for Multisplice<'a>
impl<'a> Sync for Multisplice<'a>
impl<'a> Unpin for Multisplice<'a>
impl<'a> UnwindSafe for Multisplice<'a>
impl<'a> RefUnwindSafe for Multisplice<'a>
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,