1
1
//! [Mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection)
2
2
3
+ use core:: fmt:: { self , Debug } ;
3
4
use unicode_script:: { Script , ScriptExtension } ;
4
5
5
6
/// An Augmented script set, as defined by UTS 39
6
7
///
7
8
/// 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 ) ]
9
10
pub struct AugmentedScriptSet {
10
11
/// The base ScriptExtension value
11
12
pub base : ScriptExtension ,
@@ -23,10 +24,7 @@ impl From<ScriptExtension> for AugmentedScriptSet {
23
24
let mut jpan = false ;
24
25
let mut kore = false ;
25
26
26
- if ext == ScriptExtension :: Single ( Script :: Common )
27
- || ext == ScriptExtension :: Single ( Script :: Inherited )
28
- || ext. contains_script ( Script :: Han )
29
- {
27
+ if ext. is_common ( ) || ext. is_inherited ( ) || ext. contains_script ( Script :: Han ) {
30
28
hanb = true ;
31
29
jpan = true ;
32
30
kore = true ;
@@ -67,14 +65,46 @@ impl From<&'_ str> for AugmentedScriptSet {
67
65
impl Default for AugmentedScriptSet {
68
66
fn default ( ) -> Self {
69
67
AugmentedScriptSet {
70
- base : ScriptExtension :: Single ( Script :: Common ) ,
68
+ base : Script :: Common . into ( ) ,
71
69
hanb : true ,
72
70
jpan : true ,
73
71
kore : true ,
74
72
}
75
73
}
76
74
}
77
75
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
+
78
108
impl AugmentedScriptSet {
79
109
/// Intersect this set with another
80
110
pub fn intersect_with ( & mut self , other : Self ) {
@@ -91,8 +121,7 @@ impl AugmentedScriptSet {
91
121
92
122
/// Check if the set is "All" (Common or Inherited)
93
123
pub fn is_all ( & self ) -> bool {
94
- self . base == ScriptExtension :: Single ( Script :: Common )
95
- || self . base == ScriptExtension :: Single ( Script :: Inherited )
124
+ self . base . is_common ( ) || self . base . is_inherited ( )
96
125
}
97
126
98
127
/// Construct an AugmentedScriptSet for a given character
0 commit comments