Skip to content

Commit

Permalink
Move to .NET 9
Browse files Browse the repository at this point in the history
Fix collision with new Split extension in .NET. Tweak BufferScope to better perform when calling EnsureCapacity.
  • Loading branch information
JeremyKuhne committed Nov 15, 2024
1 parent d17a035 commit 3c2549c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<!-- Input Directories -->
<PackagesDir Condition="'$(PackagesDir)'==''">$(RepoDir)packages\</PackagesDir>

<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
10 changes: 9 additions & 1 deletion src/thirtytwo/Support/BufferScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ public BufferScope(Span<T> initialBuffer, int minimumLength)
/// <param name="copy">True to copy the existing elements when new space is allocated.</param>
public unsafe void EnsureCapacity(int capacity, bool copy = false)
{
if (_span!.Length >= capacity)
if (_span.Length >= capacity)
{
return;
}

// Keep method separate for better inlining.
IncreaseCapacity(capacity, copy);
}

private void IncreaseCapacity(int capacity, bool copy)
{
Debug.Assert(capacity > _span.Length);

T[] newArray = ArrayPool<T>.Shared.Rent(capacity);
if (copy)
{
Expand Down
31 changes: 30 additions & 1 deletion src/thirtytwo/Support/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Span<char> SliceAtNull(this Span<char> span)
/// <summary>
/// Splits into strings on the given <paramref name="delimiter"/>.
/// </summary>
public static IEnumerable<string> Split(this ReadOnlySpan<char> span, char delimiter, bool includeEmptyStrings = false)
public static IEnumerable<string> SplitToEnumerable(this ReadOnlySpan<char> span, char delimiter, bool includeEmptyStrings = false)
{
List<string> strings = [];
SpanReader<char> reader = new(span);
Expand Down Expand Up @@ -61,4 +61,33 @@ public static string[] ToStringArray(this ReadOnlySpan<BSTR> bstrs)

return strings;
}

/*
Currently not possible: https://github.com/dotnet/runtime/issues/109874
public static T[] ToArray<T>(this MemoryExtensions.SpanSplitEnumerator<T> enumerator)
where T : IEquatable<T>
{
using BufferScope<Range> ranges = new(stackalloc Range[10]);
int index = -1;
while (enumerator.MoveNext())
{
ranges.EnsureCapacity(index++);
ranges[index] = enumerator.Current;
}
if (index == -1)
{
return [];
}
T[] result = new T[ranges.Length];
for (int i = 0; i < ranges.Length; i++)
{
// The enumerator does not expose the span
// result[i] = enumerator.Span.Slice(ranges[i])
}
}
*/
}
2 changes: 1 addition & 1 deletion src/thirtytwo/Win32/System/Registry/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private static unsafe object ReadValue(ReadOnlySpan<byte> buffer, REG_VALUE_TYPE
// Size includes the null
return MemoryMarshal.Cast<byte, char>(buffer)[..^1].ToString();
case REG_VALUE_TYPE.REG_MULTI_SZ:
return MemoryMarshal.Cast<byte, char>(buffer).Split('\0').ToArray();
return MemoryMarshal.Cast<byte, char>(buffer).SplitToEnumerable('\0').ToArray();
case REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN:
return BinaryPrimitives.ReadUInt32LittleEndian(buffer);
case REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN:
Expand Down

0 comments on commit 3c2549c

Please sign in to comment.