pub trait OsStrTools {
// Required methods
fn split(&self, pat: impl Bytes) -> Vec<&OsStr>;
fn split_lines(&self) -> Vec<&OsStr>;
fn replace(&self, pat: impl Bytes, to: impl Bytes) -> OsString;
fn trim_start(&self, pat: impl Bytes) -> &OsStr;
fn trim_end(&self, pat: impl Bytes) -> &OsStr;
fn contains(&self, pat: impl Bytes) -> bool;
fn position(&self, pat: impl Bytes) -> Option<usize>;
fn splice<'a, B: Bytes>(
&'a self,
from: impl Bytes,
to: &'a [B],
) -> StringSplicer<'a, B>;
fn quote_double(&self) -> OsString;
fn quote_single(&self) -> OsString;
fn escape_double_quote(&self) -> OsString;
fn escape_single_quote(&self) -> OsString;
}
Expand description
Extension Trait for OsStr to make working OsStr them more
ergonomic. It contains many methods that work similarly to those
of String
.
Required Methods§
Sourcefn split(&self, pat: impl Bytes) -> Vec<&OsStr>
fn split(&self, pat: impl Bytes) -> Vec<&OsStr>
Split the string by looking for pat.
Splits don’t include the pattern itself. Like the stdlib multiple consecutive matches result in empty strings placed in between.
If the string starts with the pattern the first element will be an empty string. If the string ends with the pattern, there will be an empty string at the end.
Sourcefn split_lines(&self) -> Vec<&OsStr>
fn split_lines(&self) -> Vec<&OsStr>
Splits lines by looking for "\n"
.
Same as .split("\n")
.
Sourcefn replace(&self, pat: impl Bytes, to: impl Bytes) -> OsString
fn replace(&self, pat: impl Bytes, to: impl Bytes) -> OsString
Replace all matches with something else.
If no matches are found the returned string contains the whole original string.
Sourcefn trim_start(&self, pat: impl Bytes) -> &OsStr
fn trim_start(&self, pat: impl Bytes) -> &OsStr
Remove all matches of the pattern until a different character is found.
Returns the rest.
Sourcefn trim_end(&self, pat: impl Bytes) -> &OsStr
fn trim_end(&self, pat: impl Bytes) -> &OsStr
Remove all matches of the pattern from the end until a different character is found.
Returns the rest.
Sourcefn position(&self, pat: impl Bytes) -> Option<usize>
fn position(&self, pat: impl Bytes) -> Option<usize>
Looks through the string for the pattern.
The position of the first match is returned. If no matches are
found it returns None
.
Sourcefn splice<'a, B: Bytes>(
&'a self,
from: impl Bytes,
to: &'a [B],
) -> StringSplicer<'a, B>
fn splice<'a, B: Bytes>( &'a self, from: impl Bytes, to: &'a [B], ) -> StringSplicer<'a, B>
Similar to replace, but instead of inserting a single replacement string this allows inserting multiple strings at the match position.
Returns a StringSplicer
which allows to specify how the
strings should be inserted.
§Examples
use std::ffi::OsStr;
use osstrtools::{OsStrTools, StringSplicer};
let strings: &[&OsStr] = &[OsStr::new("is"),
OsStr::new("an"),
OsStr::new("example")];
let s = OsStr::new("this #");
let mut splicer: StringSplicer<'_, _> = s.splice("#", strings);
let result = splicer.assemble_with_sep(" ");
assert_eq!(result, "this is an example");
Sourcefn quote_double(&self) -> OsString
fn quote_double(&self) -> OsString
Wraps the string with double quote characters.
Sourcefn quote_single(&self) -> OsString
fn quote_single(&self) -> OsString
Wraps the string with single quote characters.
Sourcefn escape_double_quote(&self) -> OsString
fn escape_double_quote(&self) -> OsString
Replace double quote characters in the string with "\\\""
for
safe handling over to a shell.
Sourcefn escape_single_quote(&self) -> OsString
fn escape_single_quote(&self) -> OsString
Replace single quote characters in the string with "\\''"
for
safe handling over to a shell.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.