From e188f8a952c807e7fac56b97641c3739bc6dd88a Mon Sep 17 00:00:00 2001 From: Piotrekol Date: Sat, 24 Feb 2018 21:48:02 +0100 Subject: [PATCH] Make tokens case insensitive --- osu!StreamCompanion/Code/Helpers/Helpers.cs | 61 ++++++++++++++++++- .../MapDataParsers/Parser1/OutputPattern.cs | 3 +- .../MapDataParsers/Parser1/PatternEdit.cs | 11 ++-- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/osu!StreamCompanion/Code/Helpers/Helpers.cs b/osu!StreamCompanion/Code/Helpers/Helpers.cs index 0e1dfbaa..e549fdb2 100644 --- a/osu!StreamCompanion/Code/Helpers/Helpers.cs +++ b/osu!StreamCompanion/Code/Helpers/Helpers.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Threading.Tasks; using CollectionManager.DataTypes; @@ -12,7 +13,7 @@ public static T[] SubArray(this T[] data, int index, int length) Array.Copy(data, index, result, 0, length); return result; } - + [DebuggerStepThrough()] public static DateTime GetDateFromVersionString(string version) { try @@ -26,7 +27,7 @@ public static DateTime GetDateFromVersionString(string version) System.Globalization.CultureInfo.InvariantCulture); } } - + [DebuggerStepThrough()] public static T ExecWithTimeout(Func function, int timeout = 10000) { var task = Task.Factory.StartNew(function); @@ -35,7 +36,7 @@ public static T ExecWithTimeout(Func function, int timeout = 10000) else return default(T); } - + [DebuggerStepThrough()] public static OppaiSharp.Mods Convert(this Mods mods) { OppaiSharp.Mods result = OppaiSharp.Mods.NoMod; @@ -61,5 +62,59 @@ public static OppaiSharp.Mods Convert(this Mods mods) result |= OppaiSharp.Mods.SpunOut; return result; } + /// + /// Returns a new string in which all occurrences of a specified string in the current instance are replaced with another + /// specified string acording the type of search to use for the specified string. + /// + /// The string performing the replace method. + /// The string to be replaced. + /// The string replace all occurrances of oldValue. + /// One of the enumeration values that specifies the rules for the search. + /// A string that is equivalent to the current string except that all instances of oldValue are replaced with newValue. + /// If oldValue is not found in the current instance, the method returns the current instance unchanged. + [DebuggerStepThrough()] + public static string Replace(this string str, + string oldValue, string @newValue, + StringComparison comparisonType) + { + + //Check inputs + //Same as original .NET C# string.Replace behaviour + if (str == null) + { + throw new ArgumentNullException(nameof(str)); + } + if (str.Length == 0) + { + return str; + } + if (oldValue == null) + { + throw new ArgumentNullException(nameof(oldValue)); + } + if (oldValue.Length == 0) + { + throw new ArgumentException("String cannot be of zero length."); + } + + @newValue = @newValue ?? string.Empty; + + const int valueNotFound = -1; + int foundAt, startSearchFromIndex = 0; + while ((foundAt = str.IndexOf(oldValue, startSearchFromIndex, comparisonType)) != valueNotFound) + { + + str = str.Remove(foundAt, oldValue.Length) + .Insert(foundAt, @newValue); + + startSearchFromIndex = foundAt + @newValue.Length; + if (startSearchFromIndex == str.Length) + { + break; + } + } + + return str; + } } } diff --git a/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/OutputPattern.cs b/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/OutputPattern.cs index 7d14daa5..0435327a 100644 --- a/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/OutputPattern.cs +++ b/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/OutputPattern.cs @@ -6,6 +6,7 @@ using System.Linq; using CollectionManager.Annotations; using osu_StreamCompanion.Code.Core.DataTypes; +using osu_StreamCompanion.Code.Helpers; namespace osu_StreamCompanion.Code.Modules.MapDataParsers.Parser1 { @@ -97,7 +98,7 @@ public string GetFormatedPattern() string toFormat = this.Pattern; foreach (var r in Replacements) { - toFormat = toFormat.Replace(r.Key, r.Value); + toFormat = toFormat.Replace(r.Key, r.Value, StringComparison.InvariantCultureIgnoreCase); } return toFormat; } diff --git a/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/PatternEdit.cs b/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/PatternEdit.cs index 9b43ec6e..032b3333 100644 --- a/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/PatternEdit.cs +++ b/osu!StreamCompanion/Code/Modules/MapDataParsers/Parser1/PatternEdit.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Windows.Forms; using osu_StreamCompanion.Code.Core.DataTypes; +using osu_StreamCompanion.Code.Helpers; namespace osu_StreamCompanion.Code.Modules.MapDataParsers.Parser1 { @@ -33,7 +34,7 @@ public OutputPattern Current comboBox_saveEvent.SelectedItem = SaveEvents.First(s => s.Value == value.SaveEvent).Key; textBox_formating.Text = value?.Pattern ?? ""; textBox_FileName.Text = value?.Name ?? ""; - + })); } } @@ -72,7 +73,7 @@ private void Save() Current.SaveEvent = SaveEvents.First(s => s.Key == (string)comboBox_saveEvent.SelectedItem).Value; } } - + private void button_Click(object sender, EventArgs e) { if (sender == button_save) @@ -107,7 +108,7 @@ private void textBox_formating_TextChanged(object sender, EventArgs e) return; var isMemoryPattern = Current.MemoryFormatTokens.Any(textBox_formating.Text.Contains); label_warning.Visible = isMemoryPattern; - comboBox_saveEvent.SelectedItem = isMemoryPattern? "Playing" : comboBox_saveEvent.SelectedItem; + comboBox_saveEvent.SelectedItem = isMemoryPattern ? "Playing" : comboBox_saveEvent.SelectedItem; comboBox_saveEvent.Enabled = !isMemoryPattern; @@ -118,11 +119,11 @@ private void textBox_formating_TextChanged(object sender, EventArgs e) var toFormat = textBox_formating.Text; foreach (var r in _replacements) { - toFormat = toFormat.Replace(r.Key, r.Value); + toFormat = toFormat.Replace(r.Key, r.Value, StringComparison.InvariantCultureIgnoreCase); } foreach (var r in _liveReplacements) { - toFormat = toFormat.Replace(r.Key, r.Value); + toFormat = toFormat.Replace(r.Key, r.Value, StringComparison.InvariantCultureIgnoreCase); } textBox_preview.Text = toFormat; }