@@ -77,14 +77,8 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
7777 if ( text is null )
7878 return null ;
7979
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-
8680 // Flags indicating which pre-defined TextDecoration have been matched
87- byte matchedDecorations = 0 ;
81+ Decorations matchedDecorations = Decorations . None ;
8882
8983 // Sanitize the input
9084 ReadOnlySpan < char > decorationsSpan = text . AsSpan ( ) . Trim ( ) ;
@@ -95,31 +89,29 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
9589
9690 // Create new collection, save re-allocations
9791 TextDecorationCollection textDecorations = new ( 1 + decorationsSpan . Count ( ',' ) ) ;
98-
99- // Go through each item in the input and match accordingly
10092 foreach ( Range segment in decorationsSpan . Split ( ',' ) )
10193 {
10294 ReadOnlySpan < char > decoration = decorationsSpan [ segment ] . Trim ( ) ;
10395
104- if ( decoration . Equals ( "Overline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & OverlineMatch ) == 0 )
96+ if ( decoration . Equals ( "Overline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . OverlineMatch ) )
10597 {
10698 textDecorations . Add ( TextDecorations . OverLine [ 0 ] ) ;
107- matchedDecorations |= OverlineMatch ;
99+ matchedDecorations |= Decorations . OverlineMatch ;
108100 }
109- else if ( decoration . Equals ( "Baseline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & BaselineMatch ) == 0 )
101+ else if ( decoration . Equals ( "Baseline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . BaselineMatch ) )
110102 {
111103 textDecorations . Add ( TextDecorations . Baseline [ 0 ] ) ;
112- matchedDecorations |= BaselineMatch ;
104+ matchedDecorations |= Decorations . BaselineMatch ;
113105 }
114- else if ( decoration . Equals ( "Underline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & UnderlineMatch ) == 0 )
106+ else if ( decoration . Equals ( "Underline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . UnderlineMatch ) )
115107 {
116108 textDecorations . Add ( TextDecorations . Underline [ 0 ] ) ;
117- matchedDecorations |= UnderlineMatch ;
109+ matchedDecorations |= Decorations . UnderlineMatch ;
118110 }
119- else if ( decoration . Equals ( "Strikethrough" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & StrikethroughMatch ) == 0 )
111+ else if ( decoration . Equals ( "Strikethrough" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . StrikethroughMatch ) )
120112 {
121113 textDecorations . Add ( TextDecorations . Strikethrough [ 0 ] ) ;
122- matchedDecorations |= StrikethroughMatch ;
114+ matchedDecorations |= Decorations . StrikethroughMatch ;
123115 }
124116 else
125117 {
@@ -150,5 +142,18 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
150142 // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
151143 return base . ConvertTo ( context , culture , value , destinationType ) ;
152144 }
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+ }
153158 }
154159}
0 commit comments