MCPcopy Index your code
hub / github.com/RustPython/RustPython / bytes_to_hex

Function bytes_to_hex

crates/vm/src/bytes_inner.rs:1201–1242  ·  view source on GitHub ↗
(
    bytes: &[u8],
    sep: OptionalArg<Either<PyStrRef, PyBytesRef>>,
    bytes_per_sep: OptionalArg<isize>,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

1199}
1200
1201pub 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
1244pub const fn is_py_ascii_whitespace(b: u8) -> bool {
1245 matches!(b, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' | b'\x0B')

Callers 2

hexMethod · 0.85
hexMethod · 0.85

Calls 8

hex_impl_no_sepFunction · 0.85
hex_implFunction · 0.85
ErrClass · 0.50
is_emptyMethod · 0.45
to_ownedMethod · 0.45
as_wtf8Method · 0.45
as_bytesMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected