-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bytestr, charset and unicode modules.
- Loading branch information
Showing
5 changed files
with
262 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::charset::ASCII_TABLE; | ||
use crate::unicode; | ||
|
||
pub fn to_ascii(bytes: &[u8]) -> String { | ||
bytes | ||
.iter() | ||
.cloned() | ||
.map(|b| char::from(ASCII_TABLE[b as usize])) | ||
.collect::<String>() | ||
.to_string() | ||
} | ||
|
||
pub fn to_unicode(bytes: &[u8]) -> String { | ||
bytes | ||
.iter() | ||
.cloned() | ||
.map(char::from) | ||
.collect::<String>() | ||
.to_string() | ||
} | ||
|
||
pub fn to_utf8(bytes: &[u8]) -> String { | ||
unicode::to_utf8(&to_unicode(bytes)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use pretty_assertions::assert_eq; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_to_ascii() { | ||
assert_eq!(to_ascii(&[5, 225, 248, 229]), "_axe"); | ||
} | ||
|
||
#[test] | ||
fn test_to_unicode() { | ||
assert_eq!(to_unicode(&[225, 248, 229]), "áøå"); | ||
} | ||
|
||
#[test] | ||
fn test_to_utf8() { | ||
assert_eq!(to_utf8(&[5, 225, 248, 229]), "•axe"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
use lazy_static::lazy_static; | ||
|
||
lazy_static! { | ||
pub static ref ASCII_TABLE: Vec<u8> = { | ||
const ASCII_TABLE: &str = "________________[]0123456789____ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~_________________[]0123456789____ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~_"; | ||
ASCII_TABLE.as_bytes().to_vec() | ||
}; | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum CharColor { | ||
Brown, | ||
Green, | ||
White, | ||
} | ||
|
||
impl CharColor { | ||
pub fn from_char(c: char) -> Option<Self> { | ||
Self::from_byte(c as u8) | ||
} | ||
|
||
fn from_byte(b: u8) -> Option<Self> { | ||
let row_index = b / 16; | ||
let col_index = b % 16; | ||
|
||
// ascii | ||
match row_index { | ||
2..=7 => return Some(CharColor::White), | ||
10..=15 => return Some(CharColor::Brown), | ||
1 | 9 if (2..=11).contains(&col_index) => return Some(CharColor::Green), | ||
_ => {} | ||
} | ||
|
||
// chars | ||
match row_index { | ||
// white | ||
0 if [0, 5, 11, 14, 15].contains(&col_index) => Some(CharColor::White), | ||
1 if col_index == 12 => Some(CharColor::White), | ||
|
||
// brown | ||
0 if col_index == 13 => Some(CharColor::Brown), | ||
1 | 9 if (0..=1).contains(&col_index) => Some(CharColor::Brown), | ||
8 if [11, 13].contains(&col_index) => Some(CharColor::Brown), | ||
|
||
// green | ||
8 if [5, 14, 15].contains(&col_index) => Some(CharColor::Green), | ||
9 if col_index == 12 => Some(CharColor::Green), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_char_color() { | ||
let mut test_cases = vec![]; | ||
|
||
// row 1 | ||
test_cases.extend([ | ||
Some(CharColor::White), | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(CharColor::White), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(CharColor::White), | ||
None, | ||
Some(CharColor::Brown), | ||
Some(CharColor::White), | ||
Some(CharColor::White), | ||
]); | ||
|
||
// row 2 | ||
test_cases.extend([ | ||
Some(CharColor::Brown), | ||
Some(CharColor::Brown), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::White), | ||
None, | ||
None, | ||
None, | ||
]); | ||
|
||
// row 3-8 | ||
for _ in 0..6 * 16 { | ||
test_cases.push(Some(CharColor::White)); | ||
} | ||
|
||
// row 9 | ||
test_cases.extend([ | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(CharColor::Green), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Some(CharColor::Brown), | ||
None, | ||
Some(CharColor::Brown), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
]); | ||
|
||
// row 10 | ||
test_cases.extend([ | ||
Some(CharColor::Brown), | ||
Some(CharColor::Brown), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
Some(CharColor::Green), | ||
None, | ||
None, | ||
None, | ||
]); | ||
|
||
// row 1-16 | ||
for _ in 0..6 * 16 { | ||
test_cases.push(Some(CharColor::Brown)); | ||
} | ||
|
||
for (byte, expected) in test_cases.iter().enumerate() { | ||
assert_eq!( | ||
CharColor::from_byte(byte as u8), | ||
*expected, | ||
"{} [{},{}] = {:?}", | ||
byte, | ||
byte as u8 % 16, | ||
byte as u8 / 16, | ||
expected, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod bytestr; | ||
pub mod charset; | ||
pub mod unicode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::bytestr; | ||
|
||
pub fn to_ascii(ustr: &str) -> String { | ||
bytestr::to_ascii(&to_bytestr(ustr)) | ||
} | ||
|
||
pub fn to_utf8(ustr: &str) -> String { | ||
to_bytestr(ustr) | ||
.iter() | ||
.map(|c| c & 127) // strip color | ||
.map(|c| match c { | ||
32..=126 => char::from(c), | ||
0 | 5 | 14 | 15 | 28 => '•', | ||
_ => ' ', | ||
}) | ||
.collect() | ||
} | ||
|
||
pub fn to_bytestr(value: &str) -> Vec<u8> { | ||
value.chars().map(|c| c as u8).collect::<Vec<u8>>() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use pretty_assertions::assert_eq; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_to_ascii() { | ||
assert_eq!(to_ascii("áøå"), "axe"); | ||
} | ||
|
||
#[test] | ||
fn test_to_utf8() { | ||
assert_eq!(to_utf8("áøå"), "axe"); | ||
|
||
let ascii_chars = (32..=126).map(char::from).collect::<String>(); | ||
assert_eq!(to_utf8(&ascii_chars), ascii_chars); | ||
|
||
let mixed_chars = (28..=40).map(char::from).collect::<String>(); | ||
assert_eq!(to_utf8(&mixed_chars), "• !\"#$%&'("); | ||
} | ||
|
||
#[test] | ||
fn test_to_bstr() { | ||
assert_eq!(to_bytestr("áøå"), vec![225, 248, 229]); | ||
} | ||
} |