From bb04a90952b0bec8b55b418728c2bf716fd8c6e0 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 10 Oct 2023 08:39:13 +0200 Subject: [PATCH] Refactor: Use Array.Empty() --- BytecodeApi.CommandLineParser/Option.cs | 2 +- BytecodeApi.CsvParser/CsvHelper.cs | 2 +- BytecodeApi.IniParser/IniFile.cs | 2 +- BytecodeApi.PEParser/ImageOptionalHeader.cs | 2 +- BytecodeApi.Penetration/ExecutableInjection.cs | 2 +- BytecodeApi.Win32/SystemInfo/TcpView.cs | 2 +- BytecodeApi.Wmi/WmiClass.cs | 2 +- BytecodeApi/ConvertEx.cs | 6 +++--- BytecodeApi/Data/Blob.cs | 2 +- BytecodeApi/Extensions/ImageExtensions.cs | 6 +++--- BytecodeApi/IO/CommandLine.cs | 2 +- BytecodeApi/IO/Network.cs | 2 +- BytecodeApi/IO/PathEx.cs | 4 ++-- BytecodeApi/Text/Wording.cs | 4 ++-- BytecodeApi/Validate.cs | 6 +++--- 15 files changed, 23 insertions(+), 23 deletions(-) diff --git a/BytecodeApi.CommandLineParser/Option.cs b/BytecodeApi.CommandLineParser/Option.cs index ce89c93..0f8d1bb 100644 --- a/BytecodeApi.CommandLineParser/Option.cs +++ b/BytecodeApi.CommandLineParser/Option.cs @@ -49,7 +49,7 @@ public Option(string[] arguments, string[]? alternatives) } Arguments = arguments.ToReadOnlyCollection(); - Alternatives = (alternatives ?? new string[0]).ToReadOnlyCollection(); + Alternatives = (alternatives ?? Array.Empty()).ToReadOnlyCollection(); } /// diff --git a/BytecodeApi.CsvParser/CsvHelper.cs b/BytecodeApi.CsvParser/CsvHelper.cs index 3c10b38..c063b91 100644 --- a/BytecodeApi.CsvParser/CsvHelper.cs +++ b/BytecodeApi.CsvParser/CsvHelper.cs @@ -30,7 +30,7 @@ public static IEnumerable EnumerateTextFieldParser(TextFieldParser parse try { - string[] row = parser.ReadFields() ?? new string[0]; + string[] row = parser.ReadFields() ?? Array.Empty(); if (!ignoreEmptyLines || !row.All(column => column == "")) { diff --git a/BytecodeApi.IniParser/IniFile.cs b/BytecodeApi.IniParser/IniFile.cs index c760498..7c69445 100644 --- a/BytecodeApi.IniParser/IniFile.cs +++ b/BytecodeApi.IniParser/IniFile.cs @@ -29,7 +29,7 @@ public IniFile() { GlobalProperties = new(null); Sections = new(); - ErrorLines = new IniErrorLine[0].ToReadOnlyCollection(); + ErrorLines = Array.Empty().ToReadOnlyCollection(); } /// /// Creates an object from the specified file. diff --git a/BytecodeApi.PEParser/ImageOptionalHeader.cs b/BytecodeApi.PEParser/ImageOptionalHeader.cs index df2bc4b..5dea225 100644 --- a/BytecodeApi.PEParser/ImageOptionalHeader.cs +++ b/BytecodeApi.PEParser/ImageOptionalHeader.cs @@ -104,7 +104,7 @@ public abstract class ImageOptionalHeader internal ImageOptionalHeader() { - DataDirectories = new ImageDataDirectory[0]; + DataDirectories = Array.Empty(); } /// diff --git a/BytecodeApi.Penetration/ExecutableInjection.cs b/BytecodeApi.Penetration/ExecutableInjection.cs index 2cee6cd..b7e55e6 100644 --- a/BytecodeApi.Penetration/ExecutableInjection.cs +++ b/BytecodeApi.Penetration/ExecutableInjection.cs @@ -199,7 +199,7 @@ public static void ExecuteDotNetAssembly(byte[] executable, string?[]? args, boo } else if (parameters.Length == 1 && parameters.First().ParameterType == typeof(string[])) { - invoke = () => method.Invoke(null, new[] { args ?? new string[0] }); + invoke = () => method.Invoke(null, new[] { args ?? Array.Empty() }); } else { diff --git a/BytecodeApi.Win32/SystemInfo/TcpView.cs b/BytecodeApi.Win32/SystemInfo/TcpView.cs index e39603a..fac493e 100644 --- a/BytecodeApi.Win32/SystemInfo/TcpView.cs +++ b/BytecodeApi.Win32/SystemInfo/TcpView.cs @@ -119,7 +119,7 @@ TRow[] GetConnections(bool udp, int version) } else { - return new TRow[0]; + return Array.Empty(); } uint GetTable(nint table) diff --git a/BytecodeApi.Wmi/WmiClass.cs b/BytecodeApi.Wmi/WmiClass.cs index 45bd8d8..7129205 100644 --- a/BytecodeApi.Wmi/WmiClass.cs +++ b/BytecodeApi.Wmi/WmiClass.cs @@ -90,7 +90,7 @@ public object InvokeMethod(string methodName, params object[]? args) Check.ArgumentNull(methodName); using ManagementClass managementClass = new(Namespace.FullPath, Name, new ObjectGetOptions()); - return managementClass.InvokeMethod(methodName, args ?? new object[0]); + return managementClass.InvokeMethod(methodName, args ?? Array.Empty()); } /// /// Invokes a method on this . diff --git a/BytecodeApi/ConvertEx.cs b/BytecodeApi/ConvertEx.cs index ceea611..f100f40 100644 --- a/BytecodeApi/ConvertEx.cs +++ b/BytecodeApi/ConvertEx.cs @@ -141,7 +141,7 @@ public static string ToBase32String(byte[] data, bool crockford) } int padding = 8 - result.Length % 8; - if (padding > 0 && padding < 8) + if (padding is > 0 and < 8) { result.Append('=', padding); } @@ -176,7 +176,7 @@ public static byte[] FromBase32String(string str, bool crockford) if (str.Length == 0) { - return new byte[0]; + return Array.Empty(); } else { @@ -307,7 +307,7 @@ public static int FromExcelColumnString(string str, bool oneBased) /// public static string ToRomanNumber(int value) { - Check.ArgumentOutOfRange(value >= 1 && value <= 3999, nameof(value), "Number must be in range of 1...3999."); + Check.ArgumentOutOfRange(value is >= 1 and <= 3999, nameof(value), "Number must be in range of 1...3999."); // 1 I // 5 V diff --git a/BytecodeApi/Data/Blob.cs b/BytecodeApi/Data/Blob.cs index b87940f..5eff030 100644 --- a/BytecodeApi/Data/Blob.cs +++ b/BytecodeApi/Data/Blob.cs @@ -28,7 +28,7 @@ public class Blob public Blob() { Name = ""; - Content = new byte[0]; + Content = Array.Empty(); } /// /// Initializes a new instance of the class with the specified name. diff --git a/BytecodeApi/Extensions/ImageExtensions.cs b/BytecodeApi/Extensions/ImageExtensions.cs index d0fc887..24d8ffa 100644 --- a/BytecodeApi/Extensions/ImageExtensions.cs +++ b/BytecodeApi/Extensions/ImageExtensions.cs @@ -65,7 +65,7 @@ public static void SaveJpeg(this Image image, string path, int quality) { Check.ArgumentNull(image); Check.ArgumentNull(path); - Check.ArgumentOutOfRange(quality >= 0 && quality <= 100, nameof(quality), "Jpeg quality must be in range of 0...100."); + Check.ArgumentOutOfRange(quality is >= 0 and <= 100, nameof(quality), "Jpeg quality must be in range of 0...100."); using FileStream file = File.Create(path); image.SaveJpeg(file, quality); @@ -89,7 +89,7 @@ public static void SaveJpeg(this Image image, Stream stream, int quality) { Check.ArgumentNull(image); Check.ArgumentNull(stream); - Check.ArgumentOutOfRange(quality >= 0 && quality <= 100, nameof(quality), "Jpeg quality must be in range of 0...100."); + Check.ArgumentOutOfRange(quality is >= 0 and <= 100, nameof(quality), "Jpeg quality must be in range of 0...100."); using EncoderParameters encoderParameters = new(1); using EncoderParameter encoderParameter = new(Encoder.Quality, quality); @@ -119,7 +119,7 @@ public static byte[] ToArrayJpeg(this Image image) public static byte[] ToArrayJpeg(this Image image, int quality) { Check.ArgumentNull(image); - Check.ArgumentOutOfRange(quality >= 0 && quality <= 100, nameof(quality), "Jpeg quality must be in range of 0...100."); + Check.ArgumentOutOfRange(quality is >= 0 and <= 100, nameof(quality), "Jpeg quality must be in range of 0...100."); using MemoryStream memoryStream = new(); image.SaveJpeg(memoryStream, quality); diff --git a/BytecodeApi/IO/CommandLine.cs b/BytecodeApi/IO/CommandLine.cs index 5b5f5f8..90bf0f8 100644 --- a/BytecodeApi/IO/CommandLine.cs +++ b/BytecodeApi/IO/CommandLine.cs @@ -24,7 +24,7 @@ public static string[] GetArguments(string commandLine) if (commandLine.IsNullOrWhiteSpace()) { - return new string[0]; + return Array.Empty(); } else { diff --git a/BytecodeApi/IO/Network.cs b/BytecodeApi/IO/Network.cs index 57710b3..b4a7215 100644 --- a/BytecodeApi/IO/Network.cs +++ b/BytecodeApi/IO/Network.cs @@ -65,7 +65,7 @@ public static void WakeOnLan(PhysicalAddress physicalAddress, byte[]? password) byte[] packet = Enumerable .Repeat(0xff, 6) .Concat(Enumerable.Repeat(physicalAddress.GetAddressBytes(), 16).SelectMany()) - .Concat(password ?? new byte[0]) + .Concat(password ?? Array.Empty()) .ToArray(); using UdpClient client = new(); diff --git a/BytecodeApi/IO/PathEx.cs b/BytecodeApi/IO/PathEx.cs index 10f631b..a6930b9 100644 --- a/BytecodeApi/IO/PathEx.cs +++ b/BytecodeApi/IO/PathEx.cs @@ -30,7 +30,7 @@ public static string GetOriginalPath(string path) if (result > 0 && result < stringBuilder.Capacity) { path = stringBuilder.ToString(0, result); - if (path[0] >= 'a' && path[0] <= 'z' && path[1] == ':') + if (path[0] is >= 'a' and <= 'z' && path[1] == ':') { path = path[0].ToUpper() + path[1..]; } @@ -52,7 +52,7 @@ public static string GetUncPath(string path) Check.ArgumentNull(path); Check.FileOrDirectoryNotFound(path); - if (path.Length > 2 && path[1] == ':' && path[0].ToUpper() >= 'A' && path[0].ToUpper() <= 'Z') + if (path.Length > 2 && path[1] == ':' && path[0] is >= 'a' and <= 'z' or >= 'A' and <= 'Z') { StringBuilder result = new(512); int length = result.Capacity; diff --git a/BytecodeApi/Text/Wording.cs b/BytecodeApi/Text/Wording.cs index 3736a25..6e302b6 100644 --- a/BytecodeApi/Text/Wording.cs +++ b/BytecodeApi/Text/Wording.cs @@ -285,11 +285,11 @@ public static string Rot13(string str) for (int i = 0; i < str.Length; i++) { char c = newString[i]; - if (c >= 'a' && c <= 'z') + if (c is >= 'a' and <= 'z') { newString[i] = (char)(c + 13 > 'z' ? c - 13 : c + 13); } - else if (c >= 'A' && c <= 'Z') + else if (c is >= 'A' and <= 'Z') { newString[i] = (char)(c + 13 > 'Z' ? c - 13 : c + 13); } diff --git a/BytecodeApi/Validate.cs b/BytecodeApi/Validate.cs index fb1ad1c..6413ada 100644 --- a/BytecodeApi/Validate.cs +++ b/BytecodeApi/Validate.cs @@ -102,7 +102,7 @@ public static bool Base64String([NotNullWhen(true)] string? str) foreach (char c in str.Where(c => !c.IsWhiteSpace())) { - if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '+' || c == '/') + if (c is >= 'a' and <= 'z' or >= 'A' and <= 'Z' or >= '0' and <= '9' or '+' or '/') { if (padding) { @@ -125,7 +125,7 @@ public static bool Base64String([NotNullWhen(true)] string? str) return contentLength == 0 && paddingLength == 0 || contentLength == 2 && paddingLength == 2 || - contentLength == 3 && (paddingLength == 1 || paddingLength == 2); + contentLength == 3 && paddingLength is 1 or 2; } } /// @@ -159,7 +159,7 @@ public static bool Path([NotNullWhen(true)] string? str) return root?.Length >= 3 && - (root[0] >= 'a' && root[0] <= 'z' || root[0] >= 'A' && root[0] <= 'Z') && + root[0] is >= 'a' and <= 'z' or >= 'A' and <= 'Z' && root[1] == ':' && root[2] is '\\' or '/' && root.IndexOfAny(System.IO.Path.GetInvalidPathChars()) == -1 &&