Trait OsStrTools

Source
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§

Source

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.

Source

fn split_lines(&self) -> Vec<&OsStr>

Splits lines by looking for "\n".

Same as .split("\n").

Source

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.

Source

fn trim_start(&self, pat: impl Bytes) -> &OsStr

Remove all matches of the pattern until a different character is found.

Returns the rest.

Source

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.

Source

fn contains(&self, pat: impl Bytes) -> bool

Check if the string contains the pattern.

Source

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.

Source

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");
Source

fn quote_double(&self) -> OsString

Wraps the string with double quote characters.

Source

fn quote_single(&self) -> OsString

Wraps the string with single quote characters.

Source

fn escape_double_quote(&self) -> OsString

Replace double quote characters in the string with "\\\"" for safe handling over to a shell.

Source

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.

Implementations on Foreign Types§

Source§

impl OsStrTools for OsStr

Source§

fn split(&self, pat: impl Bytes) -> Vec<&OsStr>

Source§

fn replace(&self, pat: impl Bytes, with: impl Bytes) -> OsString

Source§

fn split_lines(&self) -> Vec<&OsStr>

Source§

fn quote_double(&self) -> OsString

Source§

fn quote_single(&self) -> OsString

Source§

fn splice<'a, B: Bytes>( &'a self, pat: impl Bytes, to: &'a [B], ) -> StringSplicer<'a, B>

Source§

fn trim_start(&self, pat: impl Bytes) -> &OsStr

Source§

fn trim_end(&self, pat: impl Bytes) -> &OsStr

Source§

fn contains(&self, pat: impl Bytes) -> bool

Source§

fn position(&self, pat: impl Bytes) -> Option<usize>

Source§

fn escape_double_quote(&self) -> OsString

Source§

fn escape_single_quote(&self) -> OsString

Implementors§