File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -474,6 +474,34 @@ impl CStr {
474474 self . inner . as_ptr ( )
475475 }
476476
477+ /// Returns `true` if `self.to_bytes()` has a length of 0.
478+ ///
479+ /// # Examples
480+ ///
481+ /// ```
482+ /// #![feature(cstr_is_empty)]
483+ ///
484+ /// use std::ffi::CStr;
485+ /// # use std::ffi::FromBytesWithNulError;
486+ ///
487+ /// # fn main() { test().unwrap(); }
488+ /// # fn test() -> Result<(), FromBytesWithNulError> {
489+ /// let cstr = CStr::from_bytes_with_nul(b"foo\0")?;
490+ /// assert!(!cstr.is_empty());
491+ ///
492+ /// let empty_cstr = CStr::from_bytes_with_nul(b"\0")?;
493+ /// assert!(empty_cstr.is_empty());
494+ /// # Ok(())
495+ /// # }
496+ /// ```
497+ #[ inline]
498+ #[ unstable( feature = "cstr_is_empty" , issue = "102444" ) ]
499+ pub const fn is_empty ( & self ) -> bool {
500+ // SAFETY: We know there is at least one byte; for empty strings it
501+ // is the NUL terminator.
502+ ( unsafe { self . inner . get_unchecked ( 0 ) } ) == & 0
503+ }
504+
477505 /// Converts this C string to a byte slice.
478506 ///
479507 /// The returned slice will **not** contain the trailing nul terminator that this C
You can’t perform that action at this time.
0 commit comments