Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Ignore thumbnails created by windows
# Ignore thumbnails created by windows
[Tt]humbs.db

#Ignore files build by Visual Studio
# Ignore files build by Visual Studio
*.obj
*.exe
*.pdb
Expand Down Expand Up @@ -33,5 +33,8 @@ obj/
*.ReSharper.user
[Tt]est[Rr]esult*

#Ignore Rider/Idea files
# Ignore Rider/Idea files
.idea/

# Ignore BenchmarkDotNet artifacts
BenchmarkDotNet.Artifacts
109 changes: 109 additions & 0 deletions Ramstack.Globbing.Tests/MemoryHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Ramstack.Globbing.Internal;

namespace Ramstack.Globbing;

[TestFixture]
public unsafe class MemoryHelperTests
{
[Test]
public void IndexOf_Char()
{
Span<char> buffer = stackalloc char[64];

while (buffer.Length != 0)
{
Fill(buffer);

var s = (char*)Unsafe.AsPointer(ref buffer[0]);
Assert.That(
MemoryHelper.IndexOf(s, s + buffer.Length, '*'),
Is.EqualTo(-1));

for (var i = 0; i < buffer.Length; i++)
{
Fill(buffer);
buffer[i] = '*';

Assert.That(
MemoryHelper.IndexOf(s, s + buffer.Length, '*'),
Is.EqualTo(i));
}

buffer = buffer[1..];
}
}

[Test]
public void IndexOfAny_CharChar()
{
Span<char> buffer = stackalloc char[64];

while (buffer.Length != 0)
{
Fill(buffer);
var s = (char*)Unsafe.AsPointer(ref buffer[0]);

Assert.That(
MemoryHelper.IndexOfAny(s, s + buffer.Length, '*', '?'),
Is.EqualTo(-1));

Assert.That(
MemoryHelper.IndexOfAny(s, s + buffer.Length, '?', '*'),
Is.EqualTo(-1));

foreach (var needle in "*?")
{
for (var i = 0; i < buffer.Length; i++)
{
Fill(buffer);
buffer[i] = needle;

Assert.That(
MemoryHelper.IndexOfAny(s, s + buffer.Length, '*', '?'),
Is.EqualTo(i));

Assert.That(
MemoryHelper.IndexOfAny(s, s + buffer.Length, '?', '*'),
Is.EqualTo(i));
}
}

if (buffer.Length > 2)
{
foreach (var needle in new[] { "*?", "?*" })
{
for (var i = 0; i < buffer.Length - 1; i++)
{
Fill(buffer);
buffer[i + 0] = needle[0];
buffer[i + 1] = needle[1];

var r = MemoryHelper.IndexOfAny(s, s + buffer.Length, '*', '?');
Assert.That(r, Is.EqualTo(i));

var p = MemoryHelper.IndexOfAny(s, s + buffer.Length, '?', '*');
Assert.That(p, Is.EqualTo(i));
}
}
}

buffer = buffer[1..];
}
}

private static void Fill(Span<char> s)
{
for (var i = 0; i < s.Length; i++)
{
while (true)
{
var v = Random.Shared.Next(32, 127);
if (v is '*' or '?' or '{' or '}' or '[' or '\\')
continue;

s[i] = (char)v;
break;
}
}
}
}
7 changes: 7 additions & 0 deletions Ramstack.Globbing.Tests/Ramstack.Globbing.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>preview</LangVersion>
<RootNamespace>Ramstack.Globbing</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Using Include="System.Runtime.CompilerServices" />
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Ramstack.Globbing\Internal\MemoryHelper.cs" Link="Internal\MemoryHelper.cs" />
<Compile Include="..\Ramstack.Globbing\Internal\PathHelper.cs" Link="Internal\PathHelper.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
Expand Down
5 changes: 4 additions & 1 deletion Ramstack.Globbing.slnx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<Solution>
<Folder Name="/Solution Items/">
<Folder Name="/.global/">
<File Path=".editorconfig" />
<File Path=".gitignore" />
<File Path="LICENSE" />
<File Path="README.md" />
</Folder>
<Folder Name="/benchmarks/">
<Project Path="benchmarks/Ramstack.Globbing.Benchmarks/Ramstack.Globbing.Benchmarks.csproj" />
</Folder>
<Project Path="Ramstack.Globbing.Tests/Ramstack.Globbing.Tests.csproj" />
<Project Path="Ramstack.Globbing/Ramstack.Globbing.csproj" />
</Solution>
105 changes: 105 additions & 0 deletions Ramstack.Globbing/Internal/MemoryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace Ramstack.Globbing.Internal;

internal static unsafe class MemoryHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOf(char* s, char* e, char ch)
{
var i = 0;

if (Sse2.IsSupported && s + Vector128<short>.Count <= e)
{
for (;;)
{
var result = Sse2.CompareEqual(
Vector128.Create((short)ch),
LoadVector(s));

var mask = Sse2.MoveMask(result.AsByte());
if (mask != 0)
{
var offset = BitOperations.TrailingZeroCount(mask) >>> 1;
return i + offset;
}

s += Vector128<short>.Count;
i += Vector128<short>.Count;

if (s + Vector128<short>.Count <= e)
continue;

if (s == e)
return -1;

//
// Tail handling via the same SIMD path (no scalar fallback)
//
var remaining = (int)((nint)e - (nint)s) >>> 1;
i = i + remaining - Vector128<short>.Count;
s = e - Vector128<short>.Count;
}
}

for (; s < e; s++, i++)
if (*s == ch)
return i;

return -1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfAny(char* s, char* e, char ch1, char ch2)
{
var i = 0;

if (Sse2.IsSupported && s + Vector128<short>.Count <= e)
{
for (;;)
{
var source = LoadVector(s);
var result = Sse2.Or(
Sse2.CompareEqual(source, Vector128.Create((short)ch1)),
Sse2.CompareEqual(source, Vector128.Create((short)ch2))
).AsByte();

var mask = Sse2.MoveMask(result);
if (mask != 0)
{
var offset = BitOperations.TrailingZeroCount(mask) >>> 1;
return i + offset;
}

s += Vector128<short>.Count;
i += Vector128<short>.Count;

if (s + Vector128<short>.Count <= e)
continue;

if (s == e)
return -1;

//
// Tail handling via the same SIMD path (no scalar fallback)
//
var remaining = (int)((nint)e - (nint)s) >>> 1;
i = i + remaining - Vector128<short>.Count;
s = e - Vector128<short>.Count;
}
}

for (; s < e; s++, i++)
if (*s == ch1 || *s == ch2)
return i;

return -1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector128<short> LoadVector(void* source) =>
Unsafe.ReadUnaligned<Vector128<short>>(source);
}
Loading
Loading