jmmt: move make_c_string to util
This commit is contained in:
parent
7e97ccaf48
commit
ee7504f5f7
|
@ -12,26 +12,3 @@ pub trait Validatable {
|
||||||
/// Returns true if the object is valid, false otherwise.
|
/// Returns true if the object is valid, false otherwise.
|
||||||
fn valid(&self) -> bool;
|
fn valid(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make a Rust [String] from a byte slice that came from a C string/structure.
|
|
||||||
///
|
|
||||||
/// # Usage
|
|
||||||
///
|
|
||||||
/// The byte slice has to be a valid UTF-8 string.
|
|
||||||
/// (Note that in most cases, ASCII strings are valid UTF-8, so this isn't something you'll particularly
|
|
||||||
/// have to worry about).
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function does not directly make use of any unsafe Rust code.
|
|
||||||
pub fn make_c_string(bytes: &[u8]) -> Option<String> {
|
|
||||||
let bytes_without_null = match bytes.iter().position(|&b| b == 0) {
|
|
||||||
Some(ix) => &bytes[..ix],
|
|
||||||
None => bytes,
|
|
||||||
};
|
|
||||||
|
|
||||||
match std::str::from_utf8(bytes_without_null).ok() {
|
|
||||||
Some(string) => Some(String::from(string)),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! Package.toc structures
|
//! Package.toc structures
|
||||||
|
|
||||||
use super::make_c_string;
|
use crate::util::make_c_string;
|
||||||
|
|
||||||
/// An entry inside the `package.toc` file
|
/// An entry inside the `package.toc` file
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
@ -31,3 +31,27 @@ impl Ps2Rgba {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Make a Rust [String] from a byte slice that came from a C string/structure.
|
||||||
|
///
|
||||||
|
/// # Usage
|
||||||
|
///
|
||||||
|
/// The byte slice has to be a valid UTF-8 string.
|
||||||
|
/// (Note that in most cases, ASCII strings are valid UTF-8, so this isn't something you'll particularly
|
||||||
|
/// have to worry about).
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function does not directly make use of any unsafe Rust code.
|
||||||
|
pub fn make_c_string(bytes: &[u8]) -> Option<String> {
|
||||||
|
let bytes_without_null = match bytes.iter().position(|&b| b == 0) {
|
||||||
|
Some(ix) => &bytes[..ix],
|
||||||
|
None => bytes,
|
||||||
|
};
|
||||||
|
|
||||||
|
match std::str::from_utf8(bytes_without_null).ok() {
|
||||||
|
Some(string) => Some(String::from(string)),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue