Skip to main content

utf16_reader/
lib.rs

1//! Easy way to read UTF16 encoded files
2
3use std::io::Read;
4
5/// Decodes a Reader with UTF16 data to a String
6/// 
7/// # Examples
8/// 
9/// ```
10/// use std::fs::File;
11/// use std::io::BufReader;
12/// 
13/// let f = File::open("test_files/test_be.txt").unwrap();
14/// let r = BufReader::new(f);
15/// let s = utf16_reader::read_to_string(r);
16/// 
17/// println!("{}", s);
18/// ```
19pub fn read_to_string<R: Read>(source: R) -> String {
20    let mut bytes = source.bytes();
21    
22    let x: Vec<u8> = bytes.by_ref().take(2).map(|x|x.unwrap()).collect();
23    
24    let mut i = true;
25    if ((x[0] as u16) << 8) + x[1] as u16 == 0xFEFF { i = !i };
26    
27    let (hs, ls): (Vec<u8>, Vec<u8>) = bytes.map(|x| x.unwrap()).partition(|_| {i=!i; i});
28    let c: Vec<u16> = ls.iter().zip(hs.iter()).map(|(ls, hs)| ((*hs as u16)<<8) + *ls as u16).collect();
29
30    String::from_utf16(&c).unwrap()
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use std::fs::File;
37
38    #[test]
39    fn read_be_test_file() {
40        let f = File::open("test_files/test_be.txt").unwrap();
41        let s = read_to_string(f);
42        assert_eq!("This is a test", s);
43    }
44
45    #[test]
46    fn read_le_test_file() {
47        let f = File::open("test_files/test_le.txt").unwrap();
48        let s = read_to_string(f);
49        assert_eq!("This is a test", s);
50    }
51}