Skip to content

Commit

Permalink
Minor optimizations
Browse files Browse the repository at this point in the history
* Avoids some temporary array and string allocations
  • Loading branch information
LTRData committed Apr 14, 2024
1 parent 56bf0a8 commit 22580c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
26 changes: 12 additions & 14 deletions Library/DiscUtils.Core/Internal/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,40 +344,38 @@ 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))
{
return false;
}
}

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;
Expand Down
15 changes: 9 additions & 6 deletions Library/DiscUtils.OpticalDiscSharing/OpticalDiscService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string, object>();
req["askDevice"] = string.Empty;
req["computer"] = computerName;
req["user"] = userName;
var req = new Dictionary<string, object>
{
["askDevice"] = string.Empty,
["computer"] = computerName,
["user"] = userName
};

using (var outStream = wreq.GetRequestStream())
{
Expand Down Expand Up @@ -239,12 +242,12 @@ private Dictionary<string, string> 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();
}
}

Expand Down

0 comments on commit 22580c6

Please sign in to comment.