From ada3297de708cf8da795c844b4a95932ec300c75 Mon Sep 17 00:00:00 2001 From: laurentiu021 Date: Thu, 11 Jun 2026 15:44:55 +0300 Subject: [PATCH] refactor: use [GeneratedRegex] for SFC/DISM/speedtest literal patterns --- SysManager/SysManager/ViewModels/CleanupViewModel.cs | 12 ++++++++++-- .../SysManager/ViewModels/SpeedTestViewModel.cs | 7 ++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/SysManager/SysManager/ViewModels/CleanupViewModel.cs b/SysManager/SysManager/ViewModels/CleanupViewModel.cs index dd0e050..a1de0af 100644 --- a/SysManager/SysManager/ViewModels/CleanupViewModel.cs +++ b/SysManager/SysManager/ViewModels/CleanupViewModel.cs @@ -267,7 +267,7 @@ void Collect(PowerShellLine l) if (l.Kind == OutputKind.Output) captured.Add(l.Text); if (l.Text.Contains('%') || l.Text.Contains("complete", StringComparison.OrdinalIgnoreCase)) { - var m = Regex.Match(l.Text, @"(\d+)\s*%"); + var m = SfcPercentRegex().Match(l.Text); if (m.Success && int.TryParse(m.Groups[1].Value, out var pct) && pct is >= 0 and <= 100) { Progress = pct; @@ -356,7 +356,7 @@ void Collect(PowerShellLine l) if (l.Kind == OutputKind.Output) captured.Add(l.Text); if (l.Text.Contains('%')) { - var m = Regex.Match(l.Text, @"([\d.]+)%"); + var m = DismPercentRegex().Match(l.Text); if (m.Success && double.TryParse(m.Groups[1].Value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var pct) && pct is >= 0 and <= 100) { Progress = (int)pct; @@ -410,4 +410,12 @@ private void Cancel() _sfcCts?.Cancel(); _dismCts?.Cancel(); } + + // SFC reports progress as a whole-number percentage, e.g. "50 %". + [GeneratedRegex(@"(\d+)\s*%")] + private static partial Regex SfcPercentRegex(); + + // DISM reports progress as a decimal percentage, e.g. "50.0%". + [GeneratedRegex(@"([\d.]+)%")] + private static partial Regex DismPercentRegex(); } diff --git a/SysManager/SysManager/ViewModels/SpeedTestViewModel.cs b/SysManager/SysManager/ViewModels/SpeedTestViewModel.cs index cf8e119..4d01f2c 100644 --- a/SysManager/SysManager/ViewModels/SpeedTestViewModel.cs +++ b/SysManager/SysManager/ViewModels/SpeedTestViewModel.cs @@ -3,6 +3,7 @@ // License: MIT using System.Collections.ObjectModel; +using System.Text.RegularExpressions; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Serilog; @@ -173,10 +174,14 @@ private async Task ClearOoklaHistoryAsync() private static int? ParseServerId(string option) { if (option.StartsWith("Auto")) return null; - var match = System.Text.RegularExpressions.Regex.Match(option, @"ID:\s*(\d+)"); + var match = ServerIdRegex().Match(option); return match.Success ? int.Parse(match.Groups[1].Value) : null; } + // Speedtest server options are formatted "Name (ID: 1234)". + [GeneratedRegex(@"ID:\s*(\d+)")] + private static partial Regex ServerIdRegex(); + protected override void Dispose(bool disposing) { if (disposing)