Skip to content

Commit

Permalink
Fix incorrect reading of files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuuuube committed Mar 24, 2024
1 parent 2e2b9aa commit 5dc6ba6
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ fn parse_utf8(file_buffer: &Vec<u8>) -> (String, usize) {
Ok(ok) => { ok.to_string() },
Err(err) => {
seek_position = err.valid_up_to();
std::str::from_utf8(&file_buffer[0..=seek_position]).unwrap_or({
if seek_position == 0 {
println!("Invalid UTF-8 detected, output may not match input: {}", err);
seek_position = 0;
unsafe { std::str::from_utf8_unchecked(&file_buffer) }
}).to_string()
seek_position = file_buffer.len();
unsafe { std::str::from_utf8_unchecked(&file_buffer) }.to_string()
} else {
//should never panic as long as seek_position = err.valid_up_to()
std::str::from_utf8(&file_buffer[0..seek_position]).unwrap().to_string()
}
},
};
(utf8_string, seek_position)
Expand Down

0 comments on commit 5dc6ba6

Please sign in to comment.