Try to decode bytes from hex literal string. None will be returned if the input literal is hex-invalid.
(s: &str)
| 308 | /// |
| 309 | /// None will be returned if the input literal is hex-invalid. |
| 310 | fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> { |
| 311 | let hex_bytes = s.as_bytes(); |
| 312 | |
| 313 | let mut decoded_bytes = Vec::with_capacity(hex_bytes.len().div_ceil(2)); |
| 314 | |
| 315 | let start_idx = hex_bytes.len() % 2; |
| 316 | if start_idx > 0 { |
| 317 | // The first byte is formed of only one char. |
| 318 | decoded_bytes.push(try_decode_hex_char(hex_bytes[0])?); |
| 319 | } |
| 320 | |
| 321 | for i in (start_idx..hex_bytes.len()).step_by(2) { |
| 322 | let high = try_decode_hex_char(hex_bytes[i])?; |
| 323 | let low = try_decode_hex_char(hex_bytes[i + 1])?; |
| 324 | decoded_bytes.push((high << 4) | low); |
| 325 | } |
| 326 | |
| 327 | Some(decoded_bytes) |
| 328 | } |
| 329 | |
| 330 | /// Try to decode a byte from a hex char. |
| 331 | /// |
searching dependent graphs…