slidetown/parsers/
strings.rs1use encoding_rs::EUC_KR;
2use std::io::SeekFrom;
3
4use binread::{
5 io::{Read, Seek},
6 BinRead, BinResult, ReadOptions,
7};
8
9pub fn parse_int_prefixed_string<R: Read + Seek>(
10 reader: &mut R,
11 options: &ReadOptions,
12 _: (),
13) -> BinResult<String> {
14 let pos = reader.seek(SeekFrom::Current(0))?;
15 let count = u32::read_options(reader, options, ())?;
16
17 String::from_utf8(
18 reader
19 .take(count as u64)
20 .bytes()
21 .filter_map(Result::ok)
22 .collect(),
23 )
24 .map_err(|e| binread::Error::Custom {
25 pos: pos as u64,
26 err: Box::new(e),
27 })
28}
29
30pub fn parse_lf_terminated_string<R: Read + Seek>(
31 reader: &mut R,
32 _options: &ReadOptions,
33 _: (),
34) -> BinResult<String> {
35 let pos = reader.seek(SeekFrom::Current(0))?;
36
37 String::from_utf8(
38 reader
39 .bytes()
40 .filter_map(Result::ok)
41 .take_while(|&b| b != b'\n')
42 .collect(),
43 )
44 .map_err(|e| binread::Error::Custom {
45 pos: pos as u64,
46 err: Box::new(e),
47 })
48}
49
50pub fn parse_null_terminated_euc_kr_string<R: Read + Seek>(
51 reader: &mut R,
52 _options: &ReadOptions,
53 _: (),
54) -> BinResult<String> {
55 let bytes: Vec<u8> = reader
56 .bytes()
57 .filter_map(Result::ok)
58 .take_while(|&b| b != 0)
59 .collect();
60
61 let (cow, _encoding_used, _had_errors) = EUC_KR.decode(&bytes);
62 Ok(cow.to_string())
63}