@@ -14,13 +14,10 @@ public interface IAtom : IDescribable
14
14
internal class CharacterClass : IAtom
15
15
{
16
16
public static readonly string Pattern = @"(?<!\\)\[(?<expression>.*?)(?<!\\)\]" ;
17
- private static readonly Regex Matcher = new Regex ( "^" + Pattern + " $", RegexOptions . Compiled ) ;
17
+ private static readonly Regex Matcher = new Regex ( $ "^ { Pattern } $", RegexOptions . Compiled ) ;
18
18
19
- public bool InverseMatching { get ; private set ; }
20
- public IList < string > CharacterSpecifiers { get ; private set ; }
21
-
22
- private readonly string _specifier ;
23
- private readonly Quantifier _quantifier ;
19
+ public bool InverseMatching { get ; }
20
+ public IList < string > CharacterSpecifiers { get ; }
24
21
25
22
public CharacterClass ( string specifier , Quantifier quantifier )
26
23
{
@@ -29,38 +26,26 @@ public CharacterClass(string specifier, Quantifier quantifier)
29
26
throw new ArgumentNullException ( ) ;
30
27
}
31
28
32
- _quantifier = quantifier ;
33
- Match m = Matcher . Match ( specifier ) ;
29
+ Quantifier = quantifier ;
30
+ var m = Matcher . Match ( specifier ) ;
34
31
if ( ! m . Success )
35
32
{
36
33
throw new ArgumentException ( "The given specifier does not denote a character class" ) ;
37
34
}
38
- this . _specifier = specifier ;
39
- string actualSpecifier = m . Groups [ "expression" ] . Value ;
35
+ Specifier = specifier ;
36
+ var actualSpecifier = m . Groups [ "expression" ] . Value ;
40
37
InverseMatching = actualSpecifier . StartsWith ( "^" ) ;
41
38
CharacterSpecifiers = ExtractCharacterSpecifiers ( InverseMatching ? actualSpecifier . Substring ( 1 ) : actualSpecifier ) ;
42
39
}
43
40
44
- public string Specifier
45
- {
46
- get
47
- {
48
- return _specifier ;
49
- }
50
- }
41
+ public string Specifier { get ; }
51
42
52
- public Quantifier Quantifier
53
- {
54
- get
55
- {
56
- return _quantifier ;
57
- }
58
- }
43
+ public Quantifier Quantifier { get ; }
59
44
60
45
private static readonly Regex CharacterRanges = new Regex ( @"(\\[dDwWsS]|(\\[ntfvr]|\\([0-7]{3}|x[\dA-F]{2}|u[\dA-F]{4}|[\\\.\[\]])|.)(-(\\[ntfvr]|\\([0-7]{3}|x[A-F]{2}|u[\dA-F]{4}|[\.\\\[\]])|.))?)" , RegexOptions . Compiled ) ;
61
46
private IList < string > ExtractCharacterSpecifiers ( string characterClass )
62
47
{
63
- MatchCollection specifiers = CharacterRanges . Matches ( characterClass ) ;
48
+ var specifiers = CharacterRanges . Matches ( characterClass ) ;
64
49
var result = new List < string > ( ) ;
65
50
66
51
foreach ( Match specifier in specifiers )
@@ -71,7 +56,8 @@ private IList<string> ExtractCharacterSpecifiers(string characterClass)
71
56
{
72
57
throw new ArgumentException ( "Character Ranges that have incorrectly escaped characters as target are not allowed" ) ;
73
58
}
74
- else if ( specifier . Value . Length == 1 )
59
+
60
+ if ( specifier . Value . Length == 1 )
75
61
{
76
62
// Something's bork with the Pattern. For now we skip this it shouldn't affect anyone
77
63
continue ;
@@ -82,16 +68,10 @@ private IList<string> ExtractCharacterSpecifiers(string characterClass)
82
68
return result ;
83
69
}
84
70
85
- public string Description
86
- {
87
- get
88
- {
89
- return string . Format ( InverseMatching
90
- ? AssistantResources . AtomDescription_CharacterClass_Inverted
91
- : AssistantResources . AtomDescription_CharacterClass
92
- , HumanReadableClass ( ) ) ;
93
- }
94
- }
71
+ public string Description => string . Format ( InverseMatching
72
+ ? AssistantResources . AtomDescription_CharacterClass_Inverted
73
+ : AssistantResources . AtomDescription_CharacterClass
74
+ , HumanReadableClass ( ) ) ;
95
75
96
76
private string HumanReadableClass ( )
97
77
{
@@ -100,93 +80,67 @@ private string HumanReadableClass()
100
80
101
81
public override bool Equals ( object obj )
102
82
{
103
- var other = obj as CharacterClass ;
104
- return other != null
83
+ return obj is CharacterClass other
105
84
&& other . Quantifier . Equals ( Quantifier )
106
- && other . _specifier . Equals ( _specifier ) ;
85
+ && other . Specifier . Equals ( Specifier ) ;
107
86
}
108
87
109
88
public override int GetHashCode ( )
110
89
{
111
- return _specifier . GetHashCode ( ) ;
90
+ return Specifier . GetHashCode ( ) ;
112
91
}
113
92
}
114
93
115
94
public class Group : IAtom
116
95
{
117
96
public static readonly string Pattern = @"(?<!\\)\((?<expression>.*(?<!\\))\)" ;
118
- private static readonly Regex Matcher = new Regex ( "^" + Pattern + "$" , RegexOptions . Compiled ) ;
119
-
120
- private readonly IRegularExpression _subexpression ;
121
- private readonly string _specifier ;
122
- private readonly Quantifier _quantifier ;
97
+ private static readonly Regex Matcher = new Regex ( $ "^{ Pattern } $", RegexOptions . Compiled ) ;
123
98
124
99
public Group ( string specifier , Quantifier quantifier ) {
125
100
if ( specifier == null || quantifier == null )
126
101
{
127
102
throw new ArgumentNullException ( ) ;
128
103
}
129
104
130
- _quantifier = quantifier ;
131
- Match m = Matcher . Match ( specifier ) ;
105
+ Quantifier = quantifier ;
106
+ var m = Matcher . Match ( specifier ) ;
132
107
if ( ! m . Success )
133
108
{
134
109
throw new ArgumentException ( "The given specifier does not denote a Group" ) ;
135
110
}
136
- _subexpression = RegularExpression . Parse ( m . Groups [ "expression" ] . Value ) ;
137
- _specifier = specifier ;
111
+ Subexpression = RegularExpression . Parse ( m . Groups [ "expression" ] . Value ) ;
112
+ Specifier = specifier ;
138
113
}
139
114
140
- public IRegularExpression Subexpression { get { return _subexpression ; } }
141
-
142
- public Quantifier Quantifier
143
- {
144
- get
145
- {
146
- return _quantifier ;
147
- }
148
- }
115
+ public IRegularExpression Subexpression { get ; }
149
116
150
- public string Specifier
151
- {
152
- get
153
- {
154
- return _specifier ;
155
- }
156
- }
117
+ public Quantifier Quantifier { get ; }
157
118
158
- public string Description
159
- {
160
- get
161
- {
162
- return string . Format ( AssistantResources . AtomDescription_Group , _specifier ) ;
163
- }
164
- }
119
+ public string Specifier { get ; }
120
+
121
+ public string Description => string . Format ( AssistantResources . AtomDescription_Group , Specifier ) ;
165
122
166
123
public override bool Equals ( object obj )
167
124
{
168
- var other = obj as Group ;
169
- return other != null
125
+ return obj is Group other
170
126
&& other . Quantifier . Equals ( Quantifier )
171
- && other . _specifier . Equals ( _specifier ) ;
127
+ && other . Specifier . Equals ( Specifier ) ;
172
128
}
173
129
174
130
public override int GetHashCode ( )
175
131
{
176
- return _specifier . GetHashCode ( ) ;
132
+ return Specifier . GetHashCode ( ) ;
177
133
}
178
134
}
179
135
180
136
internal class Literal : IAtom
181
137
{
182
138
public static readonly string Pattern = @"(?<expression>\\(u[\dA-F]{4}|x[\dA-F]{2}|[0-7]{3}|[bB\(\){}\\\[\]\.+*?1-9nftvrdDwWsS])|[^()\[\]{}\\*+?^$])" ;
183
- private static readonly Regex Matcher = new Regex ( "^" + Pattern + " $", RegexOptions . Compiled ) ;
139
+ private static readonly Regex Matcher = new Regex ( $ "^ { Pattern } $", RegexOptions . Compiled ) ;
184
140
private static readonly ISet < char > EscapeLiterals = new HashSet < char > ( ) ;
185
- private readonly string _specifier ;
186
- private readonly Quantifier _quantifier ;
187
141
188
142
static Literal ( ) {
189
- foreach ( char escape in new char [ ] { '.' , '+' , '*' , '?' , '(' , ')' , '{' , '}' , '[' , ']' , '|' , '\\ ' } )
143
+ foreach ( var escape in new [ ] { '.' , '+' , '*' , '?' , '(' , ')' , '{' , '}' , '[' , ']' , '|' , '\\ ' } )
190
144
{
191
145
EscapeLiterals . Add ( escape ) ;
192
146
}
@@ -212,30 +166,18 @@ public Literal(string specifier, Quantifier quantifier)
212
166
throw new ArgumentNullException ( ) ;
213
167
}
214
168
215
- _quantifier = quantifier ;
216
- Match m = Matcher . Match ( specifier ) ;
169
+ Quantifier = quantifier ;
170
+ var m = Matcher . Match ( specifier ) ;
217
171
if ( ! m . Success )
218
172
{
219
173
throw new ArgumentException ( "The given specifier does not denote a Literal" ) ;
220
174
}
221
- _specifier = specifier ;
175
+ Specifier = specifier ;
222
176
}
223
177
224
- public string Specifier
225
- {
226
- get
227
- {
228
- return _specifier ;
229
- }
230
- }
178
+ public string Specifier { get ; }
231
179
232
- public Quantifier Quantifier
233
- {
234
- get
235
- {
236
- return _quantifier ;
237
- }
238
- }
180
+ public Quantifier Quantifier { get ; }
239
181
240
182
private static readonly Dictionary < char , string > _escapeDescriptions = new Dictionary < char , string > ( ) ;
241
183
public string Description
@@ -248,9 +190,9 @@ public string Description
248
190
// - escape sequences (each having a different description)
249
191
// - codepoint escapes (belongs into above category but kept separate)
250
192
// - and actually boring literal matches
251
- if ( _specifier . Length > 1 )
193
+ if ( Specifier . Length > 1 )
252
194
{
253
- string relevant = _specifier . Substring ( 1 ) ; // skip the damn Backslash at the start
195
+ var relevant = Specifier . Substring ( 1 ) ; // skip the damn Backslash at the start
254
196
if ( relevant . Length > 1 ) // longer sequences
255
197
{
256
198
if ( relevant . StartsWith ( "u" ) )
@@ -279,26 +221,23 @@ public string Description
279
221
return _escapeDescriptions [ relevant [ 0 ] ] ;
280
222
}
281
223
}
282
- if ( _specifier . Equals ( "." ) )
283
- {
284
- return AssistantResources . AtomDescription_Dot ;
285
- }
286
- return string . Format ( AssistantResources . AtomDescription_Literal_ActualLiteral , _specifier ) ;
287
224
225
+ return Specifier . Equals ( "." )
226
+ ? AssistantResources . AtomDescription_Dot
227
+ : string . Format ( AssistantResources . AtomDescription_Literal_ActualLiteral , Specifier ) ;
288
228
}
289
229
}
290
230
291
231
public override bool Equals ( object obj )
292
232
{
293
- var other = obj as Literal ;
294
- return other != null
233
+ return obj is Literal other
295
234
&& other . Quantifier . Equals ( Quantifier )
296
- && other . _specifier . Equals ( _specifier ) ;
235
+ && other . Specifier . Equals ( Specifier ) ;
297
236
}
298
237
299
238
public override int GetHashCode ( )
300
239
{
301
- return _specifier . GetHashCode ( ) ;
240
+ return Specifier . GetHashCode ( ) ;
302
241
}
303
242
}
304
243
}
0 commit comments