From 5eb60090ed2232d9db6dc34ca7e134c22bdfced0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20St=C3=B6ckl?= Date: Thu, 4 Jul 2024 15:53:16 +0200 Subject: [PATCH] Handle newlines --- build.rs | 2 +- src/pokestr.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 0902377..d72a094 100644 --- a/build.rs +++ b/build.rs @@ -16,7 +16,7 @@ fn main() { let reader = BufReader::new(File::open(MAP_PATH).expect("Character map for generation iv")); let mapping: HashMap = reader.lines().filter_map(|r| { if let Ok(s) = r { if s.starts_with("0x") && s.len() >= 8 { Some(s) } else { None } } else { None } - }).map(|s| (u16::from_str_radix(&s[2..6], 16).expect(&format!("Invalid number: 0x{}", &s[2..6])), str_to_utf16_graphemes(&s[7..]).remove(0))).collect(); + }).map(|s| (u16::from_str_radix(&s[2..6], 16).expect(&format!("Invalid number: 0x{}", &s[2..6])), if "\\n".eq(&s[7..]) { Utf16Grapheme::Bmp(0x000au16) } else { str_to_utf16_graphemes(&s[7..]).remove(0) })).collect(); let mut writer = LineWriter::new(File::create(path).unwrap()); write!(writer, "use utf16::Utf16Grapheme; pub const CHARACTER_MAP_BY_GENIV: [Utf16Grapheme; {}] = [\n", CHARACTERS).unwrap(); diff --git a/src/pokestr.rs b/src/pokestr.rs index b3658d2..1eb296c 100644 --- a/src/pokestr.rs +++ b/src/pokestr.rs @@ -46,14 +46,26 @@ impl TryFrom<&Gen4Str> for String { } } +const HARD_CODED_MAPPINGS: [(u16, Utf16Grapheme); 1] = [(0xe000, Utf16Grapheme::Bmp(0x0a))]; + /// Look up the corresponding UTF16 grapheme to a pokémon gen iv character. /// Returns [None] when the character map does not contain such a character. fn to_utf16(geniv_char: u16) -> Option<&'static Utf16Grapheme> { + for m in &HARD_CODED_MAPPINGS { + if m.0 == geniv_char { + return Some(&m.1); + } + } CHARACTER_MAP_BY_GENIV.get(usize::from(geniv_char)) } /// Look up the corresponding pokémon gen iv character to a UTF16 grapheme. /// Returns [None] when the character map does not contain such a character. fn to_geniv_char(grapheme: &Utf16Grapheme) -> Option { + for m in &HARD_CODED_MAPPINGS { + if m.1 == *grapheme { + return Some(m.0); + } + } CHARACTER_MAP_BY_UTF16.binary_search_by(|(u, _)| u.cmp(grapheme)).map(|i| CHARACTER_MAP_BY_UTF16[i].1).ok() } \ No newline at end of file