@@ -83,14 +83,8 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
8383 if ( text is null )
8484 return null ;
8585
86- // Define constants that will make sure the match has been unique
87- const byte OverlineMatch = 1 << 0 ;
88- const byte BaselineMatch = 1 << 1 ;
89- const byte UnderlineMatch = 1 << 2 ;
90- const byte StrikethroughMatch = 1 << 3 ;
91-
9286 // Flags indicating which pre-defined TextDecoration have been matched
93- byte matchedDecorations = 0 ;
87+ Decorations matchedDecorations = Decorations . None ;
9488
9589 // Sanitize the input
9690 ReadOnlySpan < char > decorationsSpan = text . AsSpan ( ) . Trim ( ) ;
@@ -101,31 +95,29 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
10195
10296 // Create new collection, save re-allocations
10397 TextDecorationCollection textDecorations = new ( 1 + decorationsSpan . Count ( ',' ) ) ;
104-
105- // Go through each item in the input and match accordingly
10698 foreach ( Range segment in decorationsSpan . Split ( ',' ) )
10799 {
108100 ReadOnlySpan < char > decoration = decorationsSpan [ segment ] . Trim ( ) ;
109101
110- if ( decoration . Equals ( "Overline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & OverlineMatch ) == 0 )
102+ if ( decoration . Equals ( "Overline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . OverlineMatch ) )
111103 {
112104 textDecorations . Add ( TextDecorations . OverLine [ 0 ] ) ;
113- matchedDecorations |= OverlineMatch ;
105+ matchedDecorations |= Decorations . OverlineMatch ;
114106 }
115- else if ( decoration . Equals ( "Baseline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & BaselineMatch ) == 0 )
107+ else if ( decoration . Equals ( "Baseline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . BaselineMatch ) )
116108 {
117109 textDecorations . Add ( TextDecorations . Baseline [ 0 ] ) ;
118- matchedDecorations |= BaselineMatch ;
110+ matchedDecorations |= Decorations . BaselineMatch ;
119111 }
120- else if ( decoration . Equals ( "Underline" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & UnderlineMatch ) == 0 )
112+ else if ( decoration . Equals ( "Underline" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . UnderlineMatch ) )
121113 {
122114 textDecorations . Add ( TextDecorations . Underline [ 0 ] ) ;
123- matchedDecorations |= UnderlineMatch ;
115+ matchedDecorations |= Decorations . UnderlineMatch ;
124116 }
125- else if ( decoration . Equals ( "Strikethrough" , StringComparison . OrdinalIgnoreCase ) && ( matchedDecorations & StrikethroughMatch ) == 0 )
117+ else if ( decoration . Equals ( "Strikethrough" , StringComparison . OrdinalIgnoreCase ) && ! matchedDecorations . HasFlag ( Decorations . StrikethroughMatch ) )
126118 {
127119 textDecorations . Add ( TextDecorations . Strikethrough [ 0 ] ) ;
128- matchedDecorations |= StrikethroughMatch ;
120+ matchedDecorations |= Decorations . StrikethroughMatch ;
129121 }
130122 else
131123 {
@@ -156,5 +148,18 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
156148 // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
157149 return base . ConvertTo ( context , culture , value , destinationType ) ;
158150 }
151+
152+ /// <summary>
153+ /// Abstraction helper of matched decorations during conversion.
154+ /// </summary>
155+ [ Flags ]
156+ private enum Decorations : byte
157+ {
158+ None = 0 ,
159+ OverlineMatch = 1 << 0 ,
160+ BaselineMatch = 1 << 1 ,
161+ UnderlineMatch = 1 << 2 ,
162+ StrikethroughMatch = 1 << 3 ,
163+ }
159164 }
160165}
0 commit comments