Skip to content

Commit

Permalink
Make tokens case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Feb 24, 2018
1 parent 74b0b72 commit e188f8a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
61 changes: 58 additions & 3 deletions osu!StreamCompanion/Code/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using CollectionManager.DataTypes;

Expand All @@ -12,7 +13,7 @@ public static T[] SubArray<T>(this T[] data, int index, int length)
Array.Copy(data, index, result, 0, length);
return result;
}

[DebuggerStepThrough()]
public static DateTime GetDateFromVersionString(string version)
{
try
Expand All @@ -26,7 +27,7 @@ public static DateTime GetDateFromVersionString(string version)
System.Globalization.CultureInfo.InvariantCulture);
}
}

[DebuggerStepThrough()]
public static T ExecWithTimeout<T>(Func<T> function, int timeout = 10000)
{
var task = Task<T>.Factory.StartNew(function);
Expand All @@ -35,7 +36,7 @@ public static T ExecWithTimeout<T>(Func<T> function, int timeout = 10000)
else
return default(T);
}

[DebuggerStepThrough()]
public static OppaiSharp.Mods Convert(this Mods mods)
{
OppaiSharp.Mods result = OppaiSharp.Mods.NoMod;
Expand All @@ -61,5 +62,59 @@ public static OppaiSharp.Mods Convert(this Mods mods)
result |= OppaiSharp.Mods.SpunOut;
return result;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="str">The string performing the replace method.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string replace all occurrances of oldValue.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search. </param>
/// <returns>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. </returns>
[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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 ?? "";

}));
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;


Expand All @@ -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;
}
Expand Down

0 comments on commit e188f8a

Please sign in to comment.