| 1 |
use std::borrow::{Borrow, Cow}; |
| 2 |
use std::ops::Deref; |
| 3 |
use std::str::Utf8Error; |
| 4 |
|
| 5 |
/// OsStr, but specifically for Linux (since we aren't always processing native dumps). |
| 6 |
#[derive(Debug, PartialOrd, Ord, Eq, PartialEq)] |
| 7 |
pub struct LinuxOsStr([u8]); |
| 8 |
|
| 9 |
/// OsString, but specifically for Linux (since we aren't always processing native dumps). |
| 10 |
#[derive(Default, Debug, PartialOrd, Ord, Eq, PartialEq, Clone)] |
| 11 |
pub struct LinuxOsString(Vec<u8>); |
| 12 |
|
| 13 |
impl LinuxOsStr { |
| 14 |
pub fn new() -> &'static Self { |
| 15 |
Self::from_bytes(b"") |
| 16 |
} |
| 17 |
|
| 18 |
pub fn from_bytes(inner: &[u8]) -> &Self { |
| 19 |
// This is the idiom std uses for creating a type that wraps a slice. |
| 20 |
// Yes, there really isn't a way to do this without unsafe. No, it's |
| 21 |
// not at all a safety concern. |
| 22 |
unsafe { &*(inner as *const [u8] as *const LinuxOsStr) } |
| 23 |
} |
| 24 |
|
| 25 |
pub fn as_bytes(&self) -> &[u8] { |
| 26 |
&self.0 |
| 27 |
} |
| 28 |
|
| 29 |
/// Tries to interpret the LinuxOsStr as a utf8 str. |
| 30 |
/// |
| 31 |
/// While linux OsStrs are "arbitrary bytes" in general, there are often |
| 32 |
/// parts that are known to be utf8 (ascii even). |
| 33 |
/// |
| 34 |
/// For instance, when parsing /proc/self/mem, most of the line is ascii |
| 35 |
/// like "r-xp" or "1a23-4fe2". However the "path" at the end of each line |
| 36 |
/// is a true LinuxOsStr and may not be proper utf8. |
| 37 |
pub fn to_str(&self) -> Result<&str, Utf8Error> { |
| 38 |
std::str::from_utf8(self) |
| 39 |
} |
| 40 |
|
| 41 |
/// Converts to a utf8 string lossily (uses the usual std lossy algorithm). |
| 42 |
pub fn to_string_lossy(&self) -> Cow<str> { |
| 43 |
// Ok so this is the end of the line for dancing around and acting |
| 44 |
// like we can just be fine with Linux OS strings being arbitrary bags |
| 45 |
// of bytes. We need some way to print this value in a reasonable way, |
| 46 |
// and the best precedent I can find for that is std::Path::display. |
| 47 |
// This wraps the a Path (which is just an OsStr) and provides a |
| 48 |
// Display impl. |
| 49 |
// |
| 50 |
// What does this Display impl do..? |
| 51 |
// |
| 52 |
// It just calls from_utf8_lossy. |
| 53 |
// |
| 54 |
// Whelp. Ok. |
| 55 |
// |
| 56 |
// (Strictly speaking it wraps it up in the internal/unstable |
| 57 |
// Utf8Lossy iterator so it avoids the allocation, but we don't |
| 58 |
// have that luxury, so we might as well make the allocation/conversion |
| 59 |
// explicit.) |
| 60 |
String::from_utf8_lossy(self.as_bytes()) |
| 61 |
} |
| 62 |
|
| 63 |
// ~Copies of a bunch of string APIs since [u8] doesn't have them (reasonably) |
| 64 |
pub fn split_once(&self, separator: u8) -> Option<(&LinuxOsStr, &LinuxOsStr)> { |
| 65 |
self.iter().position(|&b| b == separator).map(|idx| { |
| 66 |
( |
| 67 |
Self::from_bytes(&self[..idx]), |
| 68 |
Self::from_bytes(&self[idx + 1..]), |
| 69 |
) |
| 70 |
}) |
| 71 |
} |
| 72 |
pub fn rsplit_once(&self, separator: u8) -> Option<(&LinuxOsStr, &LinuxOsStr)> { |
| 73 |
self.iter().rposition(|&b| b == separator).map(|idx| { |
| 74 |
( |
| 75 |
Self::from_bytes(&self[..idx]), |
| 76 |
Self::from_bytes(&self[idx + 1..]), |
| 77 |
) |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
pub fn split(&self, separator: u8) -> impl Iterator<Item = &LinuxOsStr> { |
| 82 |
self.as_bytes() |
| 83 |
.split(move |&b| b == separator) |
| 84 |
.map(LinuxOsStr::from_bytes) |
| 85 |
} |
| 86 |
|
| 87 |
pub fn split_ascii_whitespace(&self) -> impl Iterator<Item = &LinuxOsStr> { |
| 88 |
// Quick and dirty impl: just split on every individual whitespace |
| 89 |
// char but discard all the empty substrings. |
| 90 |
self.as_bytes() |
| 91 |
.split(|b| b.is_ascii_whitespace()) |
| 92 |
.filter(|slice| !slice.is_empty()) |
| 93 |
.map(LinuxOsStr::from_bytes) |
| 94 |
} |
| 95 |
|
| 96 |
pub fn lines(&self) -> impl Iterator<Item = &LinuxOsStr> { |
| 97 |
// Intentionally doesn't mess around with stuff like \r |
| 98 |
// since we're processing files generated by the OS, but maybe |
| 99 |
// this will be a problem later? |
| 100 |
self.split(b'\n') |
| 101 |
} |
| 102 |
|
| 103 |
pub fn trim_ascii_whitespace(&self) -> &LinuxOsStr { |
| 104 |
let input = self.as_bytes(); |
| 105 |
|
| 106 |
let mut first = None; |
| 107 |
let mut last = None; |
| 108 |
|
| 109 |
// Find first non-whitespace index |
| 110 |
for (i, &c) in input.iter().enumerate() { |
| 111 |
if !c.is_ascii_whitespace() { |
| 112 |
first = Some(i); |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Find last non-whitespace index |
| 118 |
for (i, &c) in input.iter().enumerate().rev() { |
| 119 |
if !c.is_ascii_whitespace() { |
| 120 |
last = Some(i); |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if let (Some(first), Some(last)) = (first, last) { |
| 126 |
Self::from_bytes(&input[first..=last]) |
| 127 |
} else { |
| 128 |
// string was entirely whitespace, return an empty string starting |
| 129 |
// at its position (so that it's still strictly a substring). |
| 130 |
Self::from_bytes(&input[0..0]) |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
impl LinuxOsString { |
| 136 |
/// Create a new LinuxOsString from an array of bytes. |
| 137 |
pub fn from_vec(vec: Vec<u8>) -> Self { |
| 138 |
Self(vec) |
| 139 |
} |
| 140 |
|
| 141 |
pub fn new() -> Self { |
| 142 |
Self(Vec::new()) |
| 143 |
} |
| 144 |
|
| 145 |
pub fn as_os_str(&self) -> &LinuxOsStr { |
| 146 |
self |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl Borrow<LinuxOsStr> for LinuxOsString { |
| 151 |
fn borrow(&self) -> &LinuxOsStr { |
| 152 |
LinuxOsStr::from_bytes(&self.0) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl ToOwned for LinuxOsStr { |
| 157 |
type Owned = LinuxOsString; |
| 158 |
|
| 159 |
fn to_owned(&self) -> LinuxOsString { |
| 160 |
LinuxOsString::from_vec(self.0.to_owned()) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl Deref for LinuxOsString { |
| 165 |
type Target = LinuxOsStr; |
| 166 |
|
| 167 |
fn deref(&self) -> &LinuxOsStr { |
| 168 |
LinuxOsStr::from_bytes(&self.0) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl Deref for LinuxOsStr { |
| 173 |
type Target = [u8]; |
| 174 |
|
| 175 |
fn deref(&self) -> &[u8] { |
| 176 |
&self.0 |
| 177 |
} |
| 178 |
} |