Clean up some core functionality

This commit is contained in:
Matt Nadareski
2024-10-19 12:07:43 -04:00
parent c9bff5e0aa
commit c8f708d1ae
5 changed files with 22 additions and 23 deletions

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
namespace SabreTools.Core
{
@@ -8,7 +7,7 @@ namespace SabreTools.Core
public static ConcurrentList<T> ToConcurrentList<T>(this IEnumerable<T> values)
{
var list = new ConcurrentList<T>();
list.SetInternalList(values.ToList());
list.SetInternalList([.. values]);
return list;
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SabreTools.Logging;
namespace SabreTools.Core.Tools
@@ -87,7 +86,7 @@ namespace SabreTools.Core.Tools
// Try to get the mapping attribute
MappingAttribute? attr = AttributeHelper<T>.GetAttribute(value);
if (attr?.Mappings == null || !attr.Mappings.Any())
if (attr?.Mappings == null || attr.Mappings.Length == 0)
continue;
// Loop through the mappings and add each
@@ -170,7 +169,7 @@ namespace SabreTools.Core.Tools
// Try to get the mapping attribute
MappingAttribute? attr = AttributeHelper<T>.GetAttribute(value);
if (attr?.Mappings == null || !attr.Mappings.Any())
if (attr?.Mappings == null || attr.Mappings.Length == 0)
continue;
// Use either the first or second item in the list

View File

@@ -141,25 +141,23 @@ namespace SabreTools.Core.Tools
if (DetermineMultiplier(value) > 1)
value = value.TrimEnd(['k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ']);
#if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0
return value.All(c => char.IsNumber(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || c == '.' || c == ',');
#elif NET7_0_OR_GREATER
#if NET7_0_OR_GREATER
return value.All(c => char.IsAsciiHexDigit(c) || c == '.' || c == ',');
#else
return value.All(c => c.IsAsciiHexDigit() || c == '.' || c == ',');
#endif
}
#if NET6_0
#if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0 || NET6_0
/// <summary>
/// Indicates whether a character is categorized as an ASCII hexademical digit.
/// </summary>
/// <param name="c">The character to evaluate.</param>
/// <returns>true if c is a hexademical digit; otherwise, false.</returns>
/// <remarks>This method determines whether the character is in the range '0' through '9', inclusive, 'A' through 'F', inclusive, or 'a' through 'f', inclusive.</remarks>
private static bool IsAsciiHexDigit(this char c)
internal static bool IsAsciiHexDigit(this char c)
{
return c switch
return char.ToLowerInvariant(c) switch
{
'0' => true,
'1' => true,
@@ -172,17 +170,11 @@ namespace SabreTools.Core.Tools
'8' => true,
'9' => true,
'a' => true,
'A' => true,
'b' => true,
'B' => true,
'c' => true,
'C' => true,
'd' => true,
'D' => true,
'e' => true,
'E' => true,
'f' => true,
'F' => true,
_ => false,
};
}

View File

@@ -124,7 +124,12 @@ namespace SabreTools.Core.Tools
return input;
List<char> invalidPath = [.. Path.GetInvalidPathChars()];
return new string(input.Where(c => !invalidPath.Contains(c)).ToArray());
foreach (char invalid in Path.GetInvalidPathChars())
{
input = input!.Replace(invalid.ToString(), string.Empty);
}
return input;
}
/// <summary>
@@ -223,7 +228,11 @@ namespace SabreTools.Core.Tools
hash = hash.ToLowerInvariant();
// Otherwise, make sure that every character is a proper match
if (hash.Any(c => (c < '0' || c > '9') && (c < 'a' || c > 'f')))
#if NET7_0_OR_GREATER
if (hash.Any(c => char.IsAsciiHexDigit(c)))
#else
if (hash.Any(c => c.IsAsciiHexDigit()))
#endif
hash = string.Empty;
return hash;

View File

@@ -53,11 +53,11 @@ namespace SabreTools.Core.Tools
List<Type> assemblyTypes = [];
try
{
assemblyTypes = assembly.GetTypes().ToList<Type>();
assemblyTypes = [.. assembly.GetTypes()];
}
catch (ReflectionTypeLoadException rtle)
{
assemblyTypes = rtle.Types.Where(t => t != null)!.ToList<Type>();
assemblyTypes = [.. rtle.Types.Where(t => t != null)];
}
// Loop through all types
@@ -92,11 +92,11 @@ namespace SabreTools.Core.Tools
List<Type> assemblyTypes = [];
try
{
assemblyTypes = assembly.GetTypes().ToList<Type>();
assemblyTypes = [.. assembly.GetTypes()];
}
catch (ReflectionTypeLoadException rtle)
{
assemblyTypes = rtle.Types.Where(t => t != null)!.ToList<Type>();
assemblyTypes = [.. rtle.Types.Where(t => t != null)];
}
// Loop through all types