Skip to content

Commit 51cdfc0

Browse files
authored
Merge pull request #3583 from IvenBach/Rubberduck.RegexAssistant_C#6&7_Update
C#6/C#7 technical debt payment
2 parents 7d8cfd9 + 8cf93fd commit 51cdfc0

File tree

8 files changed

+220
-377
lines changed

8 files changed

+220
-377
lines changed

Rubberduck.RegexAssistant/Atom.cs

Lines changed: 47 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ public interface IAtom : IDescribable
1414
internal class CharacterClass : IAtom
1515
{
1616
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);
1818

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; }
2421

2522
public CharacterClass(string specifier, Quantifier quantifier)
2623
{
@@ -29,38 +26,26 @@ public CharacterClass(string specifier, Quantifier quantifier)
2926
throw new ArgumentNullException();
3027
}
3128

32-
_quantifier = quantifier;
33-
Match m = Matcher.Match(specifier);
29+
Quantifier = quantifier;
30+
var m = Matcher.Match(specifier);
3431
if (!m.Success)
3532
{
3633
throw new ArgumentException("The given specifier does not denote a character class");
3734
}
38-
this._specifier = specifier;
39-
string actualSpecifier = m.Groups["expression"].Value;
35+
Specifier = specifier;
36+
var actualSpecifier = m.Groups["expression"].Value;
4037
InverseMatching = actualSpecifier.StartsWith("^");
4138
CharacterSpecifiers= ExtractCharacterSpecifiers(InverseMatching ? actualSpecifier.Substring(1) : actualSpecifier);
4239
}
4340

44-
public string Specifier
45-
{
46-
get
47-
{
48-
return _specifier;
49-
}
50-
}
41+
public string Specifier { get; }
5142

52-
public Quantifier Quantifier
53-
{
54-
get
55-
{
56-
return _quantifier;
57-
}
58-
}
43+
public Quantifier Quantifier { get; }
5944

6045
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);
6146
private IList<string> ExtractCharacterSpecifiers(string characterClass)
6247
{
63-
MatchCollection specifiers = CharacterRanges.Matches(characterClass);
48+
var specifiers = CharacterRanges.Matches(characterClass);
6449
var result = new List<string>();
6550

6651
foreach (Match specifier in specifiers)
@@ -71,7 +56,8 @@ private IList<string> ExtractCharacterSpecifiers(string characterClass)
7156
{
7257
throw new ArgumentException("Character Ranges that have incorrectly escaped characters as target are not allowed");
7358
}
74-
else if (specifier.Value.Length == 1)
59+
60+
if (specifier.Value.Length == 1)
7561
{
7662
// Something's bork with the Pattern. For now we skip this it shouldn't affect anyone
7763
continue;
@@ -82,16 +68,10 @@ private IList<string> ExtractCharacterSpecifiers(string characterClass)
8268
return result;
8369
}
8470

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());
9575

9676
private string HumanReadableClass()
9777
{
@@ -100,93 +80,67 @@ private string HumanReadableClass()
10080

10181
public override bool Equals(object obj)
10282
{
103-
var other = obj as CharacterClass;
104-
return other != null
83+
return obj is CharacterClass other
10584
&& other.Quantifier.Equals(Quantifier)
106-
&& other._specifier.Equals(_specifier);
85+
&& other.Specifier.Equals(Specifier);
10786
}
10887

10988
public override int GetHashCode()
11089
{
111-
return _specifier.GetHashCode();
90+
return Specifier.GetHashCode();
11291
}
11392
}
11493

11594
public class Group : IAtom
11695
{
11796
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);
12398

12499
public Group(string specifier, Quantifier quantifier) {
125100
if (specifier == null || quantifier == null)
126101
{
127102
throw new ArgumentNullException();
128103
}
129104

130-
_quantifier = quantifier;
131-
Match m = Matcher.Match(specifier);
105+
Quantifier = quantifier;
106+
var m = Matcher.Match(specifier);
132107
if (!m.Success)
133108
{
134109
throw new ArgumentException("The given specifier does not denote a Group");
135110
}
136-
_subexpression = RegularExpression.Parse(m.Groups["expression"].Value);
137-
_specifier = specifier;
111+
Subexpression = RegularExpression.Parse(m.Groups["expression"].Value);
112+
Specifier = specifier;
138113
}
139114

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; }
149116

150-
public string Specifier
151-
{
152-
get
153-
{
154-
return _specifier;
155-
}
156-
}
117+
public Quantifier Quantifier { get; }
157118

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);
165122

166123
public override bool Equals(object obj)
167124
{
168-
var other = obj as Group;
169-
return other != null
125+
return obj is Group other
170126
&& other.Quantifier.Equals(Quantifier)
171-
&& other._specifier.Equals(_specifier);
127+
&& other.Specifier.Equals(Specifier);
172128
}
173129

174130
public override int GetHashCode()
175131
{
176-
return _specifier.GetHashCode();
132+
return Specifier.GetHashCode();
177133
}
178134
}
179135

180136
internal class Literal : IAtom
181137
{
182138
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);
184140
private static readonly ISet<char> EscapeLiterals = new HashSet<char>();
185-
private readonly string _specifier;
186-
private readonly Quantifier _quantifier;
187141

188142
static Literal() {
189-
foreach (char escape in new char[]{ '.', '+', '*', '?', '(', ')', '{', '}', '[', ']', '|', '\\' })
143+
foreach (var escape in new[]{ '.', '+', '*', '?', '(', ')', '{', '}', '[', ']', '|', '\\' })
190144
{
191145
EscapeLiterals.Add(escape);
192146
}
@@ -212,30 +166,18 @@ public Literal(string specifier, Quantifier quantifier)
212166
throw new ArgumentNullException();
213167
}
214168

215-
_quantifier = quantifier;
216-
Match m = Matcher.Match(specifier);
169+
Quantifier = quantifier;
170+
var m = Matcher.Match(specifier);
217171
if (!m.Success)
218172
{
219173
throw new ArgumentException("The given specifier does not denote a Literal");
220174
}
221-
_specifier = specifier;
175+
Specifier = specifier;
222176
}
223177

224-
public string Specifier
225-
{
226-
get
227-
{
228-
return _specifier;
229-
}
230-
}
178+
public string Specifier { get; }
231179

232-
public Quantifier Quantifier
233-
{
234-
get
235-
{
236-
return _quantifier;
237-
}
238-
}
180+
public Quantifier Quantifier { get; }
239181

240182
private static readonly Dictionary<char, string> _escapeDescriptions = new Dictionary<char, string>();
241183
public string Description
@@ -248,9 +190,9 @@ public string Description
248190
// - escape sequences (each having a different description)
249191
// - codepoint escapes (belongs into above category but kept separate)
250192
// - and actually boring literal matches
251-
if (_specifier.Length > 1)
193+
if (Specifier.Length > 1)
252194
{
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
254196
if (relevant.Length > 1) // longer sequences
255197
{
256198
if (relevant.StartsWith("u"))
@@ -279,26 +221,23 @@ public string Description
279221
return _escapeDescriptions[relevant[0]];
280222
}
281223
}
282-
if (_specifier.Equals("."))
283-
{
284-
return AssistantResources.AtomDescription_Dot;
285-
}
286-
return string.Format(AssistantResources.AtomDescription_Literal_ActualLiteral, _specifier);
287224

225+
return Specifier.Equals(".")
226+
? AssistantResources.AtomDescription_Dot
227+
: string.Format(AssistantResources.AtomDescription_Literal_ActualLiteral, Specifier);
288228
}
289229
}
290230

291231
public override bool Equals(object obj)
292232
{
293-
var other = obj as Literal;
294-
return other != null
233+
return obj is Literal other
295234
&& other.Quantifier.Equals(Quantifier)
296-
&& other._specifier.Equals(_specifier);
235+
&& other.Specifier.Equals(Specifier);
297236
}
298237

299238
public override int GetHashCode()
300239
{
301-
return _specifier.GetHashCode();
240+
return Specifier.GetHashCode();
302241
}
303242
}
304243
}

0 commit comments

Comments
 (0)