Replace some uses of Regex.Replace

This commit is contained in:
Matt Nadareski
2024-11-22 11:50:07 -05:00
parent c7b6b08397
commit e5b02c27a8
5 changed files with 41 additions and 13 deletions

View File

@@ -22,6 +22,7 @@
- Don't preemptively sort protections
- Remove unncessary .NET Framework 4.0 gating
- Improve parameters for default output path
- Replace some uses of Regex.Replace
### 3.2.3 (2024-11-06)

View File

@@ -5,6 +5,7 @@ using System.IO;
using System.IO.Compression;
#endif
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SabreTools.RedumpLib.Data;
@@ -579,8 +580,7 @@ namespace MPF.Processors
hex = hex.Substring(0, trimLength);
// TODO: Check for non-zero values in discarded PIC
return Regex.Replace(hex, ".{32}", "$0\n", RegexOptions.Compiled);
return SplitString(hex, 32);
}
catch
{
@@ -643,6 +643,32 @@ namespace MPF.Processors
}
}
/// <summary>
/// Split a string with newlines every <paramref name="count"/> characters
/// </summary>
protected static string SplitString(string? str, int count, bool trim = false)
{
// Ignore invalid inputs
if (str == null)
return string.Empty;
if (count < 1)
return str;
// Build the output string
var sb = new StringBuilder();
for (int i = 0; i < str.Length; i += count)
{
string line = str.Substring(i, count);
if (trim)
line = line.Trim();
sb.Append(line);
sb.Append('\n');
}
return sb.ToString();
}
#endregion
}
}

View File

@@ -173,9 +173,9 @@ namespace MPF.Processors
if (hex == null)
return null;
// First separate into lines then into blocks of 4 hex digits
string bca = Regex.Replace(hex, ".{32}", "$0\n");
return Regex.Replace(bca, "[0-9a-fA-F]{4}", "$0 ");
// Separate into blocks of 4 hex digits and then lines
string bca = Regex.Replace(hex, "[0-9a-fA-F]{4}", "$0 ");
return SplitString(bca, 36, trim: true);
}
catch
{

View File

@@ -68,10 +68,7 @@ namespace MPF.Processors
if (id != null)
info.Extras!.DiscID = id.ToUpperInvariant().Substring(0, 24) + "XXXXXXXX";
if (string.IsNullOrEmpty(info.Extras!.PIC) && !string.IsNullOrEmpty(pic))
{
pic = Regex.Replace(pic, ".{32}", "$0\n");
info.Extras.PIC = pic;
}
info.Extras.PIC = SplitString(pic, 32);
}
}

View File

@@ -32,14 +32,18 @@ namespace MPF.Processors
return null;
// Convert ASCII to byte via lookup table
int[] hexLookup = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F];
int[] hexLookup =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
];
byte[] byteArray = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
{
// Convert next two chars to ASCII value relative to '0'
int a = Char.ToUpper(hexString[i]) - '0';
int b = Char.ToUpper(hexString[i + 1]) - '0';
int a = Char.ToUpperInvariant(hexString[i]) - '0';
int b = Char.ToUpperInvariant(hexString[i + 1]) - '0';
// Ensure hex string only has '0' through '9' and 'A' through 'F' (case insensitive)
if ((a < 0 || b < 0 || a > 22 || b > 22) || (a > 10 && a < 17) || (b > 10 && b < 17))