1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::borrow::Cow;

use super::stream::*;
use encoding_rs::{mem::decode_latin1, UTF_16LE};
use nom::{bytes::complete::take, combinator::map, error::ParseError, IResult};

fn decode_utf16_le(bytes: &[u8]) -> Cow<'_, str> {
    UTF_16LE.decode(bytes).0
}

pub fn parse_string<'a, E: ParseError<&'a [u8]>>(
    length_and_compression: CountOfBytesWithCompressionFlag,
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], String, E> {
    map(
        map(
            take(length_and_compression.len()),
            match length_and_compression.compressed() {
                true => decode_latin1, // Isomorphic Decode
                false => decode_utf16_le,
            },
        ),
        Cow::into_owned,
    )
}