diff --git a/CHANGELIST.md b/CHANGELIST.md index a2dd6212..f8c09843 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -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) diff --git a/MPF.Processors/BaseProcessor.cs b/MPF.Processors/BaseProcessor.cs index 540d3e81..3fcc1847 100644 --- a/MPF.Processors/BaseProcessor.cs +++ b/MPF.Processors/BaseProcessor.cs @@ -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 } } + /// + /// Split a string with newlines every characters + /// + 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 } } diff --git a/MPF.Processors/CleanRip.cs b/MPF.Processors/CleanRip.cs index 2f3aea5d..7feed005 100644 --- a/MPF.Processors/CleanRip.cs +++ b/MPF.Processors/CleanRip.cs @@ -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 { diff --git a/MPF.Processors/PS3CFW.cs b/MPF.Processors/PS3CFW.cs index 09e62ac6..4d336235 100644 --- a/MPF.Processors/PS3CFW.cs +++ b/MPF.Processors/PS3CFW.cs @@ -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); } } diff --git a/MPF.Processors/ProcessingTool.cs b/MPF.Processors/ProcessingTool.cs index 2a724eb1..8e260b80 100644 --- a/MPF.Processors/ProcessingTool.cs +++ b/MPF.Processors/ProcessingTool.cs @@ -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))