/// Convert a hexadecimal-encoded string to its corresponding bytes
Convert a hexadecimal-encoded string to its corresponding bytes
Convert a hexadecimal - encoded string to its corresponding bytes
pub fn hex_bytes(data: &str) -> Result<Vec<u8>, encode::Error> {
    // This code is optimized to be as fast as possible without using unsafe or platform specific
    // features. If you want to refactor it please make sure you don't introduce performance
    // regressions (run the benchmark with `cargo bench --features unstable`).
    // If the hex string has an uneven length fail early
    if data.len() % 2 != 0 {
        return Err(encode::Error::ParseFailed("hexstring of odd length"));
    }
    // Preallocate the uninitialized memory for the byte array
    let mut res = Vec::with_capacity(data.len() / 2);
    let mut hex_it = data.bytes();
    loop {
        // Get most significant nibble of current byte or end iteration
        let msn = match hex_it.next() {
            None => break,
            Some(x) => x,
        };
        // Get least significant nibble of current byte
        let lsn = match hex_it.next() {
            None => unreachable!("len % 2 == 0"),
            Some(x) => x,
        };
        // Convert bytes representing characters to their represented value and combine lsn and msn.
        // The and_then and map are crucial for performance, in comparison to using ? and then
        // using the results of that for the calculation it's nearly twice as fast. Using bit
        // shifting and or instead of multiply and add on the other hand doesn't show a significant
        // increase in performance.
        match hex_val(msn).and_then(|msn_val| hex_val(lsn).map(|lsn_val| msn_val * 16 + lsn_val)) {
            Ok(x) => res.push(x),
            Err(e) => return Err(e),
        }
    }
    Ok(res)
}
pub fn hex_bytes ( data : & str ) -> Result < Vec < u8 > , encode :: Error > { // This code is optimized to be as fast as possible without using unsafe or platform specific // features. If you want to refactor it please make sure you don't introduce performance // regressions (run the benchmark with `cargo bench --features unstable`). // If the hex string has an uneven length fail early if data . len ( ) % 2 != 0 { return Err ( encode :: Error :: ParseFailed ( " " ) ) ; } // Preallocate the uninitialized memory for the byte array let mut res = Vec :: with_capacity ( data . len ( ) / 2 ) ; let mut hex_it = data . bytes ( ) ; loop { // Get most significant nibble of current byte or end iteration let msn = match hex_it . next ( ) { None => break , Some ( x ) => x , } ; // Get least significant nibble of current byte let lsn = match hex_it . next ( ) { None => unreachable ! ( " " ) , Some ( x ) => x , } ; // Convert bytes representing characters to their represented value and combine lsn and msn. // The and_then and map are crucial for performance, in comparison to using ? and then // using the results of that for the calculation it's nearly twice as fast. Using bit // shifting and or instead of multiply and add on the other hand doesn't show a significant // increase in performance. match hex_val ( msn ) . and_then ( | msn_val | hex_val ( lsn ) . map ( | lsn_val | msn_val * 16 + lsn_val ) ) { Ok ( x ) => res . push ( x ) , Err ( e ) => return Err ( e ) , } } Ok ( res ) }
Convert a hexadecimal-encoded string to its corresponding bytes
Convert a hexadecimal - encoded string to its corresponding bytes
data
&str