Skip to content

Commit f417969

Browse files
committed
Minor optimizations
1 parent ce33ab2 commit f417969

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

Library/DiscUtils.Core/Internal/Utilities.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public static bool RangesOverlap<T>(T xFirst, T xLast, T yFirst, T yLast) where
5252

5353
public static bool IsAllZeros(byte[] buffer, int offset, int count)
5454
{
55+
#if NET8_0_OR_GREATER
56+
return !buffer.AsSpan(offset, count).ContainsAnyExcept((byte)0);
57+
#else
5558
var end = offset + count;
5659
for (var i = offset; i < end; ++i)
5760
{
@@ -62,10 +65,14 @@ public static bool IsAllZeros(byte[] buffer, int offset, int count)
6265
}
6366

6467
return true;
68+
#endif
6569
}
6670

6771
public static bool IsAllZeros(ReadOnlySpan<byte> buffer)
6872
{
73+
#if NET8_0_OR_GREATER
74+
return !buffer.ContainsAnyExcept((byte)0);
75+
#else
6976
for (var i = 0; i < buffer.Length; ++i)
7077
{
7178
if (buffer[i] != 0)
@@ -75,6 +82,7 @@ public static bool IsAllZeros(ReadOnlySpan<byte> buffer)
7582
}
7683

7784
return true;
85+
#endif
7886
}
7987

8088
public static bool IsPowerOfTwo(uint val)
@@ -161,7 +169,7 @@ public static long BitSwap(long value)
161169
return (long)BitSwap((ulong)value);
162170
}
163171

164-
#endregion
172+
#endregion
165173

166174
#region Path Manipulation
167175

@@ -555,25 +563,25 @@ public static Func<string, bool> ConvertWildcardsToRegEx(string pattern, bool ig
555563
return null;
556564
}
557565

558-
if (pattern.AsSpan().IndexOfAny('*', '?') < 0)
566+
static Func<string, bool> filterFactory((string pattern, bool ignoreCase) key)
559567
{
560-
if (!pattern.Contains('.'))
561-
{
562-
pattern += '.';
563-
}
564-
565-
if (ignoreCase)
568+
if (key.pattern.AsSpan().IndexOfAny('*', '?') < 0)
566569
{
567-
return name => StringComparer.OrdinalIgnoreCase.Equals(name, pattern);
570+
if (!key.pattern.Contains('.'))
571+
{
572+
key.pattern += '.';
573+
}
574+
575+
if (key.ignoreCase)
576+
{
577+
return name => StringComparer.OrdinalIgnoreCase.Equals(name, key.pattern);
578+
}
579+
else
580+
{
581+
return key.pattern.Equals;
582+
}
568583
}
569-
else
570-
{
571-
return pattern.Equals;
572-
}
573-
}
574584

575-
static Func<string, bool> filterFactory((string pattern, bool ignoreCase) key)
576-
{
577585
var regexOptions = RegexOptions.CultureInvariant | RegexOptions.Compiled;
578586

579587
if (key.ignoreCase)
@@ -610,17 +618,10 @@ public static FileAttributes FileAttributesFromUnixFilePermissions(string name,
610618
attr |= FileAttributes.ReadOnly;
611619
}
612620

613-
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
614-
if (Path.GetFileName(name.AsSpan()).StartsWith(".", StringComparison.Ordinal))
621+
if (GetFileFromPath(name.AsSpan()).StartsWith(".".AsSpan(), StringComparison.Ordinal))
615622
{
616623
attr |= FileAttributes.Hidden;
617624
}
618-
#else
619-
if (Path.GetFileName(name).StartsWith(".", StringComparison.Ordinal))
620-
{
621-
attr |= FileAttributes.Hidden;
622-
}
623-
#endif
624625

625626
return attr;
626627
}

0 commit comments

Comments
 (0)