@@ -51,25 +51,14 @@ pub fn read_stdin() -> Result<Vec<u8>, std::io::Error> {
5151/// # } _doctest_main_src_common_rs_48_0(); }
5252/// ```
5353pub fn read_file ( file : impl Into < String > ) -> Result < Vec < u8 > , std:: io:: Error > {
54- let mut file_data = Vec :: new ( ) ;
5554 let file_path = file. into ( ) ;
5655
57- match File :: open ( & file_path) {
58- Err ( e) => {
59- error ! ( "Failed to open file {file_path}: {e}" ) ;
60- Err ( e)
61- }
62- Ok ( mut fp) => match fp. read_to_end ( & mut file_data) {
63- Err ( e) => {
64- error ! ( "Failed to read file {file_path} into memory: {e}" ) ;
65- Err ( e)
66- }
67- Ok ( file_size) => {
68- debug ! ( "Loaded {file_size} bytes from {file_path}" ) ;
69- Ok ( file_data)
70- }
71- } ,
72- }
56+ let file_data = std:: fs:: read ( & file_path)
57+ . inspect_err ( |e| error ! ( "Failed to read file {file_path} into memory: {e}" ) ) ?;
58+
59+ let file_size = file_data. len ( ) ;
60+ debug ! ( "Loaded {file_size} bytes from {file_path}" ) ;
61+ Ok ( file_data)
7362}
7463
7564/// Calculates the CRC32 checksum of the given data.
@@ -112,22 +101,6 @@ pub fn epoch_to_string(epoch_timestamp: u32) -> String {
112101 }
113102}
114103
115- /// Get a C-style NULL-terminated string from the provided list of u8 bytes.
116- /// Return value does not include the terminating NULL byte.
117- fn get_cstring_bytes ( raw_data : & [ u8 ] ) -> Vec < u8 > {
118- let mut cstring: Vec < u8 > = vec ! [ ] ;
119-
120- for raw_byte in raw_data {
121- if * raw_byte == 0 {
122- break ;
123- } else {
124- cstring. push ( * raw_byte) ;
125- }
126- }
127-
128- cstring
129- }
130-
131104/// Get a C-style NULL-terminated string from the provided array of u8 bytes.
132105///
133106/// ## Example
@@ -142,14 +115,9 @@ fn get_cstring_bytes(raw_data: &[u8]) -> Vec<u8> {
142115/// assert_eq!(string, "this_is_a_c_string");
143116/// ```
144117pub fn get_cstring ( raw_data : & [ u8 ] ) -> String {
145- let raw_string = get_cstring_bytes ( raw_data) ;
146-
147- let string: String = match String :: from_utf8 ( raw_string) {
148- Err ( _) => "" . to_string ( ) ,
149- Ok ( s) => s. clone ( ) ,
150- } ;
151-
152- string
118+ let first_zero = raw_data. iter ( ) . position ( |& r| r == 0 ) . unwrap_or ( raw_data. len ( ) ) ;
119+ let raw_bytes = & raw_data[ ..first_zero] ;
120+ String :: from_utf8_lossy ( raw_bytes) . into_owned ( )
153121}
154122
155123/// Returns true if the provided byte is an ASCII number
@@ -163,10 +131,7 @@ pub fn get_cstring(raw_data: &[u8]) -> String {
163131/// assert!(!is_ascii_number(0xFE));
164132/// ```
165133pub fn is_ascii_number ( b : u8 ) -> bool {
166- const ZERO : u8 = 48 ;
167- const NINE : u8 = 57 ;
168-
169- ( ZERO ..=NINE ) . contains ( & b)
134+ b. is_ascii_digit ( )
170135}
171136
172137/// Returns true if the provided byte is a printable ASCII character
@@ -213,10 +178,8 @@ pub fn is_offset_safe(
213178 last_offset : Option < usize > ,
214179) -> bool {
215180 // If a previous file offset was specified, ensure that it is less than the next file offset
216- if let Some ( previous_offset) = last_offset {
217- if previous_offset >= next_offset {
218- return false ;
219- }
181+ if last_offset. is_some_and ( |b| b >= next_offset) {
182+ return false ;
220183 }
221184
222185 // Ensure that the next file offset is within the bounds of available file data
0 commit comments