diff --git a/.global.editorconfig.ini b/.global.editorconfig.ini index 3a4c5b40e6..a5469f0628 100644 --- a/.global.editorconfig.ini +++ b/.global.editorconfig.ini @@ -73,6 +73,8 @@ dotnet_diagnostic.CA1822.severity = silent dotnet_code_quality.CA1826.exclude_ordefault_methods = true # Avoid StringBuilder parameters for P/Invokes dotnet_diagnostic.CA1838.severity = suggestion +# Use the 'StringComparison' method overloads to perform case-insensitive string comparisons +dotnet_diagnostic.CA1862.severity = error ## Usage rules diff --git a/src/BizHawk.Client.Common/RomGame.cs b/src/BizHawk.Client.Common/RomGame.cs index eda1d3d065..090e7071f8 100644 --- a/src/BizHawk.Client.Common/RomGame.cs +++ b/src/BizHawk.Client.Common/RomGame.cs @@ -63,7 +63,9 @@ namespace BizHawk.Client.Common NotInDatabase = true }; +#pragma warning disable CA1862 // testing whether it's all-caps if (!string.IsNullOrWhiteSpace(GameInfo.Name) && GameInfo.Name == GameInfo.Name.ToUpperInvariant()) +#pragma warning restore CA1862 { GameInfo.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(GameInfo.Name.ToLowerInvariant()); } diff --git a/src/BizHawk.Client.Common/RomLoader.cs b/src/BizHawk.Client.Common/RomLoader.cs index 12712bc0e0..a1a738e14a 100644 --- a/src/BizHawk.Client.Common/RomLoader.cs +++ b/src/BizHawk.Client.Common/RomLoader.cs @@ -655,7 +655,7 @@ namespace BizHawk.Client.Common private static bool IsDiscForXML(string system, string path) { var ext = Path.GetExtension(path); - if (system == VSystemID.Raw.Arcade && ext.ToLowerInvariant() == ".chd") + if (system is VSystemID.Raw.Arcade && ".chd".EqualsIgnoreCase(ext)) { return false; } diff --git a/src/BizHawk.Client.EmuHawk/AVOut/ImageSequenceWriter.cs b/src/BizHawk.Client.EmuHawk/AVOut/ImageSequenceWriter.cs index f0bf9bf699..e16965f65f 100644 --- a/src/BizHawk.Client.EmuHawk/AVOut/ImageSequenceWriter.cs +++ b/src/BizHawk.Client.EmuHawk/AVOut/ImageSequenceWriter.cs @@ -4,6 +4,7 @@ using System.Drawing.Imaging; using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Common.PathExtensions; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk @@ -49,11 +50,11 @@ namespace BizHawk.Client.EmuHawk var name = Path.Combine(dir!, $"{fileNoExt}_{_frame}{ext}"); BitmapBuffer bb = new BitmapBuffer(source.BufferWidth, source.BufferHeight, source.GetVideoBuffer()); using var bmp = bb.ToSysdrawingBitmap(); - if (ext.ToUpperInvariant() == ".PNG") + if (".PNG".EqualsIgnoreCase(ext)) { bmp.Save(name, ImageFormat.Png); } - else if (ext.ToUpperInvariant() == ".JPG") + else if (".JPG".EqualsIgnoreCase(ext)) { bmp.Save(name, ImageFormat.Jpeg); } diff --git a/src/BizHawk.Client.EmuHawk/AVOut/JMDWriter.cs b/src/BizHawk.Client.EmuHawk/AVOut/JMDWriter.cs index 8d0800ebf1..cfbe067e5a 100644 --- a/src/BizHawk.Client.EmuHawk/AVOut/JMDWriter.cs +++ b/src/BizHawk.Client.EmuHawk/AVOut/JMDWriter.cs @@ -6,7 +6,7 @@ using System.Text; using System.Threading; using BizHawk.Client.Common; - +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk @@ -600,7 +600,7 @@ namespace BizHawk.Client.EmuHawk public void OpenFile(string baseName) { string ext = Path.GetExtension(baseName); - if (ext == null || ext.ToLowerInvariant() != ".jmd") + if (ext?.EqualsIgnoreCase(".jmd") is true or null) { baseName += ".jmd"; } diff --git a/src/BizHawk.Client.EmuHawk/ArchiveChooser.cs b/src/BizHawk.Client.EmuHawk/ArchiveChooser.cs index 3a95e41783..9105b94ab1 100644 --- a/src/BizHawk.Client.EmuHawk/ArchiveChooser.cs +++ b/src/BizHawk.Client.EmuHawk/ArchiveChooser.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Windows.Forms; using BizHawk.Common; +using BizHawk.Common.StringExtensions; namespace BizHawk.Client.EmuHawk { @@ -33,9 +34,7 @@ namespace BizHawk.Client.EmuHawk lvi.SubItems.Add(new ListViewItem.ListViewSubItem()); lvi.Text = item.Name; long size = item.Size; - var extension = Path.GetExtension(item.Name); - if (extension != null && size % 1024 == 16 && extension.ToUpperInvariant() == ".NES") - size -= 16; + if (size % 1024 is 16 && Path.GetExtension(item.Name)?.EqualsIgnoreCase(".NES") is true) size -= 16; lvi.SubItems[1].Text = Util.FormatFileSize(size); _archiveItems.Add(lvi); } diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index f035b54c11..b952f8aab5 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -1388,7 +1388,7 @@ namespace BizHawk.Client.EmuHawk using (var bb = Config.ScreenshotCaptureOsd ? CaptureOSD() : MakeScreenshotImage()) { using var img = bb.ToSysdrawingBitmap(); - if (Path.GetExtension(path).ToUpperInvariant() == ".JPG") + if (".JPG".EqualsIgnoreCase(Path.GetExtension(path))) { img.Save(fi.FullName, ImageFormat.Jpeg); } @@ -3724,7 +3724,8 @@ namespace BizHawk.Client.EmuHawk InputManager.SyncControls(Emulator, MovieSession, Config); _multiDiskMode = false; - if (oaOpenrom != null && Path.GetExtension(oaOpenrom.Path.Replace("|", "")).ToLowerInvariant() == ".xml" && Emulator is not LibsnesCore) + if (oaOpenrom is not null && ".xml".EqualsIgnoreCase(Path.GetExtension(oaOpenrom.Path.Replace("|", ""))) + && Emulator is not LibsnesCore) { // this is a multi-disk bundler file // determine the xml assets and create RomStatusDetails for all of them diff --git a/src/BizHawk.Emulation.Common/Database/Database.cs b/src/BizHawk.Emulation.Common/Database/Database.cs index d011e82b6f..1fd3cf5fcb 100644 --- a/src/BizHawk.Emulation.Common/Database/Database.cs +++ b/src/BizHawk.Emulation.Common/Database/Database.cs @@ -487,7 +487,9 @@ namespace BizHawk.Emulation.Common game.Name = Path.GetFileNameWithoutExtension(fileName)?.Replace('_', ' '); // If filename is all-caps, then attempt to proper-case the title. +#pragma warning disable CA1862 // testing whether it's all-caps if (!string.IsNullOrWhiteSpace(game.Name) && game.Name == game.Name.ToUpperInvariant()) +#pragma warning restore CA1862 { game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(game.Name.ToLowerInvariant()); } diff --git a/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs b/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs index f188b36c4c..83c24885e2 100644 --- a/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs +++ b/src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.cs @@ -251,7 +251,7 @@ namespace BizHawk.Emulation.Cores.Arcades.MAME foreach (var rom in roms) { // only close non-chd files - if (rom.Extension.ToLowerInvariant() != ".chd") + if (!".chd".EqualsIgnoreCase(rom.Extension)) { _exe.RemoveReadonlyFile(MakeFileName(rom)); } diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs index defd68480f..94a11dd721 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/CSW/CswConverter.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Text; +using BizHawk.Common.StringExtensions; + namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { /// @@ -57,7 +59,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum int majorVer = data[8]; int minorVer = data[9]; - if (ident.ToUpperInvariant() != "COMPRESSED SQUARE WAVE") + if (!"COMPRESSED SQUARE WAVE".EqualsIgnoreCase(ident)) { // this is not a valid CSW format file return false; @@ -80,7 +82,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // (first 22 bytes of the file) string ident = Encoding.ASCII.GetString(data, 0, 22); - if (ident.ToUpperInvariant() != "COMPRESSED SQUARE WAVE") + if (!"COMPRESSED SQUARE WAVE".EqualsIgnoreCase(ident)) { // this is not a valid CSW format file throw new Exception($"{nameof(CswConverter)}: This is not a valid CSW format file"); diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs index 22f69db230..771cf24683 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/PZX/PzxConverter.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Text; +using BizHawk.Common.StringExtensions; + namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { /// @@ -67,7 +69,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum int majorVer = data[8]; int minorVer = data[9]; - if (ident.ToUpperInvariant() != "PZXT") + if (!"PZXT".EqualsIgnoreCase(ident)) { // this is not a valid PZX format file return false; @@ -96,7 +98,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // check whether this is a valid pzx format file by looking at the identifier in the header block string ident = Encoding.ASCII.GetString(data, 0, 4); - if (ident.ToUpperInvariant() != "PZXT") + if (!"PZXT".EqualsIgnoreCase(ident)) { // this is not a valid TZX format file throw new Exception($"{nameof(PzxConverter)}: This is not a valid PZX format file"); diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs index 20187fb095..35120b7f8d 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Tape/WAV/WavConverter.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Text; +using BizHawk.Common.StringExtensions; + namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { /// @@ -50,7 +52,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // check whether this is a valid wav format file by looking at the identifier in the header string ident = Encoding.ASCII.GetString(data, 8, 4); - if (ident.ToUpperInvariant() != "WAVE") + if (!"WAVE".EqualsIgnoreCase(ident)) { // this is not a valid WAV format file return false; @@ -72,7 +74,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum // check whether this is a valid pzx format file by looking at the identifier in the header block string ident = Encoding.ASCII.GetString(data, 8, 4); - if (ident.ToUpperInvariant() != "WAVE") + if (!"WAVE".EqualsIgnoreCase(ident)) { // this is not a valid TZX format file throw new Exception($"{nameof(WavConverter)}: This is not a valid WAV format file"); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs index b90678a3ad..d234fb066f 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs @@ -6,6 +6,7 @@ using System.Text; using BizHawk.BizInvoke; using BizHawk.Common; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; #pragma warning disable BHI1007 // target-typed Exception TODO don't @@ -122,7 +123,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS _serviceProvider.Register(_encoreVideoProvider); var romPath = lp.Roms[0].RomPath; - if (lp.Roms[0].Extension.ToLowerInvariant() == ".cia") + if (".cia".EqualsIgnoreCase(lp.Roms[0].Extension)) { var message = new byte[1024]; var res = _core.Encore_InstallCIA(_context, romPath, message, message.Length); @@ -145,7 +146,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS for (var i = 1; i < lp.Roms.Count; i++) { // doesn't make sense if not a CIA - if (lp.Roms[i].Extension.ToLowerInvariant() != ".cia") + if (".cia".EqualsIgnoreCase(lp.Roms[i].Extension)) { Dispose(); throw new("ROMs after the index 0 should be CIAs"); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs index adae5f4dd0..028ac296a0 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs @@ -35,7 +35,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy public void SetCpuRegister(string register, int value) { - if (register.Length == 9 && register.Substring(4, 5).ToUpperInvariant() == " BANK") + if (register.Length is 9 && register.EndsWith(" BANK", StringComparison.OrdinalIgnoreCase)) { var type = (LibGambatte.BankType)Enum.Parse(typeof(LibGambatte.BankType), register.Substring(0, 4).ToUpperInvariant()); LibGambatte.gambatte_setbank(GambatteState, type, value); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs index bbeeccdefb..cceb0160d1 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs @@ -4,6 +4,7 @@ using System.IO; using BizHawk.Common; using BizHawk.Common.PathExtensions; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Components.W65816; @@ -279,7 +280,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES // every rom requests msu1.rom... why? who knows. // also handle msu-1 pcm files here bool isMsu1Rom = hint == "msu1.rom"; - bool isMsu1Pcm = Path.GetExtension(hint).ToLowerInvariant() == ".pcm"; + var isMsu1Pcm = ".pcm".EqualsIgnoreCase(Path.GetExtension(hint)); if (isMsu1Rom || isMsu1Pcm) { // well, check if we have an msu-1 xml diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs index 1aa0ec90fc..ce1dc0d26e 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/CCD_format.cs @@ -204,7 +204,7 @@ namespace BizHawk.Emulation.DiscSystem var parts = line.Split('='); if (parts.Length != 2) throw new CCDParseException("Malformed or unexpected CCD format: parsing item into two parts"); - if (parts[0].ToUpperInvariant() == "FLAGS") + if ("FLAGS".EqualsIgnoreCase(parts[0])) { // flags are a space-separated collection of symbolic constants: // https://www.gnu.org/software/ccd2cue/manual/html_node/FLAGS-_0028Compact-Disc-fields_0029.html#FLAGS-_0028Compact-Disc-fields_0029 diff --git a/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs b/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs index f54c792801..e2a5128d17 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs @@ -238,7 +238,7 @@ namespace BizHawk.Emulation.DiscSystem return DiscType.Playdia; if (sysId is "CDTV" or "AMIGA" - || iso.Root.Children.Keys.Any(k => k.ToLowerInvariant().Contains("cd32"))) + || iso.Root.Children.Keys.Any(static k => k.ContainsIgnoreCase("cd32"))) { return DiscType.Amiga; } diff --git a/src/BizHawk.Tests/Emulation.Common/Database/FirmwareDatabaseTests.cs b/src/BizHawk.Tests/Emulation.Common/Database/FirmwareDatabaseTests.cs index d2c5a9a8f9..010f719e12 100644 --- a/src/BizHawk.Tests/Emulation.Common/Database/FirmwareDatabaseTests.cs +++ b/src/BizHawk.Tests/Emulation.Common/Database/FirmwareDatabaseTests.cs @@ -22,7 +22,9 @@ namespace BizHawk.Tests.Emulation.Common public void CheckFormatOfHashes() { static void CustomAssert(string hash) +#pragma warning disable CA1862 // testing whether it's all-caps => Assert.IsTrue(hash.Length == 40 && hash == hash.ToUpperInvariant() && hash.IsHex(), $"incorrectly formatted: {hash}"); +#pragma warning restore CA1862 foreach (var hash in FirmwareDatabase.FirmwareFilesByHash.Keys) CustomAssert(hash); foreach (var fo in FirmwareDatabase.FirmwareOptions) CustomAssert(fo.Hash); }