From 5e39d6876b806604090b892369cba9892c7dac25 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 12 Jan 2020 20:04:31 +0100 Subject: Correctly interpret and change values; does not write yet --- src/encoding.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/encoding.rs') diff --git a/src/encoding.rs b/src/encoding.rs index c117cd0..fc0c335 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -1,4 +1,6 @@ use std::convert::TryFrom; +use std::io; +use crate::error::IntoIOError; use crate::util::read_big_endian; macro_rules! guard { @@ -42,10 +44,13 @@ pub fn from_ucs_2_bom(bytes: &[u8]) -> Option { Some(res) } -pub fn from_utf8_mistaken_as_latin1(latin1: &str) -> Option { - guard!(latin1.chars().all(|c| (c as usize) < 256)); - match std::str::from_utf8(latin1.as_bytes()) { - Ok(res) => Some(res.to_string()), - Err(_) => None, - } +pub fn from_utf8_mistaken_as_latin1(latin1: &str) -> io::Result { + latin1 + .chars() + .map(|c| u8::try_from(u32::from(c))) + .collect::, _>>() + .map_err(|e| e.ioerr()) + .and_then(|v| std::str::from_utf8(&v) + .map(|s| s.to_string()) + .map_err(|e| e.ioerr())) } -- cgit v1.2.3