fn literal<'a>(remainder: &'a [u8], sanitised: &mut String, break_on_repl: bool) -> &'a [u8] {
    if remainder.len() == 0 {
        return &[];
    }

    let current = remainder[0];

    match current {
        //Key
        b'"'|b'\'' => {
            enum StringState {
                Unescaped,
                Escaped
            }

            let (rest, key) = take_while(&remainder[1..], StringState::Unescaped, 
                |s, c| {
                    match (s, c) {
                        //Escape char
                        (StringState::Unescaped, b'\\') => {
                            (StringState::Escaped, true)
                        },
                        //Ignore char after escape
                        (StringState::Escaped, _) => {
                            (StringState::Unescaped, true)
                        },
                        //Unescaped quote
                        (StringState::Unescaped, c) if c == current => {
                            (StringState::Unescaped, false)
                        },
                        //Anything else
                        _ => {
                            (StringState::Unescaped, true)
                        }
                    }
                }
            );

            sanitised.push('"');
            sanitised.push_str(key);
            sanitised.push('"');

            literal(&rest[1..], sanitised, break_on_repl)
        },
        //Start of item
        b'{'|b'['|b':' => {
            sanitised.push(current as char);

            literal(&remainder[1..], sanitised, break_on_repl)
        },
        //Trim whitespace
        b' '|b'\r'|b'\n'|b'\t' => {
            literal(&remainder[1..], sanitised, break_on_repl)
        },
        //Unquoted key
        b if (b as char).is_alphabetic() => {
            let (rest, key) = take_while(remainder, (), |_, c| {
                ((), is_ident(c as char))
            });

            //Check if the string is a special value; true, false or null
            //For special values, push them as straight unquoted values. Otherwise, quote them
            match key {
                "true"|"false"|"null" =>
                    sanitised.push_str(key),
                _ => {
                    sanitised.push('"');
                    sanitised.push_str(key);
                    sanitised.push('"');
                }
            }

            literal(rest, sanitised, break_on_repl)
        },
        //Number
        b if (b as char).is_numeric() => {
            let (rest, key) = take_while(remainder, (), |_, c| {
                ((), {
                   (c as char).is_numeric() || 
                    c == b'.' || 
                    c == b'+' || 
                    c == b'-' || 
                    c == b'e' || 
                    c == b'E'
                })
            });

            sanitised.push_str(key);

            literal(rest, sanitised, break_on_repl)
        },
        //Replacement
        b'$' if break_on_repl => {
            //Strip trailing whitespace
            let remainder = shift_while(&remainder[1..], |c| c == b' ');

            remainder
        },
        //Other chars
        _ => {
            sanitised.push(current as char);

            literal(&remainder[1..], sanitised, break_on_repl)
        }
    }
}
// Parse a literal and maybe break on a replacement token.
Parse a literal and maybe break on a replacement token.
Parse a literal and maybe break on a replacement token .
fn literal<'a>(remainder: &'a [u8], sanitised: &mut String, break_on_repl: bool) -> &'a [u8] {
    if remainder.len() == 0 {
        return &[];
    }
    let current = remainder[0];
    match current {
        //Key
        b'"'|b'\'' => {
            enum StringState {
                Unescaped,
                Escaped
            }
            let (rest, key) = take_while(&remainder[1..], StringState::Unescaped, 
                |s, c| {
                    match (s, c) {
                        //Escape char
                        (StringState::Unescaped, b'\\') => {
                            (StringState::Escaped, true)
                        },
                        //Ignore char after escape
                        (StringState::Escaped, _) => {
                            (StringState::Unescaped, true)
                        },
                        //Unescaped quote
                        (StringState::Unescaped, c) if c == current => {
                            (StringState::Unescaped, false)
                        },
                        //Anything else
                        _ => {
                            (StringState::Unescaped, true)
                        }
                    }
                }
            );
            sanitised.push('"');
            sanitised.push_str(key);
            sanitised.push('"');
            literal(&rest[1..], sanitised, break_on_repl)
        },
        //Start of item
        b'{'|b'['|b':' => {
            sanitised.push(current as char);
            literal(&remainder[1..], sanitised, break_on_repl)
        },
        //Trim whitespace
        b' '|b'\r'|b'\n'|b'\t' => {
            literal(&remainder[1..], sanitised, break_on_repl)
        },
        //Unquoted key
        b if (b as char).is_alphabetic() => {
            let (rest, key) = take_while(remainder, (), |_, c| {
                ((), is_ident(c as char))
            });
            //Check if the string is a special value; true, false or null
            //For special values, push them as straight unquoted values. Otherwise, quote them
            match key {
                "true"|"false"|"null" =>
                    sanitised.push_str(key),
                _ => {
                    sanitised.push('"');
                    sanitised.push_str(key);
                    sanitised.push('"');
                }
            }
            literal(rest, sanitised, break_on_repl)
        },
        //Number
        b if (b as char).is_numeric() => {
            let (rest, key) = take_while(remainder, (), |_, c| {
                ((), {
                   (c as char).is_numeric() || 
                    c == b'.' || 
                    c == b'+' || 
                    c == b'-' || 
                    c == b'e' || 
                    c == b'E'
                })
            });
            sanitised.push_str(key);
            literal(rest, sanitised, break_on_repl)
        },
        //Replacement
        b'$' if break_on_repl => {
            //Strip trailing whitespace
            let remainder = shift_while(&remainder[1..], |c| c == b' ');
            remainder
        },
        //Other chars
        _ => {
            sanitised.push(current as char);
            literal(&remainder[1..], sanitised, break_on_repl)
        }
    }
}
fn literal < ' a > ( remainder : & ' a [ u8 ] , sanitised : & mut String , break_on_repl : bool ) -> & ' a [ u8 ] { if remainder . len ( ) == 0 { return & [ ] ; } let current = remainder [ 0 ] ; match current { //Key b'"' | b'\'' => { enum StringState { Unescaped , Escaped } let ( rest , key ) = take_while ( & remainder [ 1 .. ] , StringState :: Unescaped , | s , c | { match ( s , c ) { //Escape char ( StringState :: Unescaped , b'\\' ) => { ( StringState :: Escaped , true ) } , //Ignore char after escape ( StringState :: Escaped , _ ) => { ( StringState :: Unescaped , true ) } , //Unescaped quote ( StringState :: Unescaped , c ) if c == current => { ( StringState :: Unescaped , false ) } , //Anything else _ => { ( StringState :: Unescaped , true ) } } } ) ; sanitised . push ( '"' ) ; sanitised . push_str ( key ) ; sanitised . push ( '"' ) ; literal ( & rest [ 1 .. ] , sanitised , break_on_repl ) } , //Start of item b'{' | b'[' | b':' => { sanitised . push ( current as char ) ; literal ( & remainder [ 1 .. ] , sanitised , break_on_repl ) } , //Trim whitespace b' ' | b'\r' | b'\n' | b'\t' => { literal ( & remainder [ 1 .. ] , sanitised , break_on_repl ) } , //Unquoted key b if ( b as char ) . is_alphabetic ( ) => { let ( rest , key ) = take_while ( remainder , ( ) , | _ , c | { ( ( ) , is_ident ( c as char ) ) } ) ; //Check if the string is a special value; true, false or null //For special values, push them as straight unquoted values. Otherwise, quote them match key { " " | " " | " " => sanitised . push_str ( key ) , _ => { sanitised . push ( '"' ) ; sanitised . push_str ( key ) ; sanitised . push ( '"' ) ; } } literal ( rest , sanitised , break_on_repl ) } , //Number b if ( b as char ) . is_numeric ( ) => { let ( rest , key ) = take_while ( remainder , ( ) , | _ , c | { ( ( ) , { ( c as char ) . is_numeric ( ) || c == b'.' || c == b'+' || c == b'-' || c == b'e' || c == b'E' } ) } ) ; sanitised . push_str ( key ) ; literal ( rest , sanitised , break_on_repl ) } , //Replacement b'$' if break_on_repl => { //Strip trailing whitespace let remainder = shift_while ( & remainder [ 1 .. ] , | c | c == b' ' ) ; remainder } , //Other chars _ => { sanitised . push ( current as char ) ; literal ( & remainder [ 1 .. ] , sanitised , break_on_repl ) } } }
Parse a literal and maybe break on a replacement token.
Parse a literal and maybe break on a replacement token .
remainder
&'a [u8]
sanitised
&mut String
break_on_repl
bool
remainder
&'a [u8]
sanitised
&mut String
break_on_repl
bool