Skip to content

Commit

Permalink
Refactor: Use Array.Empty<T>()
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fischer committed Oct 10, 2023
1 parent 25d207e commit bb04a90
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion BytecodeApi.CommandLineParser/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Option(string[] arguments, string[]? alternatives)
}

Arguments = arguments.ToReadOnlyCollection();
Alternatives = (alternatives ?? new string[0]).ToReadOnlyCollection();
Alternatives = (alternatives ?? Array.Empty<string>()).ToReadOnlyCollection();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi.CsvParser/CsvHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IEnumerable<CsvRow> EnumerateTextFieldParser(TextFieldParser parse

try
{
string[] row = parser.ReadFields() ?? new string[0];
string[] row = parser.ReadFields() ?? Array.Empty<string>();

if (!ignoreEmptyLines || !row.All(column => column == ""))
{
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi.IniParser/IniFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IniFile()
{
GlobalProperties = new(null);
Sections = new();
ErrorLines = new IniErrorLine[0].ToReadOnlyCollection();
ErrorLines = Array.Empty<IniErrorLine>().ToReadOnlyCollection();
}
/// <summary>
/// Creates an <see cref="IniFile" /> object from the specified file.
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi.PEParser/ImageOptionalHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public abstract class ImageOptionalHeader

internal ImageOptionalHeader()
{
DataDirectories = new ImageDataDirectory[0];
DataDirectories = Array.Empty<ImageDataDirectory>();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi.Penetration/ExecutableInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>() });
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi.Win32/SystemInfo/TcpView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ TRow[] GetConnections<TTable, TRow>(bool udp, int version)
}
else
{
return new TRow[0];
return Array.Empty<TRow>();
}

uint GetTable(nint table)
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi.Wmi/WmiClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>());
}
/// <summary>
/// Invokes a method on this <see cref="WmiClass" />.
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi/ConvertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public static byte[] FromBase32String(string str, bool crockford)

if (str.Length == 0)
{
return new byte[0];
return Array.Empty<byte>();
}
else
{
Expand Down Expand Up @@ -307,7 +307,7 @@ public static int FromExcelColumnString(string str, bool oneBased)
/// </returns>
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
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi/Data/Blob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Blob
public Blob()
{
Name = "";
Content = new byte[0];
Content = Array.Empty<byte>();
}
/// <summary>
/// Initializes a new instance of the <see cref="Blob" /> class with the specified name.
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi/Extensions/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi/IO/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static string[] GetArguments(string commandLine)

if (commandLine.IsNullOrWhiteSpace())
{
return new string[0];
return Array.Empty<string>();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion BytecodeApi/IO/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void WakeOnLan(PhysicalAddress physicalAddress, byte[]? password)
byte[] packet = Enumerable
.Repeat<byte>(0xff, 6)
.Concat(Enumerable.Repeat(physicalAddress.GetAddressBytes(), 16).SelectMany())
.Concat(password ?? new byte[0])
.Concat(password ?? Array.Empty<byte>())
.ToArray();

using UdpClient client = new();
Expand Down
4 changes: 2 additions & 2 deletions BytecodeApi/IO/PathEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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..];
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions BytecodeApi/Text/Wording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi/Validate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
}
/// <summary>
Expand Down Expand Up @@ -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 &&
Expand Down

0 comments on commit bb04a90

Please sign in to comment.