8000 Implement pretty-print for augumented script set. · unicode-rs/unicode-security@8c35245 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8c35245

Browse files
committed
Implement pretty-print for augumented script set.
1 parent 2e6d28a commit 8c35245

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/mixed_script.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! [Mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection)
22
3+
use core::fmt::{self, Debug};
34
use unicode_script::{Script, ScriptExtension};
45

56
/// An Augmented script set, as defined by UTS 39
67
///
78
/// https://www.unicode.org/reports/tr39/#def-augmented-script-set
8-
#[derive(Copy, Clone, PartialEq, Debug, Hash)]
9+
#[derive(Copy, Clone, PartialEq, Hash)]
910
pub struct AugmentedScriptSet {
1011
/// The base ScriptExtension value
1112
pub base: ScriptExtension,
@@ -72,6 +73,42 @@ impl Default for AugmentedScriptSet {
7273
}
7374
}
7475

76+
impl Debug for AugmentedScriptSet {
77+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78+
if self.is_empty() {
79+
write!(f, "AugmentedScriptSet{{∅}}")?;
80+
} else if self.is_all() {
81+
write!(f, "AugmentedScriptSet{{ALL}}")?;
82+
} else {
83+
write!(f, "AugmentedScriptSet{{")?;
84+
let mut first_entry = true;
85+
let hanb = if self.hanb { Some("Hanb") } else { None };
86+
let jpan = if self.jpan { Some("Jpan") } else { None };
87+
let kore = if self.kore { Some("Kore") } else { None };
88+
for writing_system in None.into_iter().chain(hanb).chain(jpan).chain(kore) {
89+
if !first_entry {
90+
write!(f, ", ")?;
91+
} else {
92+
first_entry = false;
93+
}
94+
write!(f, "{}", writing_system)?;
95+
}
96+
for script in self.base.iter() {
97+
if !first_entry {
98+
write!(f, ", ")?;
99+
} else {
100+
first_entry = false;
101+
}
102+
write!(f, "{}", script.short_name())?;
103+
}
104+
//for script in self.hanb
105+
write!(f, "}}")?;
106+
}
107+
//if self.base.is_common() || self.base.is_
108+
Ok(())
109+
}
110+
}
111+
75112
impl AugmentedScriptSet {
76113
/// Intersect this set with another
77114
pub fn intersect_with(&mut self, other: Self) {

src/tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,40 @@ fn test_potential_mixed_script_detection() {
7777
assert!(is_potential_mixed_script_confusable_char('A'));
7878
assert!(!is_potential_mixed_script_confusable_char('D'));
7979
}
80+
81+
#[test]
82+
fn test_augmented_script_set() {
83+
use crate::mixed_script::AugmentedScriptSet;
84+
let augmented_script_sets = vec![
85+
AugmentedScriptSet::default(),
86+
AugmentedScriptSet::from('0'),
87+
AugmentedScriptSet::from('a'),
88+
AugmentedScriptSet::from('μ'),
89+
AugmentedScriptSet::from('汉'),
90+
AugmentedScriptSet::from('ひ'),
91+
AugmentedScriptSet::from('カ'),
92+
AugmentedScriptSet::from('한'),
93+
AugmentedScriptSet::from("汉ひ"),
94+
AugmentedScriptSet::from("汉a"),
95+
AugmentedScriptSet::from("汉μ"),
96+
AugmentedScriptSet::from("〆切"),
97+
];
98+
let debug_output = vec![
99+
"AugmentedScriptSet{ALL}",
100+
"AugmentedScriptSet{ALL}",
101+
"AugmentedScriptSet{Latn}",
102+
"AugmentedScriptSet{Grek}",
103+
"AugmentedScriptSet{Hanb, Jpan, Kore, Hani}",
104+
"AugmentedScriptSet{Jpan, Hira}",
105+
"AugmentedScriptSet{Jpan, Kana}",
106+
"AugmentedScriptSet{Kore, Hang}",
107+
"AugmentedScriptSet{Jpan}",
108+
"AugmentedScriptSet{∅}",
109+
"AugmentedScriptSet{∅}",
110+
"AugmentedScriptSet{Hanb, Jpan, Kore, Hani}",
111+
];
112+
113+
for (ss, output) in augmented_script_sets.into_iter().zip(debug_output) {
114+
assert_eq!(format!("{:?}", ss), output);
115+
}
116+
}

0 commit comments

Comments
 (0)
0