remove redundant CP437 ZST
This commit is contained in:
parent
fbd42d7c47
commit
2c3d31f649
2 changed files with 40 additions and 45 deletions
80
src/cp437.rs
80
src/cp437.rs
|
|
@ -1,11 +1,12 @@
|
|||
//! Contains functions to convert between UTF-8 and Codepage 437.
|
||||
//!
|
||||
//! See <https://en.wikipedia.org/wiki/Code_page_437#Character_set>
|
||||
|
||||
#![allow(clippy::module_name_repetitions)] // CP437 is the name of the encoding and thus repeated
|
||||
|
||||
use crate::{CharGrid, Cp437Grid};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Contains functions to convert between UTF-8 and Codepage 437.
|
||||
///
|
||||
/// See <https://en.wikipedia.org/wiki/Code_page_437#Character_set>
|
||||
pub struct Cp437Converter;
|
||||
|
||||
/// An array of 256 elements, mapping most of the CP437 values to UTF-8 characters
|
||||
///
|
||||
/// Mostly follows CP437, except 0x0A, which is kept for use as line ending.
|
||||
|
|
@ -34,47 +35,47 @@ const CP437_TO_UTF8: [char; 256] = [
|
|||
];
|
||||
static UTF8_TO_CP437: once_cell::sync::Lazy<HashMap<char, u8>> =
|
||||
once_cell::sync::Lazy::new(|| {
|
||||
let pairs = CP437_TO_UTF8
|
||||
CP437_TO_UTF8
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(move |(index, char)| (*char, index as u8));
|
||||
HashMap::from_iter(pairs)
|
||||
.map(
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
move |(index, char)| (*char, index as u8),
|
||||
)
|
||||
.collect::<HashMap<_, _>>()
|
||||
});
|
||||
|
||||
impl Cp437Converter {
|
||||
const MISSING_CHAR_CP437: u8 = 0x3F; // '?'
|
||||
const MISSING_CHAR_CP437: u8 = 0x3F; // '?'
|
||||
|
||||
/// Convert the provided bytes to UTF-8.
|
||||
pub fn cp437_to_str(cp437: &[u8]) -> String {
|
||||
cp437
|
||||
.iter()
|
||||
.map(move |char| Self::cp437_to_char(*char))
|
||||
.collect()
|
||||
}
|
||||
/// Convert the provided bytes to UTF-8.
|
||||
#[must_use]
|
||||
pub fn cp437_to_str(cp437: &[u8]) -> String {
|
||||
cp437.iter().map(move |char| cp437_to_char(*char)).collect()
|
||||
}
|
||||
|
||||
/// Convert a single CP-437 character to UTF-8.
|
||||
pub fn cp437_to_char(cp437: u8) -> char {
|
||||
CP437_TO_UTF8[cp437 as usize]
|
||||
}
|
||||
/// Convert a single CP-437 character to UTF-8.
|
||||
#[must_use]
|
||||
pub fn cp437_to_char(cp437: u8) -> char {
|
||||
CP437_TO_UTF8[cp437 as usize]
|
||||
}
|
||||
|
||||
/// Convert the provided text to CP-437 bytes.
|
||||
///
|
||||
/// Characters that are not available are mapped to '?'.
|
||||
pub fn str_to_cp437(utf8: &str) -> Vec<u8> {
|
||||
utf8.chars().map(Self::char_to_cp437).collect()
|
||||
}
|
||||
/// Convert the provided text to CP-437 bytes.
|
||||
///
|
||||
/// Characters that are not available are mapped to '?'.
|
||||
#[must_use]
|
||||
pub fn str_to_cp437(utf8: &str) -> Vec<u8> {
|
||||
utf8.chars().map(char_to_cp437).collect()
|
||||
}
|
||||
|
||||
/// Convert a single UTF-8 character to CP-437.
|
||||
pub fn char_to_cp437(utf8: char) -> u8 {
|
||||
*UTF8_TO_CP437
|
||||
.get(&utf8)
|
||||
.unwrap_or(&Self::MISSING_CHAR_CP437)
|
||||
}
|
||||
/// Convert a single UTF-8 character to CP-437.
|
||||
#[must_use]
|
||||
pub fn char_to_cp437(utf8: char) -> u8 {
|
||||
*UTF8_TO_CP437.get(&utf8).unwrap_or(&MISSING_CHAR_CP437)
|
||||
}
|
||||
|
||||
impl From<&Cp437Grid> for CharGrid {
|
||||
fn from(value: &Cp437Grid) -> Self {
|
||||
value.map(Cp437Converter::cp437_to_char)
|
||||
value.map(cp437_to_char)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,7 +87,7 @@ impl From<Cp437Grid> for CharGrid {
|
|||
|
||||
impl From<&CharGrid> for Cp437Grid {
|
||||
fn from(value: &CharGrid) -> Self {
|
||||
value.map(Cp437Converter::char_to_cp437)
|
||||
value.map(char_to_cp437)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -125,17 +126,14 @@ mod tests_feature_cp437 {
|
|||
│dx ≡ Σ √x²ⁿ·δx
|
||||
⌡"#;
|
||||
|
||||
let cp437 = Cp437Converter::str_to_cp437(utf8);
|
||||
let actual = Cp437Converter::cp437_to_str(&cp437);
|
||||
let cp437 = str_to_cp437(utf8);
|
||||
let actual = cp437_to_str(&cp437);
|
||||
assert_eq!(utf8, actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_invalid() {
|
||||
assert_eq!(
|
||||
Cp437Converter::cp437_to_char(Cp437Converter::char_to_cp437('😜')),
|
||||
'?'
|
||||
);
|
||||
assert_eq!(cp437_to_char(char_to_cp437('😜')), '?');
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -73,13 +73,10 @@ mod connections;
|
|||
mod constants;
|
||||
mod containers;
|
||||
#[cfg(feature = "cp437")]
|
||||
mod cp437;
|
||||
pub mod cp437;
|
||||
mod origin;
|
||||
mod packet;
|
||||
|
||||
#[cfg(feature = "cp437")]
|
||||
pub use crate::cp437::Cp437Converter;
|
||||
|
||||
// include README.md in doctest
|
||||
#[doc = include_str!("../README.md")]
|
||||
#[cfg(doctest)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue