From 45fd7ee81ea3fb0ff2e245ae128c084602944eb6 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 17 May 2020 10:39:24 -0500 Subject: [PATCH] nuke most of SubstringExtensions, and make it where i can at least read it --- .../Extensions/SubstringExtensions.cs | 219 ++---------------- 1 file changed, 19 insertions(+), 200 deletions(-) diff --git a/src/BizHawk.Common/Extensions/SubstringExtensions.cs b/src/BizHawk.Common/Extensions/SubstringExtensions.cs index 804cfdd964..5077c0fa77 100644 --- a/src/BizHawk.Common/Extensions/SubstringExtensions.cs +++ b/src/BizHawk.Common/Extensions/SubstringExtensions.cs @@ -1,211 +1,30 @@ -using System; - -namespace BizHawk.Common.StringExtensions +namespace BizHawk.Common.StringExtensions { public static class SubstringExtensions { - /// with the first char removed, or the original if the first char of is not - public static string RemovePrefix(this string str, char prefix) => str.RemovePrefix(prefix, notFoundValue: str); + /// + /// with the last char removed, or the original + /// if the last char of is not + /// + public static string RemoveSuffix(this string str, char suffix) => + str.Length != 0 && str[str.Length - 1] == suffix + ? str.Substring(0, str.Length - 1) + : str; - /// with the first char removed, or if the first char of is not - public static string RemovePrefix(this string str, char prefix, string notFoundValue) => str.Length != 0 && str[0] == prefix ? str.Substring(1, str.Length - 1) : notFoundValue; - - /// with the leading substring removed, or the original if does not start with - public static string RemovePrefix(this string str, string prefix) => str.RemovePrefix(prefix, notFoundValue: str); - - /// with the leading substring removed, or if does not start with - public static string RemovePrefix(this string str, string prefix, string notFoundValue) => str.StartsWith(prefix) ? str.Substring(prefix.Length, str.Length - prefix.Length) : notFoundValue; - - /// with the first char removed, or string.Empty if the first char of is not - public static string RemovePrefixOrEmpty(this string str, char prefix) => str.RemovePrefix(prefix, notFoundValue: str); - - /// with the leading substring removed, or string.Empty if does not start with - public static string RemovePrefixOrEmpty(this string str, string prefix) => str.RemovePrefix(prefix, notFoundValue: str); - - /// with the first char removed, or < if the first char of is not - public static string? RemovePrefixOrNull(this string str, char prefix) => str.Length != 0 && str[0] == prefix ? str.Substring(1, str.Length - 1) : null; - - /// with the leading substring removed, or if does not start with - public static string? RemovePrefixOrNull(this string str, string prefix) => str.StartsWith(prefix) ? str.Substring(prefix.Length, str.Length - prefix.Length) : null; - - /// with the last char removed, or the original if the last char of is not - public static string RemoveSuffix(this string str, char suffix) => str.RemoveSuffix(suffix, notFoundValue: str); - - /// with the last char removed, or if the last char of is not - public static string RemoveSuffix(this string str, char suffix, string notFoundValue) => str.Length != 0 && str[str.Length - 1] == suffix ? str.Substring(0, str.Length - 1) : notFoundValue; - - /// with the trailing substring removed, or the original if does not end with - public static string RemoveSuffix(this string str, string suffix) => str.RemoveSuffix(suffix, notFoundValue: str); - - /// with the trailing substring removed, or if does not end with - public static string RemoveSuffix(this string str, string suffix, string notFoundValue) => str.EndsWith(suffix) ? str.Substring(0, str.Length - suffix.Length) : notFoundValue; - - /// with the last char removed, or string.Empty if the last char of is not - public static string RemoveSuffixOrEmpty(this string str, char suffix) => str.RemoveSuffix(suffix, notFoundValue: str); - - /// with the trailing substring removed, or string.Empty if does not end with - public static string RemoveSuffixOrEmpty(this string str, string suffix) => str.RemoveSuffix(suffix, notFoundValue: str); - - /// with the last char removed, or < if the last char of is not - public static string? RemoveSuffixOrNull(this string str, char suffix) => str.Length != 0 && str[str.Length - 1] == suffix ? str.Substring(0, str.Length - 1) : null; - - /// with the trailing substring removed, or if does not end with - public static string? RemoveSuffixOrNull(this string str, string suffix) => str.EndsWith(suffix) ? str.Substring(0, str.Length - suffix.Length) : null; - - /// the substring of after the first occurrence of , or the original if not found - public static string SubstringAfter(this string str, char delimiter) => str.SubstringAfter(delimiter, notFoundValue: str); - - /// the substring of after the first occurrence of , or if not found - public static string SubstringAfter(this string str, char delimiter, string notFoundValue) + /// + /// the substring of before the first occurrence of + /// , or the original if not found + /// + public static string SubstringBefore(this string str, char delimiter) { var index = str.IndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(index + 1, str.Length - index - 1); + return index < 0 ? str : str.Substring(0, index); } - /// the substring of after the first occurrence of , or the original if not found - public static string SubstringAfter(this string str, string delimiter) => str.SubstringAfter(delimiter, notFoundValue: str); - - /// the substring of after the first occurrence of , or if not found - public static string SubstringAfter(this string str, string delimiter, string notFoundValue) - { - var index = str.IndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(index + delimiter.Length, str.Length - index - delimiter.Length); - } - - /// the substring of after the last occurrence of , or the original if not found - public static string SubstringAfterLast(this string str, char delimiter) => str.SubstringAfterLast(delimiter, notFoundValue: str); - - /// the substring of after the last occurrence of , or if not found - public static string SubstringAfterLast(this string str, char delimiter, string notFoundValue) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(index + 1, str.Length - index - 1); - } - - /// the substring of after the last occurrence of , or the original if not found - public static string SubstringAfterLast(this string str, string delimiter) => str.SubstringAfterLast(delimiter, notFoundValue: str); - - /// the substring of after the last occurrence of , or if not found - public static string SubstringAfterLast(this string str, string delimiter, string notFoundValue) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(index + delimiter.Length, str.Length - index - delimiter.Length); - } - - /// the substring of after the last occurrence of , or string.Empty if not found - public static string SubstringAfterLastOrEmpty(this string str, char delimiter) => str.SubstringAfterLast(delimiter, notFoundValue: str); - - /// the substring of after the last occurrence of , or string.Empty if not found - public static string SubstringAfterLastOrEmpty(this string str, string delimiter) => str.SubstringAfterLast(delimiter, notFoundValue: str); - - /// the substring of after the last occurrence of , or if not found - public static string? SubstringAfterLastOrNull(this string str, char delimiter) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? null : str.Substring(index + 1, str.Length - index - 1); - } - - /// the substring of after the last occurrence of , or if not found - public static string? SubstringAfterLastOrNull(this string str, string delimiter) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? null : str.Substring(index + delimiter.Length, str.Length - index - delimiter.Length); - } - - /// the substring of after the first occurrence of , or string.Empty if not found - public static string SubstringAfterOrEmpty(this string str, char delimiter) => str.SubstringAfter(delimiter, notFoundValue: str); - - /// the substring of after the first occurrence of , or string.Empty if not found - public static string SubstringAfterOrEmpty(this string str, string delimiter) => str.SubstringAfter(delimiter, notFoundValue: str); - - /// the substring of after the first occurrence of , or if not found - public static string? SubstringAfterOrNull(this string str, char delimiter) - { - var index = str.IndexOf(delimiter); - return index < 0 ? null : str.Substring(index + 1, str.Length - index - 1); - } - - /// the substring of after the first occurrence of , or if not found - public static string? SubstringAfterOrNull(this string str, string delimiter) - { - var index = str.IndexOf(delimiter); - return index < 0 ? null : str.Substring(index + delimiter.Length, str.Length - index - delimiter.Length); - } - - /// the substring of before the first occurrence of , or the original if not found - public static string SubstringBefore(this string str, char delimiter) => str.SubstringBefore(delimiter, notFoundValue: str); - - /// the substring of before the first occurrence of , or if not found - public static string SubstringBefore(this string str, char delimiter, string notFoundValue) - { - var index = str.IndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(0, index); - } - - /// the substring of before the first occurrence of , or the original if not found - public static string SubstringBefore(this string str, string delimiter) => str.SubstringBefore(delimiter, notFoundValue: str); - - /// the substring of before the first occurrence of , or if not found - public static string SubstringBefore(this string str, string delimiter, string notFoundValue) - { - var index = str.IndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(0, index); - } - - /// the substring of before the last occurrence of , or the original if not found - public static string SubstringBeforeLast(this string str, char delimiter) => str.SubstringBeforeLast(delimiter, notFoundValue: str); - - /// the substring of before the last occurrence of , or if not found - public static string SubstringBeforeLast(this string str, char delimiter, string notFoundValue) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(0, index); - } - - /// the substring of before the last occurrence of , or the original if not found - public static string SubstringBeforeLast(this string str, string delimiter) => str.SubstringBeforeLast(delimiter, notFoundValue: str); - - /// the substring of before the last occurrence of , or if not found - public static string SubstringBeforeLast(this string str, string delimiter, string notFoundValue) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? notFoundValue : str.Substring(0, index); - } - - /// the substring of before the last occurrence of , or string.Empty if not found - public static string SubstringBeforeLastOrEmpty(this string str, char delimiter) => str.SubstringBeforeLast(delimiter, notFoundValue: str); - - /// the substring of before the last occurrence of , or string.Empty if not found - public static string SubstringBeforeLastOrEmpty(this string str, string delimiter) => str.SubstringBeforeLast(delimiter, notFoundValue: str); - - /// the substring of before the last occurrence of , or if not found - public static string? SubstringBeforeLastOrNull(this string str, char delimiter) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? null : str.Substring(0, index); - } - - /// the substring of before the last occurrence of , or if not found - public static string? SubstringBeforeLastOrNull(this string str, string delimiter) - { - var index = str.LastIndexOf(delimiter); - return index < 0 ? null : str.Substring(0, index); - } - - /// the substring of before the first occurrence of , or string.Empty if not found - public static string SubstringBeforeOrEmpty(this string str, char delimiter) => str.SubstringBefore(delimiter, notFoundValue: str); - - /// the substring of before the first occurrence of , or string.Empty if not found - public static string SubstringBeforeOrEmpty(this string str, string delimiter) => str.SubstringBefore(delimiter, notFoundValue: str); - - /// the substring of before the first occurrence of , or if not found - public static string? SubstringBeforeOrNull(this string str, char delimiter) - { - var index = str.IndexOf(delimiter); - return index < 0 ? null : str.Substring(0, index); - } - - /// the substring of before the first occurrence of , or if not found + /// + /// the substring of before the first occurrence of + /// , or if not found + /// public static string? SubstringBeforeOrNull(this string str, string delimiter) { var index = str.IndexOf(delimiter);