pub fn string_to_snbt(string: &str) -> Cow<'_, str> {
        if !Self::should_quote(string) {
            return Cow::Borrowed(string);
        }

        // Determine the best option for the surrounding quotes to minimize escape sequences
        let surrounding: char;
        if string.contains('"') {
            surrounding = '\'';
        } else {
            surrounding = '"';
        }

        let mut snbt_string = String::with_capacity(2 + string.len());
        snbt_string.push(surrounding);

        // Construct the string accounting for escape sequences
        for ch in string.chars() {
            match ch {
                '\n' => {
                    snbt_string.push_str("\\n");
                    continue;
                }
                '\r' => {
                    snbt_string.push_str("\\r");
                    continue;
                }
                '\t' => {
                    snbt_string.push_str("\\t");
                    continue;
                }
                _ =>
                    if ch == surrounding || ch == '\\' {
                        snbt_string.push('\\');
                    },
            }
            snbt_string.push(ch);
        }

        snbt_string.push(surrounding);
        Cow::Owned(snbt_string)
    }
/// Wraps the given string in quotes and escapes any quotes contained in the original string.
///
/// # Examples
///
/// ```
/// # use quartz_nbt::NbtTag;
/// use std::borrow::Cow;
///
/// assert_eq!(NbtTag::string_to_snbt("string"), Cow::Borrowed("string"));
/// assert_eq!(
///     NbtTag::string_to_snbt("\\\n\r\t'\""),
///     Cow::<str>::Owned(String::from(r#"'\\\n\r\t\'"'"#))
/// );
/// ```
Wraps the given string in quotes and escapes any quotes contained in the original string.
Wraps the given string in quotes and escapes any quotes contained in the original string .
pub fn string_to_snbt(string: &str) -> Cow<'_, str> {
        if !Self::should_quote(string) {
            return Cow::Borrowed(string);
        }
        // Determine the best option for the surrounding quotes to minimize escape sequences
        let surrounding: char;
        if string.contains('"') {
            surrounding = '\'';
        } else {
            surrounding = '"';
        }
        let mut snbt_string = String::with_capacity(2 + string.len());
        snbt_string.push(surrounding);
        // Construct the string accounting for escape sequences
        for ch in string.chars() {
            match ch {
                '\n' => {
                    snbt_string.push_str("\\n");
                    continue;
                }
                '\r' => {
                    snbt_string.push_str("\\r");
                    continue;
                }
                '\t' => {
                    snbt_string.push_str("\\t");
                    continue;
                }
                _ =>
                    if ch == surrounding || ch == '\\' {
                        snbt_string.push('\\');
                    },
            }
            snbt_string.push(ch);
        }
        snbt_string.push(surrounding);
        Cow::Owned(snbt_string)
    }
pub fn string_to_snbt ( string : & str ) -> Cow < ' _ , str > { if ! Self :: should_quote ( string ) { return Cow :: Borrowed ( string ) ; } // Determine the best option for the surrounding quotes to minimize escape sequences let surrounding : char ; if string . contains ( '"' ) { surrounding = '\'' ; } else { surrounding = '"' ; } let mut snbt_string = String :: with_capacity ( 2 + string . len ( ) ) ; snbt_string . push ( surrounding ) ; // Construct the string accounting for escape sequences for ch in string . chars ( ) { match ch { '\n' => { snbt_string . push_str ( " \\ " ) ; continue ; } '\r' => { snbt_string . push_str ( " \\ " ) ; continue ; } '\t' => { snbt_string . push_str ( " \\ " ) ; continue ; } _ => if ch == surrounding || ch == '\\' { snbt_string . push ( '\\' ) ; } , } snbt_string . push ( ch ) ; } snbt_string . push ( surrounding ) ; Cow :: Owned ( snbt_string ) }
Wraps the given string in quotes and escapes any quotes contained in the original string.
Wraps the given string in quotes and escapes any quotes contained in the original string .
string
&str
string
&str
examples
None
use quartz_nbt::nbttag;
None