Consolidate parameter string splitting

This commit is contained in:
Matt Nadareski
2024-11-16 01:23:30 -05:00
parent 68fd5a2aa0
commit 595cd0d60f
5 changed files with 25 additions and 26 deletions

View File

@@ -4,6 +4,7 @@
- Be smarter about Linq usage
- Add .NET 9 to target frameworks
- Be smarter about some data types
- Consolidate parameter string splitting
### 3.2.3 (2024-11-06)

View File

@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts.Aaru
@@ -1280,12 +1278,7 @@ namespace MPF.ExecutionContexts.Aaru
return false;
// Now split the string into parts for easier validation
// https://stackoverflow.com/questions/14655023/split-a-string-that-has-white-spaces-unless-they-are-enclosed-within-quotes
parameters = parameters!.Trim();
List<string> parts = Regex.Matches(parameters, @"[\""].+?[\""]|[^ ]+", RegexOptions.Compiled)
.Cast<Match>()
.Select(m => m.Value)
.ToList();
List<string> parts = SplitParameterString(parameters!);
// Search for pre-command flags first
int start = 0;

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts
@@ -289,6 +290,24 @@ namespace MPF.ExecutionContexts
#region Parameter Parsing
/// <summary>
/// Split a parameters string into a list while taking quotes into account
/// </summary>
/// <see href="https://stackoverflow.com/questions/14655023/split-a-string-that-has-white-spaces-unless-they-are-enclosed-within-quotes"/>
protected static List<string> SplitParameterString(string parameters)
{
// Ensure the parameter string is trimmed
parameters = parameters.Trim();
// Split the string using Regex
var matches = Regex.Matches(parameters, @"[\""].+?[\""]|[^ ]+", RegexOptions.Compiled);
// Get just the values from the matches
var matchArr = new Match[matches.Count];
matches.CopyTo(matchArr, 0);
return [.. Array.ConvertAll(matchArr, m => m.Value)];
}
/// <summary>
/// Returns whether or not the selected item exists
/// </summary>

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts.DiscImageCreator
@@ -423,7 +421,7 @@ namespace MPF.ExecutionContexts.DiscImageCreator
{
if (DrivePath != null)
{
if (DrivePath.Contains(' '))
if (DrivePath.Contains(" "))
parameters.Add($"\"{DrivePath}\"");
else
parameters.Add(DrivePath);
@@ -1073,12 +1071,7 @@ namespace MPF.ExecutionContexts.DiscImageCreator
return false;
// Now split the string into parts for easier validation
// https://stackoverflow.com/questions/14655023/split-a-string-that-has-white-spaces-unless-they-are-enclosed-within-quotes
parameters = parameters!.Trim();
List<string> parts = Regex.Matches(parameters, @"[\""].+?[\""]|[^ ]+", RegexOptions.Compiled)
.Cast<Match>()
.Select(m => m.Value)
.ToList();
List<string> parts = SplitParameterString(parameters!);
// Determine what the commandline should look like given the first item
BaseCommand = parts[0];

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using SabreTools.RedumpLib.Data;
namespace MPF.ExecutionContexts.Redumper
@@ -273,7 +271,7 @@ namespace MPF.ExecutionContexts.Redumper
{
if (DriveValue != null)
{
if (DriveValue.Contains(' '))
if (DriveValue.Contains(" "))
parameters.Add($"{FlagStrings.Drive}=\"{DriveValue}\"");
else
parameters.Add($"{FlagStrings.Drive}={DriveValue}");
@@ -656,12 +654,7 @@ namespace MPF.ExecutionContexts.Redumper
return false;
// Now split the string into parts for easier validation
// https://stackoverflow.com/questions/14655023/split-a-string-that-has-white-spaces-unless-they-are-enclosed-within-quotes
parameters = parameters!.Trim();
List<string> parts = Regex.Matches(parameters, @"([a-zA-Z\-]*=)?[\""].+?[\""]|[^ ]+", RegexOptions.Compiled)
.Cast<Match>()
.Select(m => m.Value)
.ToList();
List<string> parts = SplitParameterString(parameters!);
// Setup the modes
ModeValues = [];