8
8
9
9
namespace Hl7 . Cql . CodeGeneration . NET
10
10
{
11
- internal class VariableNameGenerator
11
+ internal sealed class VariableNameGenerator
12
12
{
13
- private readonly object SyncRoot = new ( ) ;
14
- public string Postfix { get ; }
13
+ private readonly object _syncRoot = new ( ) ;
14
+ private readonly List < char > _letters = [ ( char ) ( 'a' - 1 ) ] ;
15
+ private readonly string _prefix = string . Empty ;
15
16
16
- private List < string > Reserved { get ; }
17
-
18
- private readonly List < char > Letters = [ ( char ) ( 'a' - 1 ) ] ;
19
- private readonly string Prefix = string . Empty ;
17
+ private string Postfix { get ; }
20
18
19
+ private List < string > Reserved { get ; }
21
20
22
21
/// <summary>
23
22
/// Create a new VariableNameGenerator with an (optional) set of extra reserved variable names
@@ -30,9 +29,9 @@ public VariableNameGenerator ForNewScope(IEnumerable<ParameterExpression>? scope
30
29
}
31
30
32
31
/// <inheritdoc cref="ForNewScope(IEnumerable{ParameterExpression}?)"/>
33
- public VariableNameGenerator ForNewScope ( IEnumerable < string > ? scopeNames )
32
+ private VariableNameGenerator ForNewScope ( IEnumerable < string > ? scopeNames )
34
33
{
35
- var newGenerator = new VariableNameGenerator ( Letters , Reserved . Concat ( scopeNames ?? [ ] ) , Postfix ) ;
34
+ var newGenerator = new VariableNameGenerator ( _letters , Reserved . Concat ( scopeNames ?? [ ] ) , Postfix ) ;
36
35
return newGenerator ;
37
36
}
38
37
@@ -42,109 +41,50 @@ public VariableNameGenerator(IEnumerable<string>? reserved = null, string postfi
42
41
Postfix = postfix ;
43
42
}
44
43
45
- public VariableNameGenerator ( IEnumerable < ParameterExpression > reserved , string postfix = "" ) :
46
- this ( reserved . Where ( p => p . Name is not null ) . Select ( p => p . Name ! ) , postfix )
47
- {
48
- // Nothing
49
- }
50
-
51
- internal VariableNameGenerator ( List < char > state , IEnumerable < string > ? reserved = null , string postfix = "" )
44
+ private VariableNameGenerator ( List < char > state , IEnumerable < string > ? reserved = null , string postfix = "" )
52
45
{
53
46
Reserved = reserved ? . ToList ( ) ?? [ ] ;
54
47
Postfix = postfix ;
55
- Letters = state ;
48
+ _letters = state ;
56
49
}
57
50
58
- public virtual string Next ( )
51
+ public string Next ( )
59
52
{
60
- lock ( SyncRoot )
53
+ lock ( _syncRoot )
61
54
{
62
55
string vn = "" ;
63
56
do
64
57
{
65
- var lastIndex = Letters . Count - 1 ;
66
- var next = ( char ) ( Letters [ lastIndex ] + 1 ) ;
58
+ var lastIndex = _letters . Count - 1 ;
59
+ var next = ( char ) ( _letters [ lastIndex ] + 1 ) ;
67
60
if ( next > 'z' )
68
61
{
69
62
next = 'a' ;
70
- Letters [ lastIndex ] = next ;
71
- if ( Letters . Count > 1 )
63
+ _letters [ lastIndex ] = next ;
64
+ if ( _letters . Count > 1 )
72
65
{
73
- if ( Letters [ 0 ] == 'z' )
66
+ if ( _letters [ 0 ] == 'z' )
74
67
{
75
- Letters . Insert ( 0 , 'a' ) ;
68
+ _letters . Insert ( 0 , 'a' ) ;
76
69
}
77
70
else
78
71
{
79
- Letters [ 0 ] = ( char ) ( Letters [ 0 ] + 1 ) ;
72
+ _letters [ 0 ] = ( char ) ( _letters [ 0 ] + 1 ) ;
80
73
}
81
74
}
82
- else Letters . Insert ( 0 , 'a' ) ;
75
+ else _letters . Insert ( 0 , 'a' ) ;
83
76
}
84
77
else
85
78
{
86
- Letters [ lastIndex ] = next ;
79
+ _letters [ lastIndex ] = next ;
87
80
}
88
- vn = $ "{ Prefix } { new string ( Letters . ToArray ( ) ) } { Postfix } ";
81
+ vn = $ "{ _prefix } { new string ( _letters . ToArray ( ) ) } { Postfix } ";
89
82
}
90
83
while ( Reserved . Contains ( vn ) || SyntaxFacts . GetKeywordKind ( vn ) != SyntaxKind . None ) ;
91
84
92
85
return vn ;
93
86
}
94
87
}
95
88
96
- public static string ? NormalizeIdentifier ( string ? identifier )
97
- {
98
- if ( string . IsNullOrEmpty ( identifier ) )
99
- return null ;
100
-
101
- ReadOnlySpan < char > span = identifier . AsSpan ( ) ;
102
-
103
- int leadingUnderscoreCount = 0 ;
104
- while ( leadingUnderscoreCount < span . Length && span [ leadingUnderscoreCount ] == '_' )
105
- leadingUnderscoreCount ++ ;
106
-
107
- if ( leadingUnderscoreCount > 0 )
108
- span = span [ leadingUnderscoreCount ..] ;
109
-
110
- if ( span . Length > 0 && span [ 0 ] == '$' )
111
- span = span [ 1 ..] ;
112
-
113
- Span < char > buffer = stackalloc char [ span . Length + 2 ] ;
114
- int bufferIndex = 0 ;
115
-
116
- foreach ( var c in span )
117
- {
118
- switch ( c )
119
- {
120
- case '"' :
121
- case '\' ' :
122
- continue ;
123
- case '&' :
124
- buffer [ bufferIndex ++ ] = 'a' ;
125
- buffer [ bufferIndex ++ ] = 'n' ;
126
- buffer [ bufferIndex ++ ] = 'd' ;
127
- continue ;
128
- default :
129
- buffer [ bufferIndex ++ ] = SyntaxFacts . IsIdentifierPartCharacter ( c ) ? c : '_' ;
130
- break ;
131
- }
132
- }
133
-
134
- var normalized = buffer [ ..bufferIndex ] . ToString ( ) ;
135
-
136
- if ( normalized . Length > 0 && ! SyntaxFacts . IsIdentifierStartCharacter ( normalized [ 0 ] ) )
137
- //normalized = "_" + normalized;
138
- leadingUnderscoreCount ++ ;
139
-
140
- if ( leadingUnderscoreCount > 0 )
141
- normalized = new string ( '_' , leadingUnderscoreCount ) + normalized ;
142
-
143
- if ( SyntaxFacts . GetKeywordKind ( normalized ) != SyntaxKind . None )
144
- normalized = $ "@{ normalized } ";
145
-
146
- return normalized ;
147
- }
148
-
149
89
}
150
90
}
0 commit comments