string_more

Trait StrExt

Source
pub trait StrExt: Sailed {
    // Required methods
    fn fill_start(&self, fill: impl EncodeUtf8, times: usize) -> String;
    fn fill_end(&self, fill: impl EncodeUtf8, times: usize) -> String;
    fn center(&self, fill: impl EncodeUtf8, times: usize) -> String;
    fn enclose(
        &self,
        fill_start: impl EncodeUtf8,
        fill_end: impl EncodeUtf8,
    ) -> String;
    fn expand_tabs(&self, tabsize: usize) -> String;
    fn shift(&self, index: usize, count: usize, fill: impl EncodeUtf8) -> String;
    fn levenshtein_distance(&self, other: &str) -> usize;
    fn hamming_distance(&self, other: &str) -> Option<usize>;
    fn char_frequencies<M: HzMap>(&self) -> M;
}
Expand description

The StrExt trait extends the standard library functionality for immutable string slices (&str), providing advanced string manipulation utilities.

This trait enables a set of operations that return a modified String based on transformations of the original string slice. It offers various padding, enclosing, and whitespace-expansion functions useful for formatting and text processing.

Each method preserves the original slice and returns a new String with the modifications applied.

§Examples

use string_more::StrExt;

let example = "example";
assert_eq!(example.fill_start("*", 3), "***example");
assert_eq!(example.center("-", 4), "----example----");

Required Methods§

Source

fn fill_start(&self, fill: impl EncodeUtf8, times: usize) -> String

Returns a new String where the specified fill is prepended to the original slice times times.

Source

fn fill_end(&self, fill: impl EncodeUtf8, times: usize) -> String

Returns a new String where the specified fill is appended to the original slice times times.

Source

fn center(&self, fill: impl EncodeUtf8, times: usize) -> String

Centers the original slice in a new String, padding both the beginning and end with fill, repeating times times on each side for a balanced result.

Source

fn enclose( &self, fill_start: impl EncodeUtf8, fill_end: impl EncodeUtf8, ) -> String

Returns a new String with fill_start and fill_end added at the beginning and end of the original slice, respectively.

Source

fn expand_tabs(&self, tabsize: usize) -> String

Expands all tab characters (\t) in the original slice, replacing each tab with tabsize spaces.

Source

fn shift(&self, index: usize, count: usize, fill: impl EncodeUtf8) -> String

Shifts the characters starting at the specified index in the original slice by count positions, filling the gap with the specified fill characters.

§Panics

Panics if the index do not lie on a char boundary, or if it is out of bounds.

Source

fn levenshtein_distance(&self, other: &str) -> usize

Computes the Levenshtein distance between the strings. The strings may have different lengths.

Source

fn hamming_distance(&self, other: &str) -> Option<usize>

Computes the Hamming distance between the strings. The strings must have the same lengths, otherwise this function returns None.

Source

fn char_frequencies<M: HzMap>(&self) -> M

Computes the frequency of chars in the string. The user can specify the output map in which the frequencies will be stored.

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.

Implementors§

Source§

impl<T> StrExt for T
where T: Sailed + Deref<Target = str>,