Extract some extensions for string `OrdinalIgnoreCase` comparisons

`Contains`/`Equals`/`StartsWith`
refactored a few call-sites while I was at it
This commit is contained in:
YoshiRulz 2025-01-29 21:49:49 +10:00
parent a9fd86689e
commit 96207e80ea
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
42 changed files with 155 additions and 166 deletions

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Net.Sockets;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.Common
{
@ -191,7 +192,7 @@ namespace BizHawk.Client.Common
? ((string?, string?)?) null // don't bother
: (httpClientURIGET, httpClientURIPOST);
var audiosync = result.GetValueForOption(OptionAVDumpAudioSync)?.Equals("true", StringComparison.OrdinalIgnoreCase);
var audiosync = result.GetValueForOption(OptionAVDumpAudioSync)?.EqualsIgnoreCase("true");
List<(string Key, string Value)>? userdataUnparsedPairs = null;
if (result.GetValueForOption(OptionUserdataUnparsedPairs) is string list1)

View File

@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common.StringExtensions;
using Newtonsoft.Json;
namespace BizHawk.Client.Common
@ -67,7 +70,7 @@ namespace BizHawk.Client.Common
{
if (!Frozen)
{
return recentlist.RemoveAll(recent => string.Equals(newFile, recent, StringComparison.OrdinalIgnoreCase)) != 0; // none removed => return false
return recentlist.RemoveAll(newFile.EqualsIgnoreCase) is not 0; // none removed => return false
}
return false;

View File

@ -401,7 +401,7 @@ namespace BizHawk.Client.Common
return (int)CorePriority.UserPreference;
}
if (string.Equals(c.Name, dbForcedCoreName, StringComparison.OrdinalIgnoreCase))
if (c.Name.EqualsIgnoreCase(dbForcedCoreName))
{
return (int)CorePriority.GameDbPreference;
}

View File

@ -24,7 +24,7 @@ namespace BizHawk.Client.Common
var globalBase = collection[PathEntryCollection.GLOBAL, "Base"].Path;
// if %exe% prefixed then substitute exe path and repeat
if (globalBase.StartsWith("%exe%", StringComparison.OrdinalIgnoreCase))
if (globalBase.StartsWithIgnoreCase("%exe%"))
{
globalBase = PathUtils.ExeDirectoryPath + globalBase.Substring(5);
}

View File

@ -74,7 +74,7 @@ namespace BizHawk.Client.Common
{
Result.Movie.Comments.Add(ParseHeader(line, "comment"));
}
else if (line.StartsWith("guid", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("guid"))
{
// We no longer care to keep this info
}

View File

@ -1,5 +1,6 @@
using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores;
using BizHawk.Emulation.Cores.Nintendo.NES;
@ -41,7 +42,7 @@ namespace BizHawk.Client.Common
{
ImportInputFrame(line);
}
else if (line.StartsWith("sub", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("sub"))
{
var subtitle = ImportTextSubtitle(line);
@ -50,11 +51,11 @@ namespace BizHawk.Client.Common
Result.Movie.Subtitles.AddFromString(subtitle);
}
}
else if (line.StartsWith("emuversion", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("emuversion"))
{
Result.Movie.Comments.Add($"{EmulationOrigin} {emulator} version {ParseHeader(line, "emuVersion")}");
}
else if (line.StartsWith("version", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("version"))
{
string version = ParseHeader(line, "version");
@ -67,15 +68,15 @@ namespace BizHawk.Client.Common
Result.Movie.Comments.Add($"{MovieOrigin} .fm2 version 3");
}
}
else if (line.StartsWith("romfilename", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("romfilename"))
{
Result.Movie.HeaderEntries[HeaderKeys.GameName] = ParseHeader(line, "romFilename");
}
else if (line.StartsWith("cdgamename", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("cdgamename"))
{
Result.Movie.HeaderEntries[HeaderKeys.GameName] = ParseHeader(line, "cdGameName");
}
else if (line.StartsWith("romchecksum", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("romchecksum"))
{
string blob = ParseHeader(line, "romChecksum");
byte[] md5 = DecodeBlob(blob);
@ -88,19 +89,19 @@ namespace BizHawk.Client.Common
Result.Warnings.Add("Bad ROM checksum.");
}
}
else if (line.StartsWith("comment author", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("comment author"))
{
Result.Movie.HeaderEntries[HeaderKeys.Author] = ParseHeader(line, "comment author");
}
else if (line.StartsWith("rerecordcount", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("rerecordcount"))
{
Result.Movie.Rerecords = (ulong) (int.TryParse(ParseHeader(line, "rerecordCount"), out var rerecordCount) ? rerecordCount : default);
}
else if (line.StartsWith("guid", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("guid"))
{
// We no longer care to keep this info
}
else if (line.StartsWith("startsfromsavestate", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("startsfromsavestate"))
{
// If this movie starts from a savestate, we can't support it.
if (ParseHeader(line, "StartsFromSavestate") == "1")
@ -109,11 +110,11 @@ namespace BizHawk.Client.Common
break;
}
}
else if (line.StartsWith("palflag", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("palflag"))
{
Result.Movie.HeaderEntries[HeaderKeys.Pal] = ParseHeader(line, "palFlag");
}
else if (line.StartsWith("port0", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("port0"))
{
if (!isFourScore && ParseHeader(line, "port0") == "1")
{
@ -122,7 +123,7 @@ namespace BizHawk.Client.Common
_deck.ControllerDef.BuildMnemonicsCache(Result.Movie.SystemID);
}
}
else if (line.StartsWith("port1", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("port1"))
{
if (!isFourScore && ParseHeader(line, "port1") == "1")
{
@ -131,14 +132,14 @@ namespace BizHawk.Client.Common
_deck.ControllerDef.BuildMnemonicsCache(Result.Movie.SystemID);
}
}
else if (line.StartsWith("port2", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("port2"))
{
if (ParseHeader(line, "port2") == "1")
{
Result.Warnings.Add("Famicom port detected but not yet supported, ignoring");
}
}
else if (line.StartsWith("fourscore", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("fourscore"))
{
isFourScore = ParseHeader(line, "fourscore") == "1";
if (isFourScore)
@ -244,7 +245,7 @@ namespace BizHawk.Client.Common
}
// base64
if (!blob.StartsWith("base64:", StringComparison.OrdinalIgnoreCase))
if (!blob.StartsWithIgnoreCase("base64:"))
{
return null;
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text.RegularExpressions;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.Common
{
@ -52,15 +53,15 @@ namespace BizHawk.Client.Common
// try to generate a matching hash from the original ROM
if (Result.Movie.HeaderEntries.TryGetValue(HeaderKeys.Crc32, out string crcHash))
{
hash = PromptForRom(data => string.Equals(CRC32Checksum.ComputeDigestHex(data), crcHash, StringComparison.OrdinalIgnoreCase));
hash = PromptForRom(data => CRC32Checksum.ComputeDigestHex(data).EqualsIgnoreCase(crcHash));
}
else if (Result.Movie.HeaderEntries.TryGetValue(HeaderKeys.Md5, out string md5Hash))
{
hash = PromptForRom(data => string.Equals(MD5Checksum.ComputeDigestHex(data), md5Hash, StringComparison.OrdinalIgnoreCase));
hash = PromptForRom(data => MD5Checksum.ComputeDigestHex(data).EqualsIgnoreCase(md5Hash));
}
else if (Result.Movie.HeaderEntries.TryGetValue(HeaderKeys.Sha256, out string sha256Hash))
{
hash = PromptForRom(data => string.Equals(SHA256Checksum.ComputeDigestHex(data), sha256Hash, StringComparison.OrdinalIgnoreCase));
hash = PromptForRom(data => SHA256Checksum.ComputeDigestHex(data).EqualsIgnoreCase(sha256Hash));
}
if (hash is not null)

View File

@ -1,3 +1,4 @@
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.PCEngine;
@ -43,7 +44,7 @@ namespace BizHawk.Client.Common.movie.import
{
ImportTextFrame(line);
}
else if (line.StartsWith("ports", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("ports"))
{
var portNumStr = ParseHeader(line, "ports");
if (int.TryParse(portNumStr, out int ports))
@ -82,32 +83,32 @@ namespace BizHawk.Client.Common.movie.import
ss.Port5);
}
}
else if (line.StartsWith("pcecd", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("pcecd"))
{
Result.Movie.HeaderEntries[HeaderKeys.Platform] = VSystemID.Raw.PCECD;
}
else if (line.StartsWith("emuversion", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("emuversion"))
{
Result.Movie.Comments.Add($"{EmulationOrigin} Mednafen/PCEjin version {ParseHeader(line, "emuVersion")}");
}
else if (line.StartsWith("version", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("version"))
{
string version = ParseHeader(line, "version");
Result.Movie.Comments.Add($"{MovieOrigin} .mc2 version {version}");
}
else if (line.StartsWith("romfilename", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("romfilename"))
{
Result.Movie.HeaderEntries[HeaderKeys.GameName] = ParseHeader(line, "romFilename");
}
else if (line.StartsWith("cdgamename", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("cdgamename"))
{
Result.Movie.HeaderEntries[HeaderKeys.GameName] = ParseHeader(line, "cdGameName");
}
else if (line.StartsWith("comment author", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("comment author"))
{
Result.Movie.HeaderEntries[HeaderKeys.Author] = ParseHeader(line, "comment author");
}
else if (line.StartsWith("rerecordcount", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("rerecordcount"))
{
int rerecordCount;
@ -123,7 +124,7 @@ namespace BizHawk.Client.Common.movie.import
Result.Movie.Rerecords = (ulong)rerecordCount;
}
else if (line.StartsWith("startsfromsavestate", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("startsfromsavestate"))
{
// If this movie starts from a savestate, we can't support it.
if (ParseHeader(line, "StartsFromSavestate") == "1")

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Reflection;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.Common
{
@ -18,11 +19,7 @@ namespace BizHawk.Client.Common
/// Returns a value indicating whether or not there is an importer for the given extension
/// </summary>
public static bool IsValidMovieExtension(string extension)
{
return Importers
.Select(i => i.Value)
.Any(e => string.Equals(extension, e.Extension, StringComparison.OrdinalIgnoreCase));
}
=> Importers.Any(kvp => kvp.Value.Extension.EqualsIgnoreCase(extension));
public static readonly FilesystemFilterSet AvailableImporters = new FilesystemFilterSet(
Importers.Values.OrderBy(attr => attr.Emulator)
@ -40,7 +37,7 @@ namespace BizHawk.Client.Common
Config config)
{
string ext = Path.GetExtension(path) ?? "";
var result = Importers.FirstOrNull(kvp => string.Equals(kvp.Value.Extension, ext, StringComparison.OrdinalIgnoreCase));
var result = Importers.FirstOrNull(kvp => kvp.Value.Extension.EqualsIgnoreCase(ext));
// Create a new instance of the importer class using the no-argument constructor
return result is { Key: var importerType }
&& importerType.GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty<object>()) is IMovieImport importer

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common.movie.import
@ -44,20 +45,20 @@ namespace BizHawk.Client.Common.movie.import
{
ImportTextFrame(line);
}
else if (line.StartsWith("emuversion", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("emuversion"))
{
Result.Movie.Comments.Add($"{EmulationOrigin} Yabause version {ParseHeader(line, "emuVersion")}");
}
else if (line.StartsWith("version", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("version"))
{
string version = ParseHeader(line, "version");
Result.Movie.Comments.Add($"{MovieOrigin} .ymv version {version}");
}
else if (line.StartsWith("cdGameName", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("cdGameName"))
{
Result.Movie.HeaderEntries[HeaderKeys.GameName] = ParseHeader(line, "romFilename");
}
else if (line.StartsWith("rerecordcount", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("rerecordcount"))
{
int rerecordCount;
@ -73,7 +74,7 @@ namespace BizHawk.Client.Common.movie.import
Result.Movie.Rerecords = (ulong)rerecordCount;
}
else if (line.StartsWith("startsfromsavestate", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("startsfromsavestate"))
{
// If this movie starts from a savestate, we can't support it.
if (ParseHeader(line, "StartsFromSavestate") == "1")
@ -81,7 +82,7 @@ namespace BizHawk.Client.Common.movie.import
Result.Errors.Add("Movies that begin with a savestate are not supported.");
}
}
else if (line.StartsWith("ispal", StringComparison.OrdinalIgnoreCase))
else if (line.StartsWithIgnoreCase("ispal"))
{
bool pal = ParseHeader(line, "isPal") == "1";
Result.Movie.HeaderEntries[HeaderKeys.Pal] = pal.ToString();

View File

@ -30,18 +30,10 @@ namespace BizHawk.Client.Common
{
return 0;
}
if (string.Equals(x.Notes, y.Notes, StringComparison.OrdinalIgnoreCase))
{
if (x.Address.Equals(y.Address))
{
return x.Size.CompareTo(y.Size);
}
return x.Address.CompareTo(y.Address);
}
return string.Compare(x.Notes, y.Notes, StringComparison.OrdinalIgnoreCase);
var cmpNotes = string.Compare(x.Notes, y.Notes, StringComparison.OrdinalIgnoreCase);
if (cmpNotes is not 0) return cmpNotes;
var cmpAddr = x.Address.CompareTo(y.Address);
return cmpAddr is not 0 ? cmpAddr : x.Size.CompareTo(y.Size);
}
}
}

View File

@ -2,6 +2,7 @@ using System.IO;
using System.Security.Principal;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -15,7 +16,7 @@ namespace BizHawk.Client.EmuHawk
public static string ResolveShortcut(string filename)
{
if (OSTailoredCode.IsUnixHost || HawkFile.PathContainsPipe(filename)
|| !".lnk".Equals(Path.GetExtension(filename), StringComparison.OrdinalIgnoreCase))
|| !".lnk".EqualsIgnoreCase(Path.GetExtension(filename)))
{
return filename; // archive internal files are never shortcuts (and choke when analyzing any further)
}

View File

@ -9,6 +9,7 @@ using System.Windows.Forms;
using BizHawk.Bizware.Graphics;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.CustomControls;
using BizHawk.Emulation.Cores;
@ -270,7 +271,7 @@ namespace BizHawk.Client.EmuHawk
}
// super hacky! this needs to be done first. still not worth the trouble to make this system fully proper
if (Array.Exists(args, static arg => arg.StartsWith("--gdi", StringComparison.OrdinalIgnoreCase)))
if (Array.Exists(args, static arg => arg.StartsWithIgnoreCase("--gdi")))
{
initialConfig.DispMethod = EDispMethod.GdiPlus;
}

View File

@ -129,7 +129,7 @@ namespace BizHawk.Client.EmuHawk
if (term == ';' || term == '\0')
{
var fn = Encoding.ASCII.GetString(buf2048, index + 33, filename.Length);
if (filename.Equals(fn, StringComparison.OrdinalIgnoreCase))
if (filename.EqualsIgnoreCase(fn))
{
filesize = (buf2048[index + 13] << 24) | (buf2048[index + 12] << 16) | (buf2048[index + 11] << 8) | buf2048[index + 10];
return (buf2048[index + 4] << 16) | (buf2048[index + 3] << 8) | buf2048[index + 2];

View File

@ -6,6 +6,7 @@ using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -199,7 +200,7 @@ namespace BizHawk.Client.EmuHawk
{
if (e.IsPressed(Keys.Enter) || e.IsPressed(Keys.Tab))
{
var k = HotkeyInfo.AllHotkeys.FirstOrNull(kvp => string.Compare(kvp.Value.DisplayName, SearchBox.Text, StringComparison.OrdinalIgnoreCase) is 0)?.Key;
var k = HotkeyInfo.AllHotkeys.FirstOrNull(kvp => kvp.Value.DisplayName.EqualsIgnoreCase(SearchBox.Text))?.Key;
// Found
if (k is not null)

View File

@ -217,13 +217,8 @@ namespace BizHawk.Client.EmuHawk
var tas = new List<int>();
for (var i = 0; i < indices.Count; i++)
{
foreach (var ext in MovieService.MovieExtensions)
{
if ($".{ext}".Equals(Path.GetExtension(_movieList[indices[i]].Filename), StringComparison.OrdinalIgnoreCase))
{
tas.Add(i);
}
}
var fileExt = Path.GetExtension(_movieList[indices[i]].Filename);
if (MovieService.MovieExtensions.Select(static s => $".{s}").Any(fileExt.EqualsIgnoreCase)) tas.Add(i);
}
if (tas.Count is 0)

View File

@ -31,8 +31,8 @@ namespace BizHawk.Client.EmuHawk
if (!string.IsNullOrWhiteSpace(FilterBox.Text))
{
_filteredList = _functionList
.Where(f => $"{f.Library}.{f.Name}".Contains(FilterBox.Text, StringComparison.OrdinalIgnoreCase)
|| f.Description.Contains(FilterBox.Text, StringComparison.OrdinalIgnoreCase))
.Where(f => $"{f.Library}.{f.Name}".ContainsIgnoreCase(FilterBox.Text)
|| f.Description.ContainsIgnoreCase(FilterBox.Text))
.ToList();
}
else

View File

@ -148,7 +148,7 @@ namespace BizHawk.Common.PathExtensions
if (fromUri.Scheme != toUri.Scheme) return basePath;
var relativePath = Uri.UnescapeDataString(fromUri.MakeRelativeUri(toUri).ToString());
return (toUri.Scheme.Equals(Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase)
return (Uri.UriSchemeFile.EqualsIgnoreCase(toUri.Scheme)
? relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
: relativePath
).TrimEnd(dirSepChar);

View File

@ -22,6 +22,11 @@ namespace BizHawk.Common.StringExtensions
public static bool Contains(this string haystack, string needle, StringComparison comparisonType)
=> haystack.IndexOf(needle, comparisonType) != -1;
/// <inheritdoc cref="EqualsIgnoreCase"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsIgnoreCase(this string haystack, string needle)
=> haystack.Contains(needle, StringComparison.OrdinalIgnoreCase);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsOrdinal(this string haystack, char needle)
=> haystack.Contains(needle); // already ordinal
@ -39,11 +44,23 @@ namespace BizHawk.Common.StringExtensions
public static bool EndsWithOrdinal(this string haystack, char needle)
=> haystack.EndsWith(needle); // already ordinal
#pragma warning disable RS0030 // doc comment links to banned API
/// <summary>performs a non-localised but case-insensitive comparison</summary>
/// <remarks>
/// uses <see cref="StringComparison.OrdinalIgnoreCase"/>,
/// equivalent to <c>str.ToUpperInvariant().SequenceEqual(other.ToUpperInvariant())</c> per <see href="https://learn.microsoft.com/en-us/dotnet/api/system.stringcomparer.ordinalignorecase?view=netstandard-2.0#remarks">docs</see>;
/// whereas <see cref="StringComparison.InvariantCultureIgnoreCase"/> is different (for non-ASCII text)
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EqualsIgnoreCase(this string str, string other)
=> str.Equals(other, StringComparison.OrdinalIgnoreCase);
#pragma warning restore RS0030
/// <returns>
/// <see langword="true"/> if <paramref name="str"/> appears in <paramref name="options"/> (case-insensitive)
/// </returns>
public static bool In(this string str, params string[] options) =>
options.Any(opt => string.Equals(opt, str, StringComparison.OrdinalIgnoreCase));
public static bool In(this string str, params string[] options)
=> options.Any(str.EqualsIgnoreCase);
/// <returns>a copy of <paramref name="raw"/> with all characters outside <c>[0-9A-Za-z]</c> removed</returns>
public static string OnlyAlphanumeric(this string raw)
@ -103,6 +120,11 @@ namespace BizHawk.Common.StringExtensions
public static bool StartsWith(this string str, char c)
=> str.Length >= 1 && str[0] == c;
/// <inheritdoc cref="EqualsIgnoreCase"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWithIgnoreCase(this string haystack, string needle)
=> haystack.StartsWith(needle, StringComparison.OrdinalIgnoreCase);
/// <returns>
/// the substring of <paramref name="str"/> after the first occurrence of <paramref name="delimiter"/>, or
/// the original <paramref name="str"/> if not found

View File

@ -145,13 +145,11 @@ namespace BizHawk.Common
Exists = false;
return;
}
for (int i = 0, l = scanResults.Count; i < l; i++)
var i = scanResults.FindIndex(item => item.Name.EqualsIgnoreCase(autobind));
if (i >= 0)
{
if (string.Equals(scanResults[i].Name, autobind, StringComparison.OrdinalIgnoreCase))
{
BindArchiveMember(i);
return;
}
BindArchiveMember(i);
return;
}
}

View File

@ -88,7 +88,7 @@ namespace BizHawk.Common
});
private static readonly Lazy<bool> _isWSL = new(static () => IsUnixHost
&& SimpleSubshell(cmd: "uname", args: "-r", noOutputMsg: "missing uname?").Contains("microsoft", StringComparison.OrdinalIgnoreCase));
&& SimpleSubshell(cmd: "uname", args: "-r", noOutputMsg: "missing uname?").ContainsIgnoreCase("microsoft"));
public static (WindowsVersion Version, Version? Win10PlusVersion)? HostWindowsVersion => _HostWindowsVersion.Value;

View File

@ -8,6 +8,8 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Common
{
public static unsafe class Util
@ -110,7 +112,9 @@ namespace BizHawk.Common
/// <returns>all <see cref="Type">Types</see> with the name <paramref name="className"/></returns>
/// <remarks>adapted from https://stackoverflow.com/a/13727044/7467292</remarks>
public static IList<Type> GetTypeByName(string className) => AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(asm => asm.GetTypesWithoutLoadErrors().Where(type => className.Equals(type.Name, StringComparison.OrdinalIgnoreCase))).ToList();
.SelectMany(static asm => asm.GetTypesWithoutLoadErrors())
.Where(type => type.Name.EqualsIgnoreCase(className))
.ToList();
/// <remarks>TODO replace this with GetTypes (i.e. the try block) when VB.NET dep is properly removed</remarks>
public static IEnumerable<Type> GetTypesWithoutLoadErrors(this Assembly assembly)

View File

@ -82,7 +82,7 @@ namespace BizHawk.Emulation.Common
continue;
string str = Encoding.ASCII.GetString(s.SectorData, 0, s.SectorData.Length);
if (str.Contains("PLUS3DOS", StringComparison.OrdinalIgnoreCase))
if (str.ContainsIgnoreCase("PLUS3DOS"))
{
IdentifiedSystem = VSystemID.Raw.ZXSpectrum;
return;

View File

@ -44,8 +44,8 @@ namespace BizHawk.Emulation.Common
private static void LoadDatabase_Escape(string line, bool inUser, bool silent)
{
var isUserInclude = line.StartsWith("#includeuser", StringComparison.OrdinalIgnoreCase);
if (!isUserInclude && !line.StartsWith("#include", StringComparison.OrdinalIgnoreCase)) return;
var isUserInclude = line.StartsWithIgnoreCase("#includeuser");
if (!isUserInclude && !line.StartsWithIgnoreCase("#include")) return;
var searchUser = inUser || isUserInclude;
line = line.Substring(isUserInclude ? 12 : 8).TrimStart();

View File

@ -31,7 +31,7 @@ namespace BizHawk.Emulation.Common
// look for standard magic string
string ident = Encoding.ASCII.GetString(_data, 0, 16);
if (!ident.Contains("CAPS", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("CAPS"))
{
// incorrect format
return;

View File

@ -48,21 +48,9 @@ namespace BizHawk.Emulation.Cores.Arcades.MAME
text + (text == "" ? "" : "\r\n") +
string.Join("\r\n", _romHashes.Select(static r => $"{r.Value} - {r.Key}"));
if (text.Contains("imperfect", StringComparison.OrdinalIgnoreCase))
{
lp.Game.Status = RomStatus.Imperfect;
}
if (text.Contains("unemulated", StringComparison.OrdinalIgnoreCase))
{
lp.Game.Status = RomStatus.Unimplemented;
}
if (text.Contains("doesn't work", StringComparison.OrdinalIgnoreCase))
{
lp.Game.Status = RomStatus.NotWorking;
}
if (text.ContainsIgnoreCase("doesn't work")) lp.Game.Status = RomStatus.NotWorking;
else if (text.ContainsIgnoreCase("unemulated")) lp.Game.Status = RomStatus.Unimplemented;
else if (text.ContainsIgnoreCase("imperfect")) lp.Game.Status = RomStatus.Imperfect;
};
_exe = new(new()

View File

@ -232,7 +232,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
string hdr = Encoding.ASCII.GetString(data.Take(16).ToArray());
// disk checking first
if (hdr.Contains("EXTENDED CPC DSK", StringComparison.OrdinalIgnoreCase) || hdr.Contains("MV - CPC", StringComparison.OrdinalIgnoreCase))
if (hdr.ContainsIgnoreCase("MV - CPC") || hdr.ContainsIgnoreCase("EXTENDED CPC DSK"))
{
// amstrad .dsk disk file
// check for number of sides
@ -244,7 +244,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
}
// tape checking
if (hdr.StartsWith("ZXTAPE!", StringComparison.OrdinalIgnoreCase))
if (hdr.StartsWithIgnoreCase("ZXTAPE!"))
{
// cdt tape file
return CPCMediaType.Tape;

View File

@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("EXTENDED CPC DSK", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("EXTENDED CPC DSK"))
{
// incorrect format
return false;
@ -153,7 +153,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
{
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("EXTENDED CPC DSK", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("EXTENDED CPC DSK"))
{
// incorrect format
return false;

View File

@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("MV - CPC", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("MV - CPC"))
{
// incorrect format
return false;
@ -159,7 +159,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
{
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("MV - CPC", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("MV - CPC"))
{
// incorrect format
return false;

View File

@ -240,8 +240,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// check for SPEEDLOCK ident in sector 0
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length);
if (!ident.Contains("SPEEDLOCK", StringComparison.OrdinalIgnoreCase))
return false;
if (!ident.ContainsIgnoreCase("SPEEDLOCK")) return false;
// check for correct sector 0 lengths
if (DiskTracks[0].Sectors[0] is not { SectorSize: 2, SectorData.Length: >= 0x200 }) return false;
@ -296,8 +295,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// check for ALKATRAZ ident in sector 0
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length);
if (!ident.Contains("ALKATRAZ PROTECTION SYSTEM", StringComparison.OrdinalIgnoreCase))
return false;
if (!ident.ContainsIgnoreCase("ALKATRAZ PROTECTION SYSTEM")) return false;
// ALKATRAZ NOTES (-asni 2018-05-01)
// ---------------------------------
@ -337,8 +335,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// check for PAUL OWENS ident in sector 2
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[2].SectorData, 0, DiskTracks[0].Sectors[2].SectorData.Length);
if (!ident.Contains("PAUL OWENS", StringComparison.OrdinalIgnoreCase))
return false;
if (!ident.ContainsIgnoreCase("PAUL OWENS")) return false;
// Paul Owens Disk Protection Notes (-asni 2018-05-01)
// ---------------------------------------------------
@ -379,8 +376,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// check for Hexagon ident in sector 8
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[8].SectorData, 0, DiskTracks[0].Sectors[8].SectorData.Length);
if (ident.Contains("GON DISK PROT", StringComparison.OrdinalIgnoreCase))
return true;
if (ident.ContainsIgnoreCase("GON DISK PROT")) return true;
// hexagon protection may not be labelled as such
var track = DiskTracks[1];
@ -414,7 +410,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
// check for speedlock copyright notice
string ident = Encoding.ASCII.GetString(DiskData, 0x100, 0x1400);
if (!ident.ToUpper().Contains("SPEEDLOCK"))
if (!ident.ContainsIgnoreCase("SPEEDLOCK"))
{
// speedlock not found
return;

View File

@ -245,7 +245,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
string hdr = Encoding.ASCII.GetString(data.Take(16).ToArray());
// disk checking first
if (hdr.Contains("EXTENDED CPC DSK", StringComparison.OrdinalIgnoreCase) || hdr.Contains("MV - CPC", StringComparison.OrdinalIgnoreCase))
if (hdr.ContainsIgnoreCase("MV - CPC") || hdr.ContainsIgnoreCase("EXTENDED CPC DSK"))
{
// spectrum .dsk disk file
// check for number of sides
@ -255,17 +255,17 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
else
return SpectrumMediaType.DiskDoubleSided;
}
if (hdr.StartsWith("FDI", StringComparison.OrdinalIgnoreCase))
if (hdr.StartsWithIgnoreCase("FDI"))
{
// spectrum .fdi disk file
return SpectrumMediaType.Disk;
}
if (hdr.StartsWith("CAPS", StringComparison.OrdinalIgnoreCase))
if (hdr.StartsWithIgnoreCase("CAPS"))
{
// IPF format file
return SpectrumMediaType.Disk;
}
if (hdr.StartsWith("UDI!", StringComparison.OrdinalIgnoreCase) && data[0x08] == 0)
if (hdr.StartsWithIgnoreCase("UDI!") && data[0x08] is 0)
{
// UDI v1.0
if (hdr.StartsWithOrdinal("udi!"))
@ -282,22 +282,22 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
}
// tape checking
if (hdr.StartsWith("ZXTAPE!", StringComparison.OrdinalIgnoreCase))
if (hdr.StartsWithIgnoreCase("ZXTAPE!"))
{
// spectrum .tzx tape file
return SpectrumMediaType.Tape;
}
if (hdr.StartsWith("PZXT", StringComparison.OrdinalIgnoreCase))
if (hdr.StartsWithIgnoreCase("PZXT"))
{
// spectrum .tzx tape file
return SpectrumMediaType.Tape;
}
if (hdr.StartsWith("COMPRESSED SQ", StringComparison.OrdinalIgnoreCase))
if (hdr.StartsWithIgnoreCase("COMPRESSED SQ"))
{
// spectrum .tzx tape file
return SpectrumMediaType.Tape;
}
if (hdr.Contains("WAVE", StringComparison.OrdinalIgnoreCase))
if (hdr.ContainsIgnoreCase("WAVE"))
{
// spectrum .tzx tape file
return SpectrumMediaType.Tape;

View File

@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("EXTENDED CPC DSK", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("EXTENDED CPC DSK"))
{
// incorrect format
return false;
@ -153,7 +153,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
{
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("EXTENDED CPC DSK", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("EXTENDED CPC DSK"))
{
// incorrect format
return false;

View File

@ -28,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("MV - CPC", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("MV - CPC"))
{
// incorrect format
return false;
@ -158,7 +158,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
{
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("MV - CPC", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("MV - CPC"))
{
// incorrect format
return false;

View File

@ -240,8 +240,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check for SPEEDLOCK ident in sector 0
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length);
if (!ident.Contains("SPEEDLOCK", StringComparison.OrdinalIgnoreCase))
return false;
if (!ident.ContainsIgnoreCase("SPEEDLOCK")) return false;
// check for correct sector 0 lengths
if (DiskTracks[0].Sectors[0] is not { SectorSize: 2, SectorData.Length: >= 0x200 }) return false;
@ -296,8 +295,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check for ALKATRAZ ident in sector 0
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[0].SectorData, 0, DiskTracks[0].Sectors[0].SectorData.Length);
if (!ident.Contains("ALKATRAZ PROTECTION SYSTEM", StringComparison.OrdinalIgnoreCase))
return false;
if (!ident.ContainsIgnoreCase("ALKATRAZ PROTECTION SYSTEM")) return false;
// ALKATRAZ NOTES (-asni 2018-05-01)
// ---------------------------------
@ -337,8 +335,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check for PAUL OWENS ident in sector 2
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[2].SectorData, 0, DiskTracks[0].Sectors[2].SectorData.Length);
if (!ident.Contains("PAUL OWENS", StringComparison.OrdinalIgnoreCase))
return false;
if (!ident.ContainsIgnoreCase("PAUL OWENS")) return false;
// Paul Owens Disk Protection Notes (-asni 2018-05-01)
// ---------------------------------------------------
@ -379,8 +376,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check for Hexagon ident in sector 8
string ident = Encoding.ASCII.GetString(DiskTracks[0].Sectors[8].SectorData, 0, DiskTracks[0].Sectors[8].SectorData.Length);
if (ident.Contains("GON DISK PROT", StringComparison.OrdinalIgnoreCase))
return true;
if (ident.ContainsIgnoreCase("GON DISK PROT")) return true;
// hexagon protection may not be labelled as such
var track = DiskTracks[1];
@ -414,7 +410,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// check for speedlock copyright notice
string ident = Encoding.ASCII.GetString(DiskData, 0x100, 0x1400);
if (!ident.ToUpper().Contains("SPEEDLOCK"))
if (!ident.ContainsIgnoreCase("SPEEDLOCK"))
{
// speedlock not found
return;

View File

@ -27,7 +27,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
// look for standard magic string
string ident = Encoding.ASCII.GetString(data, 0, 16);
if (!ident.Contains("CAPS", StringComparison.OrdinalIgnoreCase))
if (!ident.ContainsIgnoreCase("CAPS"))
{
// incorrect format
return false;

View File

@ -2,6 +2,7 @@ using System.ComponentModel;
using System.Globalization;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
using Newtonsoft.Json;
@ -54,7 +55,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx
if (value?.GetType() == typeof(string))
{
var input = (string)value;
if (input.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
if (input.StartsWithIgnoreCase("0x"))
{
input = input[2..];
}
@ -89,7 +90,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx
if (value?.GetType() == typeof(string))
{
var input = (string)value;
if (input.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
if (input.StartsWithIgnoreCase("0x"))
{
input = input[2..];
}

View File

@ -1,7 +1,9 @@
using System.IO;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.DiscSystem
{
@ -29,28 +31,10 @@ namespace BizHawk.Emulation.DiscSystem
private string FindAudio(string audioPath)
{
var (dir, basePath, _) = audioPath.SplitPathToDirFileAndExt();
//look for potential candidates
DirectoryInfo di = new(dir!);
var fis = di.GetFiles();
//first, look for the file type we actually asked for
foreach (var fi in fis)
{
if (string.Equals(fi.FullName, audioPath, StringComparison.OrdinalIgnoreCase))
if (CheckForAudio(fi.FullName))
return fi.FullName;
}
//then look for any other type
foreach (var fi in fis)
{
if (string.Equals(Path.GetFileNameWithoutExtension(fi.FullName), basePath, StringComparison.OrdinalIgnoreCase))
{
if (CheckForAudio(fi.FullName))
{
return fi.FullName;
}
}
}
return null;
var filePaths = new DirectoryInfo(dir!).GetFiles().Select(static fi => fi.FullName).ToArray();
return filePaths.Where(audioPath.EqualsIgnoreCase) // first, look for the file type we actually asked for...
.Concat(filePaths.Where(filePath => Path.GetFileNameWithoutExtension(filePath).EqualsIgnoreCase(basePath))) // ...then look for any other type
.FirstOrDefault(CheckForAudio);
}
/// <exception cref="AudioDecoder_Exception">could not find source audio for <paramref name="audioPath"/></exception>

View File

@ -212,7 +212,7 @@ namespace BizHawk.Emulation.DiscSystem
continue;
}
int val;
if (parts[1].StartsWith("0x", StringComparison.OrdinalIgnoreCase))
if (parts[1].StartsWithIgnoreCase("0x"))
val = int.Parse(parts[1].Substring(2), NumberStyles.HexNumber);
else
val = int.Parse(parts[1]);

View File

@ -312,7 +312,8 @@ namespace BizHawk.Emulation.DiscSystem.CUE
// cues don't support multiple sessions themselves, but it is common for rips to put SESSION # in REM fields
// so, if we have such a REM, we'll check if the comment starts with SESSION, and interpret that as a session "command"
var trimmed = comment.Trim();
if (trimmed.StartsWith("SESSION ", StringComparison.OrdinalIgnoreCase) && int.TryParse(trimmed.Substring(8), out var number) && number > 0)
if (trimmed.StartsWithIgnoreCase("SESSION ")
&& int.TryParse(trimmed.Substring(8), out var number) && number > 0)
{
OUT_CueFile.Commands.Add(new CUE_File.Command.SESSION(number));
break;

View File

@ -2,6 +2,8 @@ using System.IO;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.DiscSystem.CUE
{
/// <summary>
@ -33,6 +35,7 @@ namespace BizHawk.Emulation.DiscSystem.CUE
/// </summary>
public List<string> Resolve(string path)
{
var interpretedAsRel = Path.Combine(_baseDir, path);
string targetFile = Path.GetFileName(path);
string targetFragment = Path.GetFileNameWithoutExtension(path);
@ -66,7 +69,7 @@ namespace BizHawk.Emulation.DiscSystem.CUE
if (cmp == 0)
{
//take care to add an exact match at the beginning
if (string.Equals(filePath, Path.Combine(_baseDir, path), StringComparison.OrdinalIgnoreCase))
if (filePath.EqualsIgnoreCase(interpretedAsRel))
results.Insert(0, filePath);
else
results.Add(filePath);

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Runtime.InteropServices;
using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using ISOParser;
@ -488,7 +489,7 @@ namespace BizHawk.Emulation.DiscSystem
var (dir, fileNoExt, _) = aFile.MDSPath.SplitPathToDirFileAndExt();
if (f.FilenameOffset is 0
|| string.Equals(fileName, "*.mdf", StringComparison.OrdinalIgnoreCase))
|| "*.mdf".EqualsIgnoreCase(fileName))
{
fileName = $@"{dir}\{fileNoExt}.mdf";
}

View File

@ -452,7 +452,7 @@ namespace BizHawk.Emulation.DiscSystem
private bool SectorContains(string s, int lba = 0)
{
var data = ReadDataSectorCached(lba);
return data != null && Encoding.ASCII.GetString(data).Contains(s, StringComparison.OrdinalIgnoreCase);
return data is not null && Encoding.ASCII.GetString(data).ContainsIgnoreCase(s);
}
}
}