Skip to content

Commit

Permalink
Add OsStrBytesExt::repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Nov 17, 2023
1 parent 911d768 commit 06b74d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::ffi::OsStr;
use std::ffi::OsString;
use std::iter;
use std::mem;
use std::ops::Range;
use std::ops::RangeFrom;
Expand Down Expand Up @@ -258,6 +260,21 @@ pub trait OsStrBytesExt: OsStrBytes {
where
I: SliceIndex;

/// Equivalent to [`str::repeat`].
///
/// # Examples
///
/// ```
/// use std::ffi::OsStr;
///
/// use os_str_bytes::OsStrBytesExt;
///
/// let os_string = OsStr::new("foo");
/// assert_eq!("foofoofoo", os_string.repeat(3));
/// ```
#[must_use]
fn repeat(&self, n: usize) -> Self::Owned;

/// Equivalent to [`str::rfind`].
///
/// # Examples
Expand Down Expand Up @@ -597,6 +614,13 @@ impl OsStrBytesExt for OsStr {
index.index(self)
}

#[inline]
fn repeat(&self, n: usize) -> Self::Owned {
let mut string = OsString::new();
string.extend(iter::repeat(self).take(n));
string
}

#[inline]
fn rfind<P>(&self, pat: P) -> Option<usize>
where
Expand Down
16 changes: 16 additions & 0 deletions src/raw_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,22 @@ impl RawOsStr {
self.as_os_str().is_empty()
}

/// Equivalent to [`OsStrBytesExt::repeat`].
///
/// # Examples
///
/// ```
/// use os_str_bytes::RawOsStr;
///
/// let raw = RawOsStr::new("foo");
/// assert_eq!("foofoofoo", raw.repeat(3));
/// ```
#[inline]
#[must_use]
pub fn repeat(&self, n: usize) -> RawOsString {
RawOsString::new(self.as_os_str().repeat(n))
}

/// Equivalent to [`OsStrBytesExt::rfind`].
///
/// # Examples
Expand Down

0 comments on commit 06b74d1

Please sign in to comment.