|
2 | 2 |
|
3 | 3 | //! String representations.
|
4 | 4 |
|
| 5 | +use core::fmt::{self, Write}; |
5 | 6 | use core::ops::{self, Deref, Index};
|
6 | 7 |
|
7 | 8 | use crate::bindings;
|
@@ -209,6 +210,58 @@ impl CStr {
|
209 | 210 | }
|
210 | 211 | }
|
211 | 212 |
|
| 213 | +impl fmt::Display for CStr { |
| 214 | + /// Formats printable ASCII characters, escaping the rest. |
| 215 | + /// |
| 216 | + /// ``` |
| 217 | + /// # use kernel::c_str; |
| 218 | + /// # use kernel::str::CStr; |
| 219 | + /// let penguin = c_str!("🐧"); |
| 220 | + /// assert_eq!(format!("{}", penguin), "\\xf0\\x9f\\x90\\xa7"); |
| 221 | + /// |
| 222 | + /// let ascii = c_str!("so \"cool\""); |
| 223 | + /// assert_eq!(format!("{}", ascii), "so \"cool\""); |
| 224 | + /// ``` |
| 225 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 226 | + for &c in self.as_bytes() { |
| 227 | + if (0x20..0x7f).contains(&c) { |
| 228 | + // Printable character |
| 229 | + f.write_char(c as char)?; |
| 230 | + } else { |
| 231 | + write!(f, "\\x{:02x}", c)?; |
| 232 | + } |
| 233 | + } |
| 234 | + Ok(()) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +impl fmt::Debug for CStr { |
| 239 | + /// Formats printable ASCII characters with a double quote on either end, escaping the rest. |
| 240 | + /// |
| 241 | + /// ``` |
| 242 | + /// # use kernel::c_str; |
| 243 | + /// # use kernel::str::CStr; |
| 244 | + /// let penguin = c_str!("🐧"); |
| 245 | + /// assert_eq!(format!("{:?}", penguin), "\"\\xf0\\x9f\\x90\\xa7\""); |
| 246 | + /// |
| 247 | + /// // embedded double quotes are escaped |
| 248 | + /// let ascii = c_str!("so \"cool\""); |
| 249 | + /// assert_eq!(format!("{:?}", ascii), "\"so \\\"cool\\\"\""); |
| 250 | + /// ``` |
| 251 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 252 | + f.write_str("\"")?; |
| 253 | + for &c in self.as_bytes() { |
| 254 | + match c { |
| 255 | + // Printable characters |
| 256 | + b'\"' => f.write_str("\\\"")?, |
| 257 | + 0x20..=0x7e => f.write_char(c as char)?, |
| 258 | + _ => write!(f, "\\x{:02x}", c)?, |
| 259 | + } |
| 260 | + } |
| 261 | + f.write_str("\"") |
| 262 | + } |
| 263 | +} |
| 264 | + |
212 | 265 | impl AsRef<BStr> for CStr {
|
213 | 266 | #[inline]
|
214 | 267 | fn as_ref(&self) -> &BStr {
|
|
0 commit comments