22using System . Linq ;
33using System . Text ;
44
5- namespace GlobExpressions . AST
5+ namespace GlobExpressions . AST ;
6+
7+ internal sealed class CharacterSet : SubSegment
68{
7- internal sealed class CharacterSet : SubSegment
8- {
9- public bool Inverted { get ; }
10- public string Characters { get ; }
11- public string ExpandedCharacters { get ; }
9+ public bool Inverted { get ; }
10+ public string Characters { get ; }
11+ public string ExpandedCharacters { get ; }
1212
13- public CharacterSet ( string characters , bool inverted )
14- : base ( GlobNodeType . CharacterSet )
15- {
13+ public CharacterSet ( string characters , bool inverted )
14+ : base ( GlobNodeType . CharacterSet )
15+ {
1616Characters = characters ;
1717Inverted = inverted ;
1818this . ExpandedCharacters = CalculateExpandedForm ( characters ) ;
1919}
2020
21- public bool Matches ( char c , bool caseSensitive ) => Contains ( c , caseSensitive ) != this . Inverted ;
21+ public bool Matches ( char c , bool caseSensitive ) => Contains ( c , caseSensitive ) != this . Inverted ;
2222
23- private bool Contains ( char c , bool caseSensitive ) => ExpandedCharacters . IndexOf ( c . ToString ( ) , caseSensitive ? StringComparison . Ordinal : StringComparison . OrdinalIgnoreCase ) >= 0 ;
23+ private bool Contains ( char c , bool caseSensitive ) => ExpandedCharacters . IndexOf ( c . ToString ( ) , caseSensitive ? StringComparison . Ordinal : StringComparison . OrdinalIgnoreCase ) >= 0 ;
2424
25- private string CalculateExpandedForm ( string chars )
25+ private string CalculateExpandedForm ( string chars )
26+ {
27+ var sb = new StringBuilder ( ) ;
28+ var i = 0 ;
29+ var len = chars . Length ;
30+
31+ // if first character is special, add it
32+ if ( chars . StartsWith ( "-" ) || chars . StartsWith ( "[" ) || chars . StartsWith ( "]" ) )
2633{
27- var sb = new StringBuilder ( ) ;
28- var i = 0 ;
29- var len = chars . Length ;
34+ sb . Append ( chars [ 0 ] ) ;
35+ i ++ ;
36+ }
3037
31- // if first character is special, add it
32- if ( chars . StartsWith ( "-" ) || chars . StartsWith ( "[" ) || chars . StartsWith ( "]" ) )
33- {
34- sb . Append ( chars [ 0 ] ) ;
35- i ++ ;
36- }
38+ while ( true )
39+ {
40+ if ( i >= len )
41+ break ;
3742
38- while ( true )
43+ if ( chars [ i ] == '-' )
3944{
40- if ( i >= len )
41- break ;
42-
43- if ( chars [ i ] == '-' )
45+ if ( i == len - 1 )
4446{
45- if ( i == len - 1 )
46- {
47- // - is last character so just add it
48- sb . Append ( '-' ) ;
49- }
50- else
51- {
52- for ( var c = chars [ i - 1 ] + 1 ; c <= chars [ i + 1 ] ; c ++ )
53- {
54- sb . Append ( ( char ) c ) ;
55- }
56- i ++ ; // skip trailing range
57- }
58- }
59- else if ( chars [ i ] == '/' )
60- {
61- i ++ ; // skip
47+ // - is last character so just add it
48+ sb . Append ( '-' ) ;
6249}
6350else
6451{
65- sb . Append ( chars [ i ] ) ;
52+ for ( var c = chars [ i - 1 ] + 1 ; c <= chars [ i + 1 ] ; c ++ )
53+ {
54+ sb . Append ( ( char ) c ) ;
55+ }
56+ i ++ ; // skip trailing range
6657}
67- i ++ ;
6858}
69-
70- return sb . ToString ( ) ;
59+ else if ( chars [ i ] == '/' )
60+ {
61+ i ++ ; // skip
62+ }
63+ else
64+ {
65+ sb . Append ( chars [ i ] ) ;
66+ }
67+ i ++ ;
7168}
69+
70+ return sb . ToString ( ) ;
7271}
73- }
72+ }