Skip to main content

EscapeCharacters

Trait EscapeCharacters 

Source
pub trait EscapeCharacters<'a> {
    // Required methods
    fn escape_characters(
        self,
        escape_character: char,
        escaped_characters: &[char],
    ) -> Cow<'a, str>;
    fn escape_ascii_characters(
        self,
        escape_character: u8,
        escaped_characters: &[u8],
    ) -> Cow<'a, str>;
}
Available on crate feature alloc only.
Expand description

To extend str and Cow<str> to have escape_characters and escape_ascii_characters method.

Typical use cases include preparing strings for SQL LIKE queries or other contexts where certain characters need to be escaped.

Required Methods§

Source

fn escape_characters( self, escape_character: char, escaped_characters: &[char], ) -> Cow<'a, str>

Escapes all occurrences of the specified characters within a string.

This function scans the input string for any character that matches one of the escaped_characters or the escape_character itself, and prefixes those characters with the provided escape_character.

Source

fn escape_ascii_characters( self, escape_character: u8, escaped_characters: &[u8], ) -> Cow<'a, str>

Escapes ASCII characters within a UTF-8 string.

Similar to EscapeCharacters::escape_characters, but operates directly on bytes instead of Unicode scalar values. This version is optimized for ASCII-only escaping and avoids unnecessary Unicode conversions.

NOTE: The escape_character must be an ASCII character (i.e., in the range 0x00 to 0x7F) for this method to work correctly.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a> EscapeCharacters<'a> for &'a str

Source§

fn escape_characters( self, escape_character: char, escaped_characters: &[char], ) -> Cow<'a, str>

Source§

fn escape_ascii_characters( self, escape_character: u8, escaped_characters: &[u8], ) -> Cow<'a, str>

Source§

impl<'a> EscapeCharacters<'a> for Cow<'a, str>

Source§

fn escape_characters( self, escape_character: char, escaped_characters: &[char], ) -> Cow<'a, str>

Source§

fn escape_ascii_characters( self, escape_character: u8, escaped_characters: &[u8], ) -> Cow<'a, str>

Implementors§