Add and use extension method `string.StartsWith(char)`

reverts 333ce98a7, partially reverts bc16a2cda
This commit is contained in:
YoshiRulz 2023-05-23 09:18:25 +10:00
parent 333ce98a71
commit 9daac2c26f
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
20 changed files with 62 additions and 40 deletions

View File

@ -7,6 +7,7 @@ using System.Threading;
using BizHawk.Bizware.BizwareGL;
using BizHawk.Bizware.OpenTK3;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using SlimDX.Direct3D9;
@ -448,8 +449,7 @@ namespace BizHawk.Bizware.DirectX
string name = prefix + descr.Name;
//uniforms done through the entry point signature have $ in their names which isn't helpful, so get rid of that
if (name.StartsWith("$"))
name = name.Substring(1);
name = name.RemovePrefix('$');
ui.Name = name;
uw.Description = descr;

View File

@ -11,6 +11,7 @@ using BizHawk.Client.Common.FilterManager;
using BizHawk.Bizware.BizwareGL;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.Common.Filters
{
@ -126,8 +127,8 @@ namespace BizHawk.Client.Common.Filters
foreach (var splitLine in content.Split('\n'))
{
var line = splitLine.Trim();
if (line.StartsWith("#")) continue; //lines that are solely comments
if (line == "") continue; //empty line
if (line.Length is 0) continue;
if (line.StartsWith('#')) continue; // comments
int eq = line.IndexOf('=');
var key = line.Substring(0, eq).Trim();
var value = line.Substring(eq + 1).Trim();

View File

@ -1,5 +1,8 @@
using System;
using System.IO;
using BizHawk.Common.StringExtensions;
using Newtonsoft.Json;
//this file contains some cumbersome self-"serialization" in order to gain a modicum of control over what the serialized output looks like
@ -39,7 +42,7 @@ namespace BizHawk.Client.Common
{
public static IOpenAdvanced ParseWithLegacy(string text)
{
return text.StartsWith("*")
return text.StartsWith('*')
? Deserialize(text.Substring(1))
: new OpenAdvanced_OpenRom { Path = text };
}

View File

@ -2,6 +2,7 @@
using System.IO;
using BizHawk.Common;
using BizHawk.Common.PathExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -316,10 +317,7 @@ namespace BizHawk.Client.Common
private static string ResolveToolsPath(this PathEntryCollection collection, string subPath)
{
if (Path.IsPathRooted(subPath) || subPath.StartsWith("%"))
{
return subPath;
}
if (Path.IsPathRooted(subPath) || subPath.StartsWith('%')) return subPath;
var toolsPath = collection[PathEntryCollection.GLOBAL, "Tools"].Path;

View File

@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -247,13 +249,7 @@ namespace BizHawk.Client.Common
{
// just skim through the input log and count input lines
// FIXME: this is potentially expensive and shouldn't be necessary for something as simple as frame count
while (tr.ReadLine() is string line)
{
if (line.Length > 0 && line[0] == '|')
{
FrameCount++;
}
}
while (tr.ReadLine() is string line) if (line.StartsWith('|')) FrameCount++;
});
}
}

View File

@ -12,7 +12,7 @@ namespace BizHawk.Client.Common
public static char Lookup(string button, string systemId)
{
var key = button.Replace("Key ", "");
if (key.StartsWith("P"))
if (key.StartsWith('P'))
{
if (key.Length > 2 && key[1] == '1' && key[2] >= '0' && key[2] <= '9') // Hack to support 10-20 controllers, TODO: regex this thing instead
{

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.Common
{
@ -53,7 +54,7 @@ namespace BizHawk.Client.Common
string line;
while ((line = reader.ReadLine()) != null)
{
if (line[0] == '|')
if (line.StartsWith('|'))
{
Log.Add(line);
}
@ -117,7 +118,7 @@ namespace BizHawk.Client.Common
string line;
while ((line = reader.ReadLine()) != null)
{
if (line[0] == '|')
if (line.StartsWith('|'))
{
newLog.Add(line);
}

View File

@ -1,5 +1,7 @@
using System.Collections.Generic;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.Common
{
internal class BkmHeader : Dictionary<string, string>
@ -62,7 +64,7 @@ namespace BizHawk.Client.Common
{
Comments.Add(line.Substring(8, line.Length - 8));
}
else if (line[0] == '|')
else if (line.StartsWith('|'))
{
return false;
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -63,7 +64,7 @@ namespace BizHawk.Client.Common
else if (Header.ParseLineFromFile(line))
{
}
else if (line.StartsWith("|"))
else if (line.StartsWith('|'))
{
_log.Add(line);
}

View File

@ -1,6 +1,9 @@
using System;
using System.IO;
using System.Linq;
using BizHawk.Common.StringExtensions;
using Newtonsoft.Json;
namespace BizHawk.Client.Common
@ -124,7 +127,7 @@ namespace BizHawk.Client.Common
break;
}
if (line[0] == '|')
if (line.StartsWith('|'))
{
VerificationLog.Add(line);
}

View File

@ -4,6 +4,8 @@ using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -226,7 +228,7 @@ namespace BizHawk.Client.Common
string line;
while ((line = reader.ReadLine()) != null)
{
if (line[0] == '|')
if (line.StartsWith('|'))
{
newLog.Add(line);
if (!timelineBranchFrame.HasValue && counter < Log.Count && line != Log[counter])

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
@ -40,7 +41,7 @@ namespace BizHawk.Client.EmuHawk.ToolExtensions
//sentinel for newer format OpenAdvanced type code
if (romLoading)
{
if (filename.StartsWith("*"))
if (filename.StartsWith('*'))
{
var oa = OpenAdvancedSerializer.ParseWithLegacy(filename);
caption = oa.DisplayName;

View File

@ -9,6 +9,7 @@ using BizHawk.Common.NumberExtensions;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -1283,7 +1284,7 @@ namespace BizHawk.Client.EmuHawk
int maxDigits = range.MaxDigits;
int curDigits = _axisTypedValue.Length;
string curMinus;
if (_axisTypedValue.StartsWith("-"))
if (_axisTypedValue.StartsWith('-'))
{
curDigits -= 1;
curMinus = "-";
@ -1323,7 +1324,7 @@ namespace BizHawk.Client.EmuHawk
}
else if (e.KeyCode == Keys.OemMinus || e.KeyCode == Keys.Subtract)
{
_axisTypedValue = _axisTypedValue.StartsWith("-")
_axisTypedValue = _axisTypedValue.StartsWith('-')
? _axisTypedValue.Substring(1)
: $"-{_axisTypedValue}";
}

View File

@ -8,6 +8,7 @@ using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.ToolExtensions;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -1258,7 +1259,7 @@ namespace BizHawk.Client.EmuHawk
{
int player;
if (column.Name.StartsWith("P") && column.Name.Length > 1 && char.IsNumber(column.Name, 1))
if (column.Name.Length >= 2 && column.Name.StartsWith('P') && char.IsNumber(column.Name, 1))
{
player = int.Parse(column.Name[1].ToString());
}

View File

@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
namespace BizHawk.Common.StringExtensions
{
@ -31,7 +32,8 @@ namespace BizHawk.Common.StringExtensions
/// <paramref name="str"/> with the first char removed, or
/// <paramref name="notFoundValue"/> if the first char of <paramref name="str"/> is not <paramref name="prefix"/>
/// </returns>
public static string RemovePrefix(this string str, char prefix, string notFoundValue) => str.Length != 0 && str[0] == prefix ? str.Substring(1, str.Length - 1) : notFoundValue;
public static string RemovePrefix(this string str, char prefix, string notFoundValue)
=> str.StartsWith(prefix) ? str.Substring(1) : notFoundValue;
/// <returns>
/// <paramref name="str"/> with the leading substring <paramref name="prefix"/> removed, or
@ -66,6 +68,14 @@ namespace BizHawk.Common.StringExtensions
/// </returns>
public static string RemoveSuffix(this string str, string suffix, string notFoundValue) => str.EndsWith(suffix) ? str.Substring(0, str.Length - suffix.Length) : notFoundValue;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith(this ReadOnlySpan<char> str, char c)
=> str.Length >= 1 && str[0] == c;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith(this string str, char c)
=> str.Length >= 1 && str[0] == c;
/// <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

@ -9,6 +9,7 @@ using System.Text;
using System.Threading;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.Common
{
@ -104,12 +105,9 @@ namespace BizHawk.Emulation.Common
var line = reader.ReadLine() ?? "";
try
{
if (line.StartsWith(";"))
{
continue; // comment
}
if (line.StartsWith(';')) continue; // comment
if (line.StartsWith("#"))
if (line.StartsWith('#'))
{
LoadDatabase_Escape(line, inUser: inUser, silent: silent);
continue;

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Atari.Jaguar
@ -58,7 +59,7 @@ namespace BizHawk.Emulation.Cores.Atari.Jaguar
{
_core.SetRegister(gpu ? 146 : 147, value);
}
else if (regName.StartsWith("R"))
else if (regName.StartsWith('R'))
{
var offset = gpu ? 18 : 82;
var reg = int.Parse(regName.Remove(0, 1));

View File

@ -4,6 +4,7 @@ using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
//check out ccd2iso linux program?
//https://wiki.archlinux.org/index.php/CD_Burning#TOC.2FCUE.2FBIN_for_mixed-mode_disks advice here
@ -189,7 +190,7 @@ namespace BizHawk.Emulation.DiscSystem
var line = sr.ReadLine();
if (line is null) break;
if (line == string.Empty) continue;
if (line.StartsWith("["))
if (line.StartsWith('['))
{
currSection = new()
{

View File

@ -3,6 +3,8 @@
using System.Text.RegularExpressions;
using System.IO;
using BizHawk.Common.StringExtensions;
//http://digitalx.org/cue-sheet/index.html "all cue sheet information is a straight 1:1 copy from the cdrwin helpfile"
//http://www.gnu.org/software/libcdio/libcdio.html#Sectors
//this is actually a great reference. they use LSN instead of LBA.. maybe a good idea for us
@ -155,9 +157,7 @@ namespace BizHawk.Emulation.DiscSystem.CUE
}
}
bool startsWithSemicolon = key.StartsWith(";");
if (startsWithSemicolon)
if (key.StartsWith(';'))
{
clp.EOF = true;
OUT_CueFile.Commands.Add(new CUE_File.Command.COMMENT(line));
@ -374,7 +374,7 @@ namespace BizHawk.Emulation.DiscSystem.CUE
if (!clp.EOF)
{
var remainder = clp.ReadLine();
if (remainder.TrimStart().StartsWith(";"))
if (remainder.TrimStart().StartsWith(';'))
{
//add a comment
OUT_CueFile.Commands.Add(new CUE_File.Command.COMMENT(remainder));

View File

@ -1,6 +1,8 @@
using System.IO;
using System.Collections.Generic;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.DiscSystem
{
public class M3U_File
@ -21,7 +23,7 @@ namespace BizHawk.Emulation.DiscSystem
var line = sr.ReadLine();
if (line == null)
break;
if (line.StartsWith("#"))
if (line.StartsWith('#'))
{
if (line == "#EXTM3U")
{