Skip to content

Commit 45050a0

Browse files
committed
Various code tidy ups, primarily regex related.
1 parent 9750c49 commit 45050a0

15 files changed

Lines changed: 60 additions & 47 deletions

File tree

EDDNResponder/Toolkit/LocationAugmenter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using EddiDataDefinitions;
33
using System;
44
using System.Collections.Generic;
5-
using System.Text.RegularExpressions;
65
using Utilities;
76

87
namespace EddiEddnResponder.Toolkit
@@ -350,8 +349,7 @@ internal bool ConfirmScan(string scannedBodyName)
350349
// If the body doesn't start with the system name, it should also
351350
// not match a naming pattern for a procedurally generated name.
352351
// If it does, it's (probably) in the wrong place.
353-
var isProcGenName = new Regex(@"[A-Z][A-Z]-[A-Z] [a-h][0-9]");
354-
if (!isProcGenName.IsMatch(scannedBodyName))
352+
if (!GeneratedRegex.PROC_GEN_SYSTEM_BODY().IsMatch(scannedBodyName))
355353
{
356354
return true;
357355
}

EddiCore/EDDI.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using System.Runtime.CompilerServices;
2222
using System.Runtime.InteropServices;
2323
using System.Text;
24-
using System.Text.RegularExpressions;
2524
using System.Threading;
2625
using System.Threading.Tasks;
2726
using Utilities;
@@ -95,9 +94,9 @@ private void SetGameVersion(string v)
9594
// The game version is typically a Semantic Version string (e.g. "4.0.0.102")
9695
// but may sometimes include additional information (e.g. "4.0.0.32 (Alpha Phase 4 Hotfix 9)")
9796
// or may be missing a Semantic Version altogether (e.g. "Fleet Carriers Update - Patch 11")
98-
var versionRegex = new Regex(@"^(?<engine>0|[1-9]\d*)\.(?<major>0|[1-9]\d*)(?:\.(?<minor>\d*))?(?:\.(?<patch>\d*))?");
99-
GameVersion = !string.IsNullOrEmpty(v) &&
100-
System.Version.TryParse(versionRegex.Match(v).Value, out var versionResult)
97+
GameVersion = !string.IsNullOrEmpty( v ) &&
98+
System.Version.TryParse( GeneratedRegex.SemanticVersionRegex().Match( v ).Value,
99+
out var versionResult )
101100
? versionResult
102101
: null;
103102

EddiCore/Upgrader/EddiUpgrader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Net.Http;
88
using System.Net.Http.Headers;
99
using System.Reflection;
10-
using System.Text.RegularExpressions;
1110
using System.Threading.Tasks;
1211
using Utilities;
1312

@@ -95,7 +94,7 @@ private static void ProcessRelease ( JObject release )
9594
{
9695
// Get the version information, removing any prefixing description and separator
9796
var version = release.Value<string>("tag_name");
98-
version = Regex.Replace( version, @"(^\w+[\\\/:_\-\|+=#@&%!~^*])+", "" );
97+
version = GeneratedRegex.EddiVersionRegex().Match( version ).Value;
9998

10099
if ( release["assets"] is JArray assets )
101100
{

GalnetMonitor/GalnetFeedItemNormalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static string RemoveControlChars ( string value )
101101

102102
private static string StripDoubleOrMoreWhiteSpace ( string value )
103103
{
104-
return GeneratedRegex.WhiteMultiSpaceRegex().Replace( value, " " );
104+
return GeneratedRegex.WhiteSpaceTwoOrMoreRegex().Replace( value, " " );
105105
}
106106

107107
private static string StripHTML ( string value )

SpeechResponder/CustomFunctions/ShipCallsign.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System;
99
using System.Linq;
1010
using System.Text;
11-
using System.Text.RegularExpressions;
1211

1312
namespace EddiSpeechResponder.CustomFunctions
1413
{
@@ -92,14 +91,29 @@ internal static string phoneticCallsign(Ship ship, string id)
9291
return sb.ToString();
9392
}
9493

95-
private static string Get3LeadingCharacters(string input)
94+
private static string Get3LeadingCharacters ( string input )
9695
{
97-
// Obtain the first three characters of the input string, zero padded and converted to ICAO (e.g. "A" becomes "Alpha Zero Zero")
98-
return SpeechConversions.ICAO(new Regex("[^a-zA-Z0-9]")
99-
.Replace(input, "")
100-
.ToUpperInvariant()
101-
.PadRight(3, '0')
102-
.Substring(0, 3));
96+
Span<char> buffer = stackalloc char[3];
97+
var count = 0;
98+
99+
if ( !string.IsNullOrEmpty( input ) )
100+
{
101+
foreach ( var c in input )
102+
{
103+
if ( char.IsLetterOrDigit( c ) )
104+
{
105+
buffer[ count++ ] = char.ToUpperInvariant( c );
106+
if ( count == 3 ) { break; }
107+
}
108+
}
109+
}
110+
111+
while ( count < 3 )
112+
{
113+
buffer[ count++ ] = '0';
114+
}
115+
116+
return SpeechConversions.ICAO( new string( buffer ) );
103117
}
104118
}
105119
}

SpeechResponder/ScriptResolverService/ScriptResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static string resolveFromValue(string script, IContext context, bool isTo
131131
// Tidy up the output script
132132
if ( isTopLevelScript )
133133
{
134-
result = GeneratedRegex.WhiteMultiSpaceRegex().Replace( result, " " )
134+
result = GeneratedRegex.WhiteSpaceTwoOrMoreRegex().Replace( result, " " )
135135
.Replace( " ,", "," )
136136
.Replace( " .", "." ).Trim();
137137
result = result.Trim() == "" ? null : result.Trim();

SpeechService/SpeechConversions/PhoneticBody.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static string getPhoneticBody(string body, bool useICAO = false)
6868
}
6969
}
7070

71-
return GeneratedRegex.WhiteMultiSpaceRegex().Replace(string.Join(" ", results), " ");
71+
return GeneratedRegex.WhiteSpaceTwoOrMoreRegex().Replace(string.Join(" ", results), " ");
7272
}
7373

7474
private static string GetLastPart(Match match, int i, int j)

Tests/EddiVoiceAttackService/MessageSerializerTests.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void Serialize_EventMessage_IncludesEventData ()
208208
}
209209

210210
[ TestMethod ]
211-
public void Serialize_EventMessage_WithModifyingCollection_DoesNotThrow ()
211+
public async Task Serialize_EventMessage_WithModifyingCollection_DoesNotThrow ()
212212
{
213213
// Arrange: Create a mutable dictionary that simulates concurrent modification
214214
var payload = new Dictionary<string, object> { { "key1", "value1" } };
@@ -226,6 +226,7 @@ public void Serialize_EventMessage_WithModifyingCollection_DoesNotThrow ()
226226
};
227227

228228
// Act: Modify the dictionary in a separate task while serializing
229+
Exception exception = null;
229230
var serializationTask = Task.Run( () =>
230231
{
231232
// Serialize multiple times to increase chance of collision
@@ -252,14 +253,18 @@ public void Serialize_EventMessage_WithModifyingCollection_DoesNotThrow ()
252253
// Assert: Both tasks should complete without exception
253254
try
254255
{
255-
Task.WaitAll( [ serializationTask, modificationTask ], TimeSpan.FromSeconds( 5 ) );
256-
Assert.IsTrue( serializationTask.IsCompletedSuccessfully );
257-
Assert.IsTrue( modificationTask.IsCompletedSuccessfully );
256+
await Task.WhenAll( serializationTask, modificationTask );
258257
}
259-
catch ( AggregateException ex )
258+
catch ( Exception ex )
260259
{
261-
Assert.Fail( $"Serialization failed during concurrent modification: {ex.InnerException?.Message}" );
260+
// Capture for debugging, but don't assert here
261+
exception = ex;
262262
}
263+
264+
// Assert (outside the catch)
265+
Assert.IsNull( exception, $"Serialization failed during concurrent modification: {exception}" );
266+
Assert.IsTrue( serializationTask.IsCompletedSuccessfully );
267+
Assert.IsTrue( modificationTask.IsCompletedSuccessfully );
263268
}
264269

265270
[ TestMethod ]

Tests/ScriptResolverTest.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8-
using System.Text.RegularExpressions;
98
using System.Threading;
109
using System.Windows;
1110

@@ -156,12 +155,6 @@ public void TestResolverRecursedCustomFunctions()
156155
Assert.IsEmpty(results);
157156
}
158157

159-
[TestMethod]
160-
public void TestResolverCallsign()
161-
{
162-
Assert.AreEqual( "ABC", new Regex("[^a-zA-Z0-9]").Replace("a-b. c", "").ToUpperInvariant().Substring(0, 3) );
163-
}
164-
165158
[TestMethod]
166159
public void TestUpgradeScript_FromDefault()
167160
{

Tests/SpeechUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void start()
2424

2525
private string CondenseSpaces(string s)
2626
{
27-
return GeneratedRegex.WhiteMultiSpaceRegex().Replace(s, " ");
27+
return GeneratedRegex.WhiteSpaceRegex().Replace(s, " ");
2828
}
2929

3030
[TestMethod]

0 commit comments

Comments
 (0)