Lines Matching defs:bytes
35 pub const fn from_bytes(bytes: &[u8]) -> &Self {
37 unsafe { &*(core::ptr::from_ref(bytes) as *const BStr) }
195 /// Supplied bytes contain an interior `NUL`.
198 /// Supplied bytes are not terminated by `NUL`.
254 let bytes = unsafe { core::slice::from_raw_parts(ptr.cast(), len) };
255 // SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`.
257 unsafe { Self::from_bytes_with_nul_unchecked(bytes) }
263 /// interior `NUL` bytes.
264 pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError> {
265 if bytes.is_empty() {
268 if bytes[bytes.len() - 1] != 0 {
272 // `i + 1 < bytes.len()` allows LLVM to optimize away bounds checking,
273 // while it couldn't optimize away bounds checks for `i < bytes.len() - 1`.
274 while i + 1 < bytes.len() {
275 if bytes[i] == 0 {
281 Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
289 /// `bytes` *must* end with a `NUL` byte, and should only have a single
292 pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
293 // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
294 unsafe { core::mem::transmute(bytes) }
302 /// `bytes` *must* end with a `NUL` byte, and should only have a single
305 pub unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr {
306 // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
307 unsafe { &mut *(core::ptr::from_mut(bytes) as *mut CStr) }
408 // INVARIANT: This doesn't introduce or remove NUL bytes in the C
423 // INVARIANT: This doesn't introduce or remove NUL bytes in the C
590 /// The string literal should not contain any `NUL` bytes.
677 let mut bytes: [u8; 256] = [0; 256];
678 // fill `bytes` with [1..=255] + [0]
680 bytes[i as usize] = i.wrapping_add(1);
682 let cstr = CStr::from_bytes_with_nul(&bytes)?;
777 /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
797 /// Returns the number of bytes written to the formatter.
805 // `pos` value after writing `len` bytes. This does not have to be bounded by `end`, but we
839 /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
901 let bytes = s.as_bytes();
902 let len = bytes.len();
912 buffer[..len].copy_from_slice(bytes);
992 /// Only considers at most the first two bytes of `bytes`.
993 pub fn kstrtobool_bytes(bytes: &[u8]) -> Result<bool> {
994 // `ktostrbool` only considers the first two bytes of the input.
995 let stack_string = [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or(&0), 0];
1007 /// The string is always `NUL`-terminated and contains no other `NUL` bytes.
1039 // Allocate a vector with the required number of bytes, and write to it.
1046 // SAFETY: The number of bytes that can be written to `f` is bounded by `size`, which is
1050 // Check that there are no `NUL` bytes before the end.
1059 // INVARIANT: We wrote the `NUL` terminator and checked above that no other `NUL` bytes
1070 // other `NUL` bytes exist.
1078 // NUL bytes.