1 // SPDX-License-Identifier: GPL-2.0
3 //! String representations.
5 use core::fmt::{self, Write};
6 use core::ops::{self, Deref, Index};
10 error::{code::*, Error},
13 /// Byte string without UTF-8 validity guarantee.
15 /// `BStr` is simply an alias to `[u8]`, but has a more evident semantical meaning.
18 /// Creates a new [`BStr`] from a string literal.
20 /// `b_str!` converts the supplied string literal to byte string, so non-ASCII
21 /// characters can be included.
26 /// # use kernel::b_str;
27 /// # use kernel::str::BStr;
28 /// const MY_BSTR: &BStr = b_str!("My awesome BStr!");
33 const S: &'static str = $str;
34 const C: &'static $crate::str::BStr = S.as_bytes();
39 /// Possible errors when using conversion functions in [`CStr`].
40 #[derive(Debug, Clone, Copy)]
41 pub enum CStrConvertError {
42 /// Supplied bytes contain an interior `NUL`.
45 /// Supplied bytes are not terminated by `NUL`.
49 impl From<CStrConvertError> for Error {
51 fn from(_: CStrConvertError) -> Error {
56 /// A string that is guaranteed to have exactly one `NUL` byte, which is at the
59 /// Used for interoperability with kernel APIs that take C strings.
61 pub struct CStr([u8]);
64 /// Returns the length of this string excluding `NUL`.
66 pub const fn len(&self) -> usize {
67 self.len_with_nul() - 1
70 /// Returns the length of this string with `NUL`.
72 pub const fn len_with_nul(&self) -> usize {
73 // SAFETY: This is one of the invariant of `CStr`.
74 // We add a `unreachable_unchecked` here to hint the optimizer that
75 // the value returned from this function is non-zero.
76 if self.0.is_empty() {
77 unsafe { core::hint::unreachable_unchecked() };
82 /// Returns `true` if the string only includes `NUL`.
84 pub const fn is_empty(&self) -> bool {
88 /// Wraps a raw C string pointer.
92 /// `ptr` must be a valid pointer to a `NUL`-terminated C string, and it must
93 /// last at least `'a`. When `CStr` is alive, the memory pointed by `ptr`
94 /// must not be mutated.
96 pub unsafe fn from_char_ptr<'a>(ptr: *const core::ffi::c_char) -> &'a Self {
97 // SAFETY: The safety precondition guarantees `ptr` is a valid pointer
98 // to a `NUL`-terminated C string.
99 let len = unsafe { bindings::strlen(ptr) } + 1;
100 // SAFETY: Lifetime guaranteed by the safety precondition.
101 let bytes = unsafe { core::slice::from_raw_parts(ptr as _, len as _) };
102 // SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`.
103 // As we have added 1 to `len`, the last byte is known to be `NUL`.
104 unsafe { Self::from_bytes_with_nul_unchecked(bytes) }
107 /// Creates a [`CStr`] from a `[u8]`.
109 /// The provided slice must be `NUL`-terminated, does not contain any
110 /// interior `NUL` bytes.
111 pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError> {
112 if bytes.is_empty() {
113 return Err(CStrConvertError::NotNulTerminated);
115 if bytes[bytes.len() - 1] != 0 {
116 return Err(CStrConvertError::NotNulTerminated);
119 // `i + 1 < bytes.len()` allows LLVM to optimize away bounds checking,
120 // while it couldn't optimize away bounds checks for `i < bytes.len() - 1`.
121 while i + 1 < bytes.len() {
123 return Err(CStrConvertError::InteriorNul);
127 // SAFETY: We just checked that all properties hold.
128 Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
131 /// Creates a [`CStr`] from a `[u8]` without performing any additional
136 /// `bytes` *must* end with a `NUL` byte, and should only have a single
137 /// `NUL` byte (or the string will be truncated).
139 pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
140 // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
141 unsafe { core::mem::transmute(bytes) }
144 /// Returns a C pointer to the string.
146 pub const fn as_char_ptr(&self) -> *const core::ffi::c_char {
150 /// Convert the string to a byte slice without the trailing 0 byte.
152 pub fn as_bytes(&self) -> &[u8] {
153 &self.0[..self.len()]
156 /// Convert the string to a byte slice containing the trailing 0 byte.
158 pub const fn as_bytes_with_nul(&self) -> &[u8] {
162 /// Yields a [`&str`] slice if the [`CStr`] contains valid UTF-8.
164 /// If the contents of the [`CStr`] are valid UTF-8 data, this
165 /// function will return the corresponding [`&str`] slice. Otherwise,
166 /// it will return an error with details of where UTF-8 validation failed.
171 /// # use kernel::str::CStr;
172 /// let cstr = CStr::from_bytes_with_nul(b"foo\0").unwrap();
173 /// assert_eq!(cstr.to_str(), Ok("foo"));
176 pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
177 core::str::from_utf8(self.as_bytes())
180 /// Unsafely convert this [`CStr`] into a [`&str`], without checking for
185 /// The contents must be valid UTF-8.
190 /// # use kernel::c_str;
191 /// # use kernel::str::CStr;
192 /// // SAFETY: String literals are guaranteed to be valid UTF-8
193 /// // by the Rust compiler.
194 /// let bar = c_str!("ツ");
195 /// assert_eq!(unsafe { bar.as_str_unchecked() }, "ツ");
198 pub unsafe fn as_str_unchecked(&self) -> &str {
199 unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
203 impl fmt::Display for CStr {
204 /// Formats printable ASCII characters, escaping the rest.
207 /// # use kernel::c_str;
208 /// # use kernel::str::CStr;
209 /// # use kernel::str::CString;
210 /// let penguin = c_str!("🐧");
211 /// let s = CString::try_from_fmt(fmt!("{}", penguin)).unwrap();
212 /// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
214 /// let ascii = c_str!("so \"cool\"");
215 /// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap();
216 /// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 for &c in self.as_bytes() {
220 if (0x20..0x7f).contains(&c) {
221 // Printable character.
222 f.write_char(c as char)?;
224 write!(f, "\\x{:02x}", c)?;
231 impl fmt::Debug for CStr {
232 /// Formats printable ASCII characters with a double quote on either end, escaping the rest.
235 /// # use kernel::c_str;
236 /// # use kernel::str::CStr;
237 /// # use kernel::str::CString;
238 /// let penguin = c_str!("🐧");
239 /// let s = CString::try_from_fmt(fmt!("{:?}", penguin)).unwrap();
240 /// assert_eq!(s.as_bytes_with_nul(), "\"\\xf0\\x9f\\x90\\xa7\"\0".as_bytes());
242 /// // Embedded double quotes are escaped.
243 /// let ascii = c_str!("so \"cool\"");
244 /// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap();
245 /// assert_eq!(s.as_bytes_with_nul(), "\"so \\\"cool\\\"\"\0".as_bytes());
247 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249 for &c in self.as_bytes() {
251 // Printable characters.
252 b'\"' => f.write_str("\\\"")?,
253 0x20..=0x7e => f.write_char(c as char)?,
254 _ => write!(f, "\\x{:02x}", c)?,
261 impl AsRef<BStr> for CStr {
263 fn as_ref(&self) -> &BStr {
268 impl Deref for CStr {
272 fn deref(&self) -> &Self::Target {
277 impl Index<ops::RangeFrom<usize>> for CStr {
281 fn index(&self, index: ops::RangeFrom<usize>) -> &Self::Output {
282 // Delegate bounds checking to slice.
283 // Assign to _ to mute clippy's unnecessary operation warning.
284 let _ = &self.as_bytes()[index.start..];
285 // SAFETY: We just checked the bounds.
286 unsafe { Self::from_bytes_with_nul_unchecked(&self.0[index.start..]) }
290 impl Index<ops::RangeFull> for CStr {
294 fn index(&self, _index: ops::RangeFull) -> &Self::Output {
302 // Marker trait for index types that can be forward to `BStr`.
303 pub trait CStrIndex {}
305 impl CStrIndex for usize {}
306 impl CStrIndex for ops::Range<usize> {}
307 impl CStrIndex for ops::RangeInclusive<usize> {}
308 impl CStrIndex for ops::RangeToInclusive<usize> {}
311 impl<Idx> Index<Idx> for CStr
313 Idx: private::CStrIndex,
316 type Output = <BStr as Index<Idx>>::Output;
319 fn index(&self, index: Idx) -> &Self::Output {
320 &self.as_bytes()[index]
324 /// Creates a new [`CStr`] from a string literal.
326 /// The string literal should not contain any `NUL` bytes.
331 /// # use kernel::c_str;
332 /// # use kernel::str::CStr;
333 /// const MY_CSTR: &CStr = c_str!("My awesome CStr!");
338 const S: &str = concat!($str, "\0");
339 const C: &$crate::str::CStr = match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) {
341 Err(_) => panic!("string contains interior NUL"),
352 fn test_cstr_to_str() {
353 let good_bytes = b"\xf0\x9f\xa6\x80\0";
354 let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
355 let checked_str = checked_cstr.to_str().unwrap();
356 assert_eq!(checked_str, "🦀");
361 fn test_cstr_to_str_panic() {
362 let bad_bytes = b"\xc3\x28\0";
363 let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap();
364 checked_cstr.to_str().unwrap();
368 fn test_cstr_as_str_unchecked() {
369 let good_bytes = b"\xf0\x9f\x90\xA7\0";
370 let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
371 let unchecked_str = unsafe { checked_cstr.as_str_unchecked() };
372 assert_eq!(unchecked_str, "🐧");
376 /// Allows formatting of [`fmt::Arguments`] into a raw buffer.
378 /// It does not fail if callers write past the end of the buffer so that they can calculate the
379 /// size required to fit everything.
383 /// The memory region between `pos` (inclusive) and `end` (exclusive) is valid for writes if `pos`
384 /// is less than `end`.
385 pub(crate) struct RawFormatter {
386 // Use `usize` to use `saturating_*` functions.
394 /// Creates a new instance of [`RawFormatter`] with the given buffer pointers.
398 /// If `pos` is less than `end`, then the region between `pos` (inclusive) and `end`
399 /// (exclusive) must be valid for writes for the lifetime of the returned [`RawFormatter`].
400 pub(crate) unsafe fn from_ptrs(pos: *mut u8, end: *mut u8) -> Self {
401 // INVARIANT: The safety requierments guarantee the type invariants.
409 /// Creates a new instance of [`RawFormatter`] with the given buffer.
413 /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
414 /// for the lifetime of the returned [`RawFormatter`].
415 pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
416 let pos = buf as usize;
417 // INVARIANT: We ensure that `end` is never less then `buf`, and the safety requirements
418 // guarantees that the memory region is valid for writes.
422 end: pos.saturating_add(len),
426 /// Returns the current insert position.
428 /// N.B. It may point to invalid memory.
429 pub(crate) fn pos(&self) -> *mut u8 {
434 impl fmt::Write for RawFormatter {
435 fn write_str(&mut self, s: &str) -> fmt::Result {
436 // `pos` value after writing `len` bytes. This does not have to be bounded by `end`, but we
437 // don't want it to wrap around to 0.
438 let pos_new = self.pos.saturating_add(s.len());
440 // Amount that we can copy. `saturating_sub` ensures we get 0 if `pos` goes past `end`.
441 let len_to_copy = core::cmp::min(pos_new, self.end).saturating_sub(self.pos);
444 // SAFETY: If `len_to_copy` is non-zero, then we know `pos` has not gone past `end`
445 // yet, so it is valid for write per the type invariants.
447 core::ptr::copy_nonoverlapping(
448 s.as_bytes().as_ptr(),
460 /// Allows formatting of [`fmt::Arguments`] into a raw buffer.
462 /// Fails if callers attempt to write more than will fit in the buffer.
463 pub(crate) struct Formatter(RawFormatter);
466 /// Creates a new instance of [`Formatter`] with the given buffer.
470 /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
471 /// for the lifetime of the returned [`Formatter`].
473 pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
474 // SAFETY: The safety requirements of this function satisfy those of the callee.
475 Self(unsafe { RawFormatter::from_buffer(buf, len) })
479 impl Deref for Formatter {
480 type Target = RawFormatter;
482 fn deref(&self) -> &Self::Target {
487 impl fmt::Write for Formatter {
488 fn write_str(&mut self, s: &str) -> fmt::Result {
489 self.0.write_str(s)?;
491 // Fail the request if we go past the end of the buffer.
492 if self.0.pos > self.0.end {