|
1 | | -use super::chars::{Char16, Char8, NUL_16, NUL_8}; |
| 1 | +use super::chars::{Char16, NUL_16}; |
2 | 2 | use core::fmt; |
3 | 3 | use core::iter::Iterator; |
4 | 4 | use core::marker::PhantomData; |
@@ -52,70 +52,6 @@ pub enum FromStrWithBufError { |
52 | 52 | BufferTooSmall, |
53 | 53 | } |
54 | 54 |
|
55 | | -/// A Latin-1 null-terminated string |
56 | | -/// |
57 | | -/// This type is largely inspired by `std::ffi::CStr`, see the documentation of |
58 | | -/// `CStr` for more details on its semantics. |
59 | | -#[repr(transparent)] |
60 | | -pub struct CStr8([Char8]); |
61 | | - |
62 | | -impl CStr8 { |
63 | | - /// Wraps a raw UEFI string with a safe C string wrapper |
64 | | - /// |
65 | | - /// # Safety |
66 | | - /// |
67 | | - /// The function will start accessing memory from `ptr` until the first |
68 | | - /// null byte. It's the callers responsability to ensure `ptr` points to |
69 | | - /// a valid string, in accessible memory. |
70 | | - pub unsafe fn from_ptr<'ptr>(ptr: *const Char8) -> &'ptr Self { |
71 | | - let mut len = 0; |
72 | | - while *ptr.add(len) != NUL_8 { |
73 | | - len += 1 |
74 | | - } |
75 | | - let ptr = ptr.cast::<u8>(); |
76 | | - Self::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1)) |
77 | | - } |
78 | | - |
79 | | - /// Creates a C string wrapper from bytes |
80 | | - pub fn from_bytes_with_nul(chars: &[u8]) -> Result<&Self, FromSliceWithNulError> { |
81 | | - let nul_pos = chars.iter().position(|&c| c == 0); |
82 | | - if let Some(nul_pos) = nul_pos { |
83 | | - if nul_pos + 1 != chars.len() { |
84 | | - return Err(FromSliceWithNulError::InteriorNul(nul_pos)); |
85 | | - } |
86 | | - Ok(unsafe { Self::from_bytes_with_nul_unchecked(chars) }) |
87 | | - } else { |
88 | | - Err(FromSliceWithNulError::NotNulTerminated) |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - /// Unsafely creates a C string wrapper from bytes |
93 | | - /// |
94 | | - /// # Safety |
95 | | - /// |
96 | | - /// It's the callers responsability to ensure chars is a valid Latin-1 |
97 | | - /// null-terminated string, with no interior null bytes. |
98 | | - pub unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self { |
99 | | - &*(chars as *const [u8] as *const Self) |
100 | | - } |
101 | | - |
102 | | - /// Returns the inner pointer to this C string |
103 | | - pub fn as_ptr(&self) -> *const Char8 { |
104 | | - self.0.as_ptr() |
105 | | - } |
106 | | - |
107 | | - /// Converts this C string to a slice of bytes |
108 | | - pub fn to_bytes(&self) -> &[u8] { |
109 | | - let chars = self.to_bytes_with_nul(); |
110 | | - &chars[..chars.len() - 1] |
111 | | - } |
112 | | - |
113 | | - /// Converts this C string to a slice of bytes containing the trailing 0 char |
114 | | - pub fn to_bytes_with_nul(&self) -> &[u8] { |
115 | | - unsafe { &*(&self.0 as *const [Char8] as *const [u8]) } |
116 | | - } |
117 | | -} |
118 | | - |
119 | 55 | /// An UCS-2 null-terminated string |
120 | 56 | /// |
121 | 57 | /// This type is largely inspired by `std::ffi::CStr`, see the documentation of |
|
0 commit comments