Cleanup StringExtensions

This commit is contained in:
YoshiRulz 2020-02-19 08:25:19 +10:00
parent 9c15ca1369
commit be60911ff9
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 154 additions and 325 deletions

View File

@ -105,7 +105,7 @@ namespace BizHawk.Client.DiscoHawk
if (files == null) return new List<string>(); if (files == null) return new List<string>();
foreach (string str in files) foreach (string str in files)
{ {
string ext = Path.GetExtension(str)?.ToUpper(); var ext = Path.GetExtension(str) ?? string.Empty;
if(!ext.In(".CUE", ".ISO", ".CCD", ".MDS")) if(!ext.In(".CUE", ".ISO", ".CCD", ".MDS"))
{ {
return new List<string>(); return new List<string>();

View File

@ -1,376 +1,205 @@
#nullable disable using System;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace BizHawk.Common.StringExtensions namespace BizHawk.Common.StringExtensions
{ {
/// <remarks>TODO how many of these <c>Is*</c>/<c>Only*</c> methods' callers can use <see cref="int.TryParse(string,out int)">int.TryParse</see> or similar instead? --yoshi</remarks>
public static class StringExtensions public static class StringExtensions
{ {
public static string GetPrecedingString(this string str, string value) #pragma warning disable CS8602 // no idea --yoshi
/// <returns>the substring of <paramref name="str"/> before the first occurrence of <paramref name="value"/>, or <see langword="null"/> if not found</returns>
public static string? GetPrecedingString(this string str, string value)
{ {
var index = str.IndexOf(value); var index = str.IndexOf(value);
return index < 0 ? null : str.Substring(0, index);
if (index < 0)
{
return null;
}
if (index == 0)
{
return "";
}
return str.Substring(0, index);
} }
public static bool In(this string str, params string[] options) /// <returns><see langword="true"/> iff <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.InvariantCultureIgnoreCase));
return options.Any(opt => opt.Equals(str, StringComparison.CurrentCultureIgnoreCase));
}
public static bool NotIn(this string str, params string[] options) /// <returns>how many times <paramref name="c"/> appears in <paramref name="str"/>, or <c>0</c> if <paramref name="str"/> is null</returns>
{ public static int HowMany(this string? str, char c) => string.IsNullOrEmpty(str) ? 0 : str.Count(t => t == c);
return options.All(opt => opt.ToLower() != str.ToLower());
}
public static bool NotIn(this string str, IEnumerable<string> options) /// <returns>how many times <paramref name="sub"/> appears in <paramref name="str"/>, or <c>0</c> if <paramref name="str"/> is null</returns>
/// <remarks>
/// occurrences may overlap, for example <c>"AAA".HowMany("AA")</c> returns <c>2</c><br/>
/// TODO except it doesn't, but <c>"AAAB".HowMany("AA")</c> does. I left this bug in so as to not break anything. --yoshi
/// </remarks>
public static int HowMany(this string? str, string sub)
{ {
return options.All(opt => opt.ToLower() != str.ToLower()); if (string.IsNullOrEmpty(str)) return 0;
}
public static int HowMany(this string str, char c)
{
return !string.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0;
}
public static int HowMany(this string str, string s)
{
if (str == null)
{
return 0;
}
var count = 0; var count = 0;
for (var i = 0; i < (str.Length - s.Length); i++) var substrLength = sub.Length;
for (int i = 0, l = str.Length - substrLength; i < l; i++)
{ {
if (str.Substring(i, s.Length) == s) if (string.Equals(str.Substring(i, substrLength), sub, StringComparison.InvariantCulture)) count++;
{
count++;
}
} }
return count; return count;
} }
#region String and Char validation extensions /// <returns><see langword="true"/> iff <paramref name="str"/> is not <see langword="null"/> and all chars of <paramref name="str"/> are digits</returns>
public static bool IsUnsigned(this string? str) => !string.IsNullOrWhiteSpace(str) && str.All(IsUnsigned);
/// <summary> /// <returns><see langword="true"/> iff <paramref name="c"/> is a digit</returns>
/// Validates all chars are 0-9 public static bool IsUnsigned(this char c) => char.IsDigit(c);
/// </summary>
public static bool IsUnsigned(this string str) /// <returns>
/// <see langword="true"/> iff <paramref name="str"/> is not <see langword="null"/>,
/// the first char of <paramref name="str"/> is <c>'-'</c> or a digit, and
/// all subsequent chars of <paramref name="str"/> are digits
/// </returns>
public static bool IsSigned(this string? str) => !string.IsNullOrWhiteSpace(str) && str[0].IsSigned() && str.Substring(1).All(IsUnsigned);
/// <returns><see langword="true"/> iff <paramref name="c"/> is <c>'-'</c> or a digit</returns>
public static bool IsSigned(this char c) => IsUnsigned(c) || c == '-';
/// <returns><see langword="true"/> iff <paramref name="str"/> is not <see langword="null"/> and all chars of <paramref name="str"/> are hex digits (<c>[0-9A-Fa-f]</c>)</returns>
/// <remarks><paramref name="str"/> should exclude the prefix <c>0x</c></remarks>
public static bool IsHex(this string? str) => !string.IsNullOrWhiteSpace(str) && str.All(IsHex);
/// <returns><see langword="true"/> iff <paramref name="c"/> is a hex digit (<c>[0-9A-Fa-f]</c>)</returns>
public static bool IsHex(this char c) => IsUnsigned(c) || 'A' <= char.ToUpperInvariant(c) && char.ToUpperInvariant(c) <= 'F';
/// <returns><see langword="true"/> iff <paramref name="str"/> is not <see langword="null"/> and all chars of <paramref name="str"/> are either <c>'0'</c> or <c>'1'</c></returns>
/// <remarks><paramref name="str"/> should exclude the prefix <c>0b</c></remarks>
public static bool IsBinary(this string? str) => !string.IsNullOrWhiteSpace(str) && str.All(IsBinary);
/// <returns><see langword="true"/> iff <paramref name="c"/> is either <c>'0'</c> or <c>'1'</c></returns>
public static bool IsBinary(this char c) => c == '0' || c == '1';
/// <returns>
/// <see langword="true"/> iff <paramref name="str"/> is not <see langword="null"/>,<br/>
/// all chars of <paramref name="str"/> are <c>'.'</c> or a digit, and<br/>
/// <paramref name="str"/> contains at most <c>1</c> decimal separator <c>'.'</c>
/// </returns>
/// <remarks>
/// <paramref name="str"/> should exclude the suffix <c>M</c>.<br/>
/// This method returning <see langword="true"/> for some <paramref name="str"/> does not imply that <see cref="float.TryParse(string,out float)">float.TryParse</see> will also return <see langword="true"/>.<br/>
/// Also this has nothing to do with fixed- vs. floating-point numbers, a better name would be <c>IsUnsignedDecimal</c>.
/// </remarks>
public static bool IsFixedPoint(this string? str) => !string.IsNullOrWhiteSpace(str) && str.HowMany('.') <= 1 && str.All(IsFixedPoint);
/// <returns><see langword="true"/> iff <paramref name="c"/> is <c>'.'</c> or a digit</returns>
/// <remarks>Also this has nothing to do with fixed- vs. floating-point numbers, a better name would be <c>IsUnsignedDecimal</c>.</remarks>
public static bool IsFixedPoint(this char c) => IsUnsigned(c) || c == '.';
/// <returns>
/// <see langword="true"/> iff <paramref name="str"/> is not <see langword="null"/>,<br/>
/// the first char of <paramref name="str"/> is <c>'-'</c>, <c>'.'</c>, or a digit,<br/>
/// all subsequent chars of <paramref name="str"/> are <c>'.'</c> or a digit, and<br/>
/// <paramref name="str"/> contains at most <c>1</c> decimal separator <c>'.'</c>
/// </returns>
/// <remarks>
/// <paramref name="str"/> should exclude the suffix <c>f</c>.<br/>
/// This method returning <see langword="true"/> for some <paramref name="str"/> does not imply that <see cref="float.TryParse(string,out float)">float.TryParse</see> will also return <see langword="true"/>.<br/>
/// Also this has nothing to do with fixed- vs. floating-point numbers, a better name would be <c>IsSignedDecimal</c>.
/// </remarks>
public static bool IsFloat(this string? str) => !string.IsNullOrWhiteSpace(str) && str.HowMany('.') <= 1 && str[0].IsFloat() && str.Substring(1).All(IsFixedPoint);
/// <returns><see langword="true"/> iff <paramref name="c"/> is <c>'-'</c>, <c>'.'</c>, or a digit</returns>
/// <remarks>Also this has nothing to do with fixed- vs. floating-point numbers, a better name would be <c>IsSignedDecimal</c>.</remarks>
public static bool IsFloat(this char c) => c.IsFixedPoint() || c == '-';
/// <returns>
/// A copy of <paramref name="raw"/> with characters removed so that the whole thing passes <see cref="IsBinary(string?)">IsBinary</see>.<br/>
/// That is, all chars of the copy will be either <c>'0'</c> or <c>'1'</c>.
/// </returns>
public static string OnlyBinary(this string? raw) => string.IsNullOrWhiteSpace(raw) ? string.Empty : string.Concat(raw.Where(IsBinary));
/// <returns>
/// A copy of <paramref name="raw"/> with characters removed so that the whole thing passes <see cref="IsUnsigned(string?)">IsUnsigned</see>.<br/>
/// That is, all chars of the copy will be digits.
/// </returns>
public static string OnlyUnsigned(this string? raw) => string.IsNullOrWhiteSpace(raw) ? string.Empty : string.Concat(raw.Where(IsUnsigned));
/// <returns>
/// A copy of <paramref name="raw"/> with characters removed so that the whole thing passes <see cref="IsSigned(string?)">IsSigned</see>.<br/>
/// That is, the first char of the copy will be <c>'-'</c> or a digit, and all subsequent chars of the copy will be digits.
/// </returns>
/// <remarks>If <paramref name="raw"/> contains a serialized negative integer, it must be at the start (<paramref name="raw"/><c>[0] == '-'</c>) or the sign will be dropped.</remarks>
public static string OnlySigned(this string? raw)
{ {
if (string.IsNullOrWhiteSpace(str)) if (string.IsNullOrWhiteSpace(raw)) return string.Empty;
{ return raw[0].IsSigned()
return false; ? raw[0] + string.Concat(raw.Skip(1).Where(IsUnsigned))
} : string.Concat(raw.Skip(1).Where(IsUnsigned));
return str.All(IsUnsigned);
} }
/// <summary> /// <returns>
/// Validates the char is 0-9 /// A copy of <paramref name="raw"/> with characters removed so that the whole thing passes <see cref="IsHex(string?)">IsHex</see>.<br/>
/// </summary> /// That is, all chars of the copy will be hex digits (<c>[0-9A-F]</c>).
public static bool IsUnsigned(this char c) /// </returns>
public static string OnlyHex(this string? raw) => string.IsNullOrWhiteSpace(raw) ? string.Empty : string.Concat(raw.Where(IsHex)).ToUpperInvariant();
/// <returns>
/// A copy of <paramref name="raw"/> with characters removed so that the whole thing passes <see cref="IsFixedPoint(string?)">IsFixedPoint</see>.<br/>
/// That is, the all chars of the copy will be <c>'.'</c> or a digit and the copy will contain at most <c>1</c> decimal separator <c>'.'</c>.
/// </returns>
/// <remarks>
/// The returned value may not be parseable by <see cref="float.TryParse(string,out float)">float.TryParse</see>.<br/>
/// Also this has nothing to do with fixed- vs. floating-point numbers, a better name would be <c>IsUnsignedDecimal</c>.
/// </remarks>
public static string OnlyFixedPoint(this string? raw)
{ {
return char.IsDigit(c); if (string.IsNullOrWhiteSpace(raw)) return string.Empty;
}
/// <summary>
/// Validates all chars are 0-9, or a dash as the first value
/// </summary>
public static bool IsSigned(this string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return false;
}
return str[0].IsSigned() && str.Substring(1).All(IsUnsigned);
}
/// <summary>
/// Validates the char is 0-9 or a dash
/// </summary>
public static bool IsSigned(this char c)
{
return char.IsDigit(c) || c == '-';
}
/// <summary>
/// Validates all chars are 0-9, A-F or a-f
/// </summary>
public static bool IsHex(this string str)
{
return !string.IsNullOrWhiteSpace(str) && str.All(IsHex);
}
/// <summary>
/// Validates the char is 0-9, A-F or a-f
/// </summary>
public static bool IsHex(this char c)
{
if (char.IsDigit(c))
{
return true;
}
return char.ToUpperInvariant(c) >= 'A' && char.ToUpperInvariant(c) <= 'F';
}
/// <summary>
/// Validates all chars are 0 or 1
/// </summary>
public static bool IsBinary(this string str)
{
return !string.IsNullOrWhiteSpace(str) && str.All(IsBinary);
}
/// <summary>
/// Validates the char is 0 or 1
/// </summary>
public static bool IsBinary(this char c)
{
return c == '0' || c == '1';
}
/// <summary>
/// Validates all chars are 0-9, a decimal point, and that there is no more than 1 decimal point, can not be signed
/// </summary>
public static bool IsFixedPoint(this string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return false;
}
return str.HowMany('.') <= 1
&& str.All(IsFixedPoint);
}
/// <summary>
/// Validates the char is 0-9, a dash, or a decimal
/// </summary>
public static bool IsFixedPoint(this char c)
{
return c.IsUnsigned() || c == '.';
}
/// <summary>
/// Validates all chars are 0-9 or decimal, and that there is no more than 1 decimal point, a dash can be the first character
/// </summary>
public static bool IsFloat(this string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return false;
}
return str.HowMany('.') <= 1
&& str[0].IsFloat()
&& str.Substring(1).All(IsFixedPoint);
}
/// <summary>
/// Validates that the char is 0-9, a dash, or a decimal point
/// </summary>
public static bool IsFloat(this char c)
{
return c.IsFixedPoint() || c == '-';
}
/// <summary>
/// Takes any string and removes any value that is not a valid binary value (0 or 1)
/// </summary>
public static string OnlyBinary(this string raw)
{
if (string.IsNullOrWhiteSpace(raw))
{
return "";
}
var output = new StringBuilder(); var output = new StringBuilder();
foreach (var chr in raw)
{
if (IsBinary(chr))
{
output.Append(chr);
}
}
return output.ToString();
}
/// <summary>
/// Takes any string and removes any value that is not a valid unsigned integer value (0-9)
/// </summary>
public static string OnlyUnsigned(this string raw)
{
if (string.IsNullOrWhiteSpace(raw))
{
return "";
}
var output = new StringBuilder();
foreach (var chr in raw)
{
if (IsUnsigned(chr))
{
output.Append(chr);
}
}
return output.ToString();
}
/// <summary>
/// Takes any string and removes any value that is not a valid unsigned integer value (0-9 or -)
/// Note: a "-" will only be kept if it is the first digit
/// </summary>
public static string OnlySigned(this string raw)
{
if (string.IsNullOrWhiteSpace(raw))
{
return "";
}
var output = new StringBuilder();
int count = 0;
foreach (var chr in raw)
{
if (count == 0 && chr == '-')
{
output.Append(chr);
}
else if (IsUnsigned(chr))
{
output.Append(chr);
}
count++;
}
return output.ToString();
}
/// <summary>
/// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase
/// </summary>
public static string OnlyHex(this string raw)
{
if (string.IsNullOrWhiteSpace(raw))
{
return "";
}
var output = new StringBuilder(raw.Length);
foreach (var chr in raw)
{
if (IsHex(chr))
{
output.Append(char.ToUpper(chr));
}
}
return output.ToString();
}
/// <summary>
/// Takes any string and removes any value that is not a fixed point value (0-9 or .)
/// Note: only the first occurrence of a . will be kept
/// </summary>
public static string OnlyFixedPoint(this string raw)
{
if (string.IsNullOrWhiteSpace(raw))
{
return "";
}
var output = new StringBuilder();
var usedDot = false; var usedDot = false;
foreach (var chr in raw) foreach (var chr in raw)
{ {
if (chr == '.') if (chr == '.')
{ {
if (usedDot) if (usedDot) continue;
{ output.Append(chr);
continue;
}
usedDot = true; usedDot = true;
} }
else if (chr.IsUnsigned()) output.Append(chr);
if (IsFixedPoint(chr))
{
output.Append(chr);
}
} }
return output.ToString(); return output.ToString();
} }
/// <summary> /// <returns>
/// Takes any string and removes any value that is not a float point value (0-9, -, or .) /// A copy of <paramref name="raw"/> with characters removed so that the whole thing passes <see cref="IsFloat(string?)">IsFloat</see>.<br/>
/// Note: - is only valid as the first character, and only the first occurrence of a . will be kept /// That is, the first char of the copy will be <c>'-'</c>, <c>'.'</c>, or a digit,<br/>
/// </summary> /// all subsequent chars of the copy will be <c>'.'</c> or a digit, and<br/>
public static string OnlyFloat(this string raw) /// the copy will contain at most <c>1</c> decimal separator <c>'.'</c>.
/// </returns>
/// <remarks>
/// If <paramref name="raw"/> contains a serialized negative decimal, it must be at the start (<paramref name="raw"/><c>[0] == '-'</c>) or the sign will be dropped.<br/>
/// The returned value may not be parseable by <see cref="float.TryParse(string,out float)">float.TryParse</see>.<br/>
/// Also this has nothing to do with fixed- vs. floating-point numbers, a better name would be <c>IsSignedDecimal</c>.
/// </remarks>
public static string OnlyFloat(this string? raw)
{ {
if (string.IsNullOrWhiteSpace(raw)) if (string.IsNullOrWhiteSpace(raw)) return string.Empty;
{
return "";
}
var output = new StringBuilder(); var output = new StringBuilder();
var usedDot = false; var usedDot = false;
var count = 0;
foreach (var chr in raw) var first = raw[0];
if (first.IsFloat())
{ {
if (count == 0 && chr == '-') output.Append(first);
if (first == '.') usedDot = true;
}
for (int i = 1, l = raw.Length; i < l; i++)
{
var chr = raw[i];
if (chr == '.')
{ {
if (usedDot) continue;
output.Append(chr); output.Append(chr);
usedDot = true;
} }
else else if (chr.IsUnsigned()) output.Append(chr);
{
if (chr == '.')
{
if (usedDot)
{
continue;
}
usedDot = true;
}
if (IsFixedPoint(chr))
{
output.Append(chr);
}
}
count++;
} }
return output.ToString(); return output.ToString();
} }
#endregion #pragma warning restore CS8602
} }
} }

View File

@ -121,7 +121,7 @@ namespace BizHawk.Emulation.Cores.Components.M68000
foreach (var opcode in opList) foreach (var opcode in opList)
{ {
int opc = Convert.ToInt32(opcode, 2); int opc = Convert.ToInt32(opcode, 2);
if (Opcodes[opc] != null && instr.NotIn("movea", "andi2sr", "eori2sr", "ori2sr", "ext", "dbcc", "swap", "cmpm")) if (Opcodes[opc] != null && !instr.In("movea", "andi2sr", "eori2sr", "ori2sr", "ext", "dbcc", "swap", "cmpm"))
Console.WriteLine("Setting opcode for {0}, a handler is already set. overwriting. {1:X4}", instr, opc); Console.WriteLine("Setting opcode for {0}, a handler is already set. overwriting. {1:X4}", instr, opc);
Opcodes[opc] = exec; Opcodes[opc] = exec;
} }