From 22580c6e1989fa5b647d2abb66032a4602bb44de Mon Sep 17 00:00:00 2001
From: LTRData
Date: Sun, 14 Apr 2024 23:04:48 +0200
Subject: [PATCH] Minor optimizations
* Avoids some temporary array and string allocations
---
Library/DiscUtils.Core/Internal/Utilities.cs | 26 +++++++++----------
.../OpticalDiscService.cs | 15 ++++++-----
2 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/Library/DiscUtils.Core/Internal/Utilities.cs b/Library/DiscUtils.Core/Internal/Utilities.cs
index 80e9219c5..c227a9f07 100644
--- a/Library/DiscUtils.Core/Internal/Utilities.cs
+++ b/Library/DiscUtils.Core/Internal/Utilities.cs
@@ -344,19 +344,25 @@ public static bool Is8Dot3(string name)
return false;
}
- var split = name.Split('.');
+ var i = name.LastIndexOf('.');
- if (split.Length is > 2 or < 1)
+ // Check for more than one dot
+ if (i >= 0 && name.LastIndexOf('.', i - 1) >= 0)
{
return false;
}
- if (split[0].Length > 8)
+ var namePart = i >= 0 ? name.AsSpan(0, i) : name.AsSpan();
+ var extPart = i >= 0 ? name.AsSpan(i + 1) : default;
+
+ if (namePart.Length is 0 or > 8
+ || extPart.Length > 3)
{
return false;
}
- foreach (var ch in split[0])
+ // Check for invalid chars
+ foreach (var ch in namePart)
{
if (!Is8Dot3Char(ch))
{
@@ -364,20 +370,12 @@ public static bool Is8Dot3(string name)
}
}
- if (split.Length > 1)
+ foreach (var ch in extPart)
{
- if (split[1].Length > 3)
+ if (!Is8Dot3Char(ch))
{
return false;
}
-
- foreach (var ch in split[1])
- {
- if (!Is8Dot3Char(ch))
- {
- return false;
- }
- }
}
return true;
diff --git a/Library/DiscUtils.OpticalDiscSharing/OpticalDiscService.cs b/Library/DiscUtils.OpticalDiscSharing/OpticalDiscService.cs
index 930b2d613..217434955 100644
--- a/Library/DiscUtils.OpticalDiscSharing/OpticalDiscService.cs
+++ b/Library/DiscUtils.OpticalDiscSharing/OpticalDiscService.cs
@@ -27,6 +27,7 @@
using System.Text;
using System.Threading;
using DiscUtils.Net.Dns;
+using LTRData.Extensions.Split;
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable SYSLIB0014 // Type or member is obsolete
@@ -180,10 +181,12 @@ private static string InitiateAsk(string userName, string computerName, UriBuild
var wreq = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
wreq.Method = "POST";
- var req = new Dictionary();
- req["askDevice"] = string.Empty;
- req["computer"] = computerName;
- req["user"] = userName;
+ var req = new Dictionary
+ {
+ ["askDevice"] = string.Empty,
+ ["computer"] = computerName,
+ ["user"] = userName
+ };
using (var outStream = wreq.GetRequestStream())
{
@@ -239,12 +242,12 @@ private Dictionary GetParams(string section)
if (_instance.Parameters.TryGetValue(section, out var data))
{
var asString = Encoding.ASCII.GetString(data);
- var nvPairs = asString.Split(',');
+ var nvPairs = asString.AsSpan().Split(',');
foreach (var nvPair in nvPairs)
{
var parts = nvPair.Split('=');
- result[parts[0]] = parts[1];
+ result[parts.First().ToString()] = parts.ElementAt(1).ToString();
}
}