1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Easy way to read UTF16 encoded files

use std::io::Read;

/// Decodes a Reader with UTF16 data to a String
/// 
/// # Examples
/// 
/// ```
/// use std::fs::File;
/// use std::io::BufReader;
/// 
/// let f = File::open("test_files/test_be.txt").unwrap();
/// let r = BufReader::new(f);
/// let s = utf16_reader::read_to_string(r);
/// 
/// println!("{}", s);
/// ```
pub fn read_to_string<R: Read>(source: R) -> String {
    let mut bytes = source.bytes();
    
    let x: Vec<u8> = bytes.by_ref().take(2).map(|x|x.unwrap()).collect();
    
    let mut i = true;
    if ((x[0] as u16) << 8) + x[1] as u16 == 0xFEFF { i = !i };
    
    let (hs, ls): (Vec<u8>, Vec<u8>) = bytes.map(|x| x.unwrap()).partition(|_| {i=!i; i});
    let c: Vec<u16> = ls.iter().zip(hs.iter()).map(|(ls, hs)| ((*hs as u16)<<8) + *ls as u16).collect();

    String::from_utf16(&c).unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;

    #[test]
    fn read_be_test_file() {
        let f = File::open("test_files/test_be.txt").unwrap();
        let s = read_to_string(f);
        assert_eq!("This is a test", s);
    }

    #[test]
    fn read_le_test_file() {
        let f = File::open("test_files/test_le.txt").unwrap();
        let s = read_to_string(f);
        assert_eq!("This is a test", s);
    }
}