[][src]Trait osstrtools::OsStrTools

pub trait OsStrTools {
    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; }

Extension Trait for OsStr to make working OsStr them more ergonomic. It contains many methods that work similarly to those of String.

Required methods

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.

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

Splits lines by looking for "\n".

Same as .split("\n").

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.

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

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

Returns the rest.

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.

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

Check if the string contains the pattern.

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.

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

fn quote_double(&self) -> OsString

Wraps the string with double quote characters.

fn quote_single(&self) -> OsString

Wraps the string with single quote characters.

fn escape_double_quote(&self) -> OsString

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

fn escape_single_quote(&self) -> OsString

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

Loading content...

Implementations on Foreign Types

impl OsStrTools for OsStr[src]

Loading content...

Implementors

Loading content...