| 1199 | } |
| 1200 | |
| 1201 | pub fn bytes_to_hex( |
| 1202 | bytes: &[u8], |
| 1203 | sep: OptionalArg<Either<PyStrRef, PyBytesRef>>, |
| 1204 | bytes_per_sep: OptionalArg<isize>, |
| 1205 | vm: &VirtualMachine, |
| 1206 | ) -> PyResult<String> { |
| 1207 | if bytes.is_empty() { |
| 1208 | return Ok("".to_owned()); |
| 1209 | } |
| 1210 | |
| 1211 | if let OptionalArg::Present(sep) = sep { |
| 1212 | let bytes_per_sep = bytes_per_sep.unwrap_or(1); |
| 1213 | if bytes_per_sep == 0 { |
| 1214 | return Ok(hex_impl_no_sep(bytes)); |
| 1215 | } |
| 1216 | |
| 1217 | let s_guard; |
| 1218 | let b_guard; |
| 1219 | let sep = match &sep { |
| 1220 | Either::A(s) => { |
| 1221 | s_guard = s.as_wtf8(); |
| 1222 | s_guard.as_bytes() |
| 1223 | } |
| 1224 | Either::B(bytes) => { |
| 1225 | b_guard = bytes.as_bytes(); |
| 1226 | b_guard |
| 1227 | } |
| 1228 | }; |
| 1229 | |
| 1230 | if sep.len() != 1 { |
| 1231 | return Err(vm.new_value_error("sep must be length 1.")); |
| 1232 | } |
| 1233 | let sep = sep[0]; |
| 1234 | if sep > 127 { |
| 1235 | return Err(vm.new_value_error("sep must be ASCII.")); |
| 1236 | } |
| 1237 | |
| 1238 | Ok(hex_impl(bytes, sep, bytes_per_sep)) |
| 1239 | } else { |
| 1240 | Ok(hex_impl_no_sep(bytes)) |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | pub const fn is_py_ascii_whitespace(b: u8) -> bool { |
| 1245 | matches!(b, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' | b'\x0B') |