@@ -77,14 +77,8 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
77
77
if ( text is null )
78
78
return null ;
79
79
80
- // Define constants that will make sure the match has been unique
81
- const byte OverlineMatch = 1 << 0 ;
82
- const byte BaselineMatch = 1 << 1 ;
83
- const byte UnderlineMatch = 1 << 2 ;
84
- const byte StrikethroughMatch = 1 << 3 ;
85
-
86
80
// Flags indicating which pre-defined TextDecoration have been matched
87
- byte matchedDecorations = 0 ;
81
+ Decorations matchedDecorations = Decorations . None ;
88
82
89
83
// Sanitize the input
90
84
ReadOnlySpan < char > decorationsSpan = text . AsSpan ( ) . Trim ( ) ;
@@ -95,31 +89,29 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
95
89
96
90
// Create new collection, save re-allocations
97
91
TextDecorationCollection textDecorations = new ( 1 + decorationsSpan . Count ( ',' ) ) ;
98
-
99
- // Go through each item in the input and match accordingly
100
92
foreach ( Range segment in decorationsSpan . Split ( ',' ) )
101
93
{
102
94
ReadOnlySpan < char > decoration = decorationsSpan [ segment ] . Trim ( ) ;
103
95
104
- if ( decoration . Equals ( "Overline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & OverlineMatch ) == 0 )
96
+ if ( decoration . Equals ( "Overline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . OverlineMatch ) )
105
97
{
106
98
textDecorations . Add ( TextDecorations . OverLine [ 0 ] ) ;
107
- matchedDecorations |= OverlineMatch ;
99
+ matchedDecorations |= Decorations . OverlineMatch ;
108
100
}
109
- else if ( decoration . Equals ( "Baseline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & BaselineMatch ) == 0 )
101
+ else if ( decoration . Equals ( "Baseline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . BaselineMatch ) )
110
102
{
111
103
textDecorations . Add ( TextDecorations . Baseline [ 0 ] ) ;
112
- matchedDecorations |= BaselineMatch ;
104
+ matchedDecorations |= Decorations . BaselineMatch ;
113
105
}
114
- else if ( decoration . Equals ( "Underline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & UnderlineMatch ) == 0 )
106
+ else if ( decoration . Equals ( "Underline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . UnderlineMatch ) )
115
107
{
116
108
textDecorations . Add ( TextDecorations . Underline [ 0 ] ) ;
117
- matchedDecorations |= UnderlineMatch ;
109
+ matchedDecorations |= Decorations . UnderlineMatch ;
118
110
}
119
- else if ( decoration . Equals ( "Strikethrough" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & StrikethroughMatch ) == 0 )
111
+ else if ( decoration . Equals ( "Strikethrough" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . StrikethroughMatch ) )
120
112
{
121
113
textDecorations . Add ( TextDecorations . Strikethrough [ 0 ] ) ;
122
- matchedDecorations |= StrikethroughMatch ;
114
+ matchedDecorations |= Decorations . StrikethroughMatch ;
123
115
}
124
116
else
125
117
{
@@ -150,5 +142,18 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
150
142
// Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
151
143
return base . ConvertTo ( context , culture , value , destinationType ) ;
152
144
}
145
+
146
+ /// <summary>
147
+ /// Abstraction helper of matched decorations during conversion.
148
+ /// </summary>
149
+ [ Flags ]
150
+ private enum Decorations : byte
151
+ {
152
+ None = 0 ,
153
+ OverlineMatch = 1 << 0 ,
154
+ BaselineMatch = 1 << 1 ,
155
+ UnderlineMatch = 1 << 2 ,
156
+ StrikethroughMatch = 1 << 3 ,
157
+ }
153
158
}
154
159
}
0 commit comments