From ee7504f5f7d77ebf188cd0f5ee86657ec62d07ba Mon Sep 17 00:00:00 2001 From: modeco80 Date: Thu, 29 Jun 2023 03:12:06 -0400 Subject: [PATCH] jmmt: move make_c_string to util --- crates/jmmt/src/format/mod.rs | 23 ----------------------- crates/jmmt/src/format/package_toc.rs | 2 +- crates/jmmt/src/util/mod.rs | 24 ++++++++++++++++++++++++ 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/crates/jmmt/src/format/mod.rs b/crates/jmmt/src/format/mod.rs index cd59c12..8efe658 100644 --- a/crates/jmmt/src/format/mod.rs +++ b/crates/jmmt/src/format/mod.rs @@ -12,26 +12,3 @@ pub trait Validatable { /// Returns true if the object is valid, false otherwise. 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 { - 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, - } -} diff --git a/crates/jmmt/src/format/package_toc.rs b/crates/jmmt/src/format/package_toc.rs index 61b8321..1ac81b5 100644 --- a/crates/jmmt/src/format/package_toc.rs +++ b/crates/jmmt/src/format/package_toc.rs @@ -1,6 +1,6 @@ //! Package.toc structures -use super::make_c_string; +use crate::util::make_c_string; /// An entry inside the `package.toc` file #[derive(Debug)] diff --git a/crates/jmmt/src/util/mod.rs b/crates/jmmt/src/util/mod.rs index e0c52d0..380aeda 100644 --- a/crates/jmmt/src/util/mod.rs +++ b/crates/jmmt/src/util/mod.rs @@ -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 { + 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, + } +}