Skip to content

Commit b6b3061

Browse files
committed
Regex performance changes and code tidy
1 parent b8a99b6 commit b6b3061

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+257
-329
lines changed

src/ZippyNeuron.Metarwiz.Console/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Text;
34
using ZippyNeuron.Metarwiz.Parser;
45
using ZippyNeuron.Metarwiz.Parser.Metars;
56
using ZippyNeuron.Metarwiz.Parser.Remarks;
7+
using ZippyNeuron.Metarwiz.Parser.Types;
68

79
namespace ZippyNeuron.Metarwiz.Console;
810

@@ -54,6 +56,11 @@ static void Main(string[] args)
5456
Out("Metar (Original)", $" | {metar}");
5557
Out("Metar (Processed)", $" | {metarwizResult}");
5658
Out("Metar (Checksum)", $" | Passed: {metarwizResult.ToString() == metar}");
59+
60+
Out("Enum CloudType", " | " + string.Join("|", Enum.GetNames<CloudType>()));
61+
Out("Enum CloudFormType", " | " + string.Join("|", Enum.GetNames<CloudFormType>()));
62+
Out("Enum RecentWeatherType", " | " + string.Join("|", Enum.GetNames<RecentWeatherType>()));
63+
Out("Enum WeatherType", " | " + string.Join("|", Enum.GetNames<WeatherType>()));
5764
}
5865

5966
private static void Out(string label, string value)

src/ZippyNeuron.Metarwiz.Console/ZippyNeuron.Metarwiz.Console.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net9.0</TargetFramework>
66
<RootNamespace>ZippyNeuron.Metarwiz.Console</RootNamespace>
7-
<LangVersion>11</LangVersion>
87
</PropertyGroup>
98

109
<ItemGroup>

src/ZippyNeuron.Metarwiz/IMetarwiz.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ namespace ZippyNeuron.Metarwiz;
55
public interface IMetarwiz
66
{
77
public MetarwizResult Parse(string metar, string tag);
8-
98
public string ToString();
109
}

src/ZippyNeuron.Metarwiz/Parser/Helpers/MetarConversion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ internal static class MetarConversion
99
public static decimal StatuteToNauticalMiles = 0.868976m;
1010
public static decimal NauticalToStatuteMiles = 1.150780m;
1111
public static decimal MetersPerMile = 1609.34m;
12-
}
12+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
namespace ZippyNeuron.Metarwiz.Parser;
22

3-
public interface IMetarItem
4-
{
5-
6-
}
3+
public interface IMetarItem { }

src/ZippyNeuron.Metarwiz/Parser/IMetarParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ namespace ZippyNeuron.Metarwiz.Parser;
55
internal interface IMetarParser
66
{
77
public IEnumerable<IMetarItem> Items { get; }
8+
public MetarInfo Info { get; }
89

910
public void Parse();
10-
11-
public MetarInfo Info { get; }
1211
}

src/ZippyNeuron.Metarwiz/Parser/MetarInfo.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,21 @@ internal MetarInfo(string metar, string tag)
3030

3131
public bool HasRemarks => !string.IsNullOrEmpty(Remarks);
3232

33-
public override string ToString()
34-
{
35-
return $"{Metar}{((HasRemarks) ? String.Concat(" ", Remarks) : String.Empty)}{Terminator}";
36-
}
33+
public override string ToString() =>
34+
$"{Metar}{((HasRemarks) ? string.Concat(" ", Remarks) : string.Empty)}{Terminator}";
3735

38-
private string RemoveTerminator(string metar)
39-
{
40-
return metar
36+
private static string RemoveTerminator(string metar) =>
37+
metar
4138
.Trim()
4239
.TrimEnd(TerminatorSymbol.ToCharArray())
4340
.Trim();
44-
}
4541

46-
private string GetRemarks(string metar)
42+
private static string GetRemarks(string metar)
4743
{
4844
int start = metar.IndexOf(RemarksTag, StringComparison.Ordinal);
4945

5046
if (start < 0)
51-
return String.Empty;
47+
return string.Empty;
5248

5349
return metar
5450
.Substring(start)
@@ -62,7 +58,7 @@ private string RemoveRemarks(string metar)
6258
if (start < 0)
6359
return metar;
6460

65-
return metar.Substring(0, start - 1)
61+
return metar[..(start - 1)]
6662
.Trim();
6763
}
68-
}
64+
}

src/ZippyNeuron.Metarwiz/Parser/MetarItem.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
public class MetarItem : IMetarItem
44
{
5-
public override string ToString()
6-
{
7-
return string.Empty;
8-
}
9-
}
5+
public override string ToString() =>
6+
string.Empty;
7+
}

src/ZippyNeuron.Metarwiz/Parser/MetarParser.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ZippyNeuron.Metarwiz.Parser;
88

99
internal sealed class MetarParser : IMetarParser
1010
{
11-
private readonly List<MetarParserItem> _items = new();
11+
private readonly List<MetarParserItem> _items = [];
1212
private readonly MetarInfo _metarInfo;
1313

1414
internal MetarParser(string metar, string tag = null)
@@ -20,11 +20,10 @@ internal MetarParser(string metar, string tag = null)
2020

2121
public MetarInfo Info => _metarInfo;
2222

23-
public IEnumerable<IMetarItem> Items => _items
23+
public IEnumerable<IMetarItem> Items => [.. _items
2424
.OrderBy(i => i.Index)
2525
.Select((value) => value.Item)
26-
.Cast<IMetarItem>()
27-
.ToList();
26+
.Cast<IMetarItem>()];
2827

2928
public void Parse()
3029
{
@@ -41,9 +40,7 @@ private void ParseTypes(string metar, IEnumerable<Type> types)
4140

4241
foreach (var type in types)
4342
{
44-
var pattern = GetMatchPattern(type);
45-
46-
var matchCollection = Regex.Matches(metarCopy, pattern, RegexOptions.None);
43+
var matchCollection = GetRegex(type).Matches(metarCopy);
4744

4845
if (matchCollection.Count <= 0) continue;
4946

@@ -69,9 +66,6 @@ private void ParseTypes(string metar, IEnumerable<Type> types)
6966
}
7067
}
7168

72-
private string GetMatchPattern(Type type)
73-
{
74-
return type.GetProperty("Pattern", BindingFlags.Static | BindingFlags.NonPublic)
75-
?.GetValue(null, null) as string;
76-
}
77-
}
69+
private static Regex GetRegex(Type type) =>
70+
(Regex)type.GetMethod("Pattern", BindingFlags.Static | BindingFlags.NonPublic)?.Invoke(null, null);
71+
}

src/ZippyNeuron.Metarwiz/Parser/MetarParserFactory.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace ZippyNeuron.Metarwiz.Parser;
77

88
internal static class MetarParserFactory
99
{
10-
public static MetarItem Create(Type t, Match m)
11-
{
12-
return (MetarItem)Activator.CreateInstance(t, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { m }, CultureInfo.InvariantCulture);
13-
}
10+
public static MetarItem Create(Type t, Match m) =>
11+
(MetarItem)Activator
12+
.CreateInstance(t, BindingFlags.Instance | BindingFlags.NonPublic, null, [m], CultureInfo.InvariantCulture);
1413
}

0 commit comments

Comments
 (0)