Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5d5f09

Browse files
committed
Implement pretty-print for augumented script set.
1 parent e8a8e16 commit a5d5f09

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/mixed_script.rs

Lines changed: 34 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, Eq)]
9+
#[derive(Copy, Clone, PartialEq, Hash, Eq)]
910
pub struct AugmentedScriptSet {
1011
/// The base ScriptExtension value
1112
pub base: ScriptExtension,
@@ -72,6 +73,38 @@ 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
89+
.into_iter()
90+
.chain(hanb)
91+
.chain(jpan)
92+
.chain(kore)
93+
.chain(self.base.iter().map(Script::short_name))
94+
{
95+
if !first_entry {
96+
write!(f, ", ")?;
97+
} else {
98+
first_entry = false;
99+
}
100+
write!(f, "{}", writing_system)?;
101+
}
102+
write!(f, "}}")?;
103+
}
104+
Ok(())
105+
}
106+
}
107+
75108
impl AugmentedScriptSet {
76109
/// Intersect this set with another
77110
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)