fn parse_proc_status(data: &[u8]) -> std::collections::HashMap<String, Vec<u8>> {
    // Format of input is: [Key] ":" [whitespace] [value] "\n".
    // Parse it char-by-char using simple state machine.
    enum ParseState {
        Key,
        Separator,
        Value,
    }

    let mut fields = std::collections::HashMap::<String, Vec<u8>>::new();
    let mut parse_state = ParseState::Key;
    let mut current_key = Vec::<u8>::new();
    let mut current_value = Vec::<u8>::new();

    for ch in data {
        match parse_state {
            ParseState::Key => {
                if *ch == b':' {
                    parse_state = ParseState::Separator;
                } else {
                    current_key.push(*ch);
                }
            }
            ParseState::Separator => {
                if *ch != b' ' && *ch != b'\t' {
                    parse_state = ParseState::Value;
                    current_value.push(*ch);
                } else if *ch == b'\n' {
                    // Keys are always ascii, hence valid utf8, so this
                    // always succeeds. Formally, ignore malformed field names.
                    if let Ok(key) = std::str::from_utf8(&current_key) {
                        fields.insert(key.to_string(), std::mem::take(&mut current_value));
                        current_key.clear();
                    }
                }
            }
            ParseState::Value => {
                if *ch == b'\n' {
                    if let Ok(key) = std::str::from_utf8(&current_key) {
                        fields.insert(key.to_string(), std::mem::take(&mut current_value));
                        current_key.clear();
                    }
                    parse_state = ParseState::Key;
                } else {
                    current_value.push(*ch);
                }
            }
        }
    }

    fields
}
// Parse the contents of /proc/<pid>/status as key/value pairs. Note that
// input must be assumed to be u8 because there are no guarantees that the
// contents are valid utf8 (in particular the "name" portion). We can only
// assume that about the keys which are from a fixed known set.
Parse the contents of /proc//status as key/value pairs. Note that
input must be assumed to be u8 because there are no guarantees that the
contents are valid utf8 (in particular the "name" portion). We can only
assume that about the keys which are from a fixed known set.
Parse the contents of / proc // status as key / value pairs . Note that input must be assumed to be u8 because there are no guarantees that the contents are valid utf8 ( in particular the " name " portion ) . We can only assume that about the keys which are from a fixed known set .
fn parse_proc_status(data: &[u8]) -> std::collections::HashMap<String, Vec<u8>> {
    // Format of input is: [Key] ":" [whitespace] [value] "\n".
    // Parse it char-by-char using simple state machine.
    enum ParseState {
        Key,
        Separator,
        Value,
    }
    let mut fields = std::collections::HashMap::<String, Vec<u8>>::new();
    let mut parse_state = ParseState::Key;
    let mut current_key = Vec::<u8>::new();
    let mut current_value = Vec::<u8>::new();
    for ch in data {
        match parse_state {
            ParseState::Key => {
                if *ch == b':' {
                    parse_state = ParseState::Separator;
                } else {
                    current_key.push(*ch);
                }
            }
            ParseState::Separator => {
                if *ch != b' ' && *ch != b'\t' {
                    parse_state = ParseState::Value;
                    current_value.push(*ch);
                } else if *ch == b'\n' {
                    // Keys are always ascii, hence valid utf8, so this
                    // always succeeds. Formally, ignore malformed field names.
                    if let Ok(key) = std::str::from_utf8(&current_key) {
                        fields.insert(key.to_string(), std::mem::take(&mut current_value));
                        current_key.clear();
                    }
                }
            }
            ParseState::Value => {
                if *ch == b'\n' {
                    if let Ok(key) = std::str::from_utf8(&current_key) {
                        fields.insert(key.to_string(), std::mem::take(&mut current_value));
                        current_key.clear();
                    }
                    parse_state = ParseState::Key;
                } else {
                    current_value.push(*ch);
                }
            }
        }
    }
    fields
}
fn parse_proc_status ( data : & [ u8 ] ) -> std :: collections :: HashMap < String , Vec < u8 > > { // Format of input is: [Key] ":" [whitespace] [value] "\n". // Parse it char-by-char using simple state machine. enum ParseState { Key , Separator , Value , } let mut fields = std :: collections :: HashMap :: < String , Vec < u8 > > :: new ( ) ; let mut parse_state = ParseState :: Key ; let mut current_key = Vec :: < u8 > :: new ( ) ; let mut current_value = Vec :: < u8 > :: new ( ) ; for ch in data { match parse_state { ParseState :: Key => { if * ch == b':' { parse_state = ParseState :: Separator ; } else { current_key . push ( * ch ) ; } } ParseState :: Separator => { if * ch != b' ' && * ch != b'\t' { parse_state = ParseState :: Value ; current_value . push ( * ch ) ; } else if * ch == b'\n' { // Keys are always ascii, hence valid utf8, so this // always succeeds. Formally, ignore malformed field names. if let Ok ( key ) = std :: str :: from_utf8 ( & current_key ) { fields . insert ( key . to_string ( ) , std :: mem :: take ( & mut current_value ) ) ; current_key . clear ( ) ; } } } ParseState :: Value => { if * ch == b'\n' { if let Ok ( key ) = std :: str :: from_utf8 ( & current_key ) { fields . insert ( key . to_string ( ) , std :: mem :: take ( & mut current_value ) ) ; current_key . clear ( ) ; } parse_state = ParseState :: Key ; } else { current_value . push ( * ch ) ; } } } } fields }
Parse the contents of /proc/<pid>/status as key/value pairs.
Parse the contents of / proc / <pid > / status as key / value pairs .
data
&[u8]
data
&[u8]