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
4 changes: 3 additions & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SpanExtensions

[![NuGet Badge](https://buildstats.info/nuget/SpanExtensions.Net)](https://www.nuget.org/packages/SpanExtensions.Net)
[![NuGet Version](https://img.shields.io/nuget/v/SpanExtensions.Net)](https://www.nuget.org/packages/SpanExtensions.Net)
[![NuGet Downloads](https://img.shields.io/nuget/dt/SpanExtensions.Net)](https://www.nuget.org/packages/SpanExtensions.Net)
[![GitHub License](https://img.shields.io/github/license/draconware-dev/SpanExtensions.Net)](https://github.com/draconware-dev/SpanExtensions.Net/blob/main/LICENSE)

## About
**`ReadonlySpan<T>`** and **`Span<T>`** are great Types in _C#_, but unfortunately working with them can sometimes be sort of a hassle and some use cases seem straight up impossible, even though they are not.
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ name: Format
on:
push:
branches: [ "dev" ]
paths:
- '**.cs'

permissions:
contents: write
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ name: .NET
on:
push:
branches: [ "main" ]
paths:
- '**.cs'
- '**.csproj'
- '**.sln'

permissions:
contents: write
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
paths:
- '**.cs'
- '**.csproj'
- '**.sln'

jobs:
test:
Expand Down
10 changes: 10 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres not (yet) to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.4.2] - 2024-10-29

### Added

- `(Readonly-)Span<T>.Count(...)` overloads to all versions before .Net 8 matching these introduced in .Net 8.

### Changed

- blocked compilation on .Net 9 due to known incompatibilities, which are to be resolved in version 1.5.

## [1.4.1] - 2024-9-9

### Fixed
Expand Down
43 changes: 43 additions & 0 deletions src/Extensions/ReadOnlySpan/Linq/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,48 @@ public static int Count<T>(this ReadOnlySpan<T> source, Predicate<T> predicate)

return count;
}

#if !NET8_0_OR_GREATER
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
/// <typeparam name="T">The element type of the span.</typeparam>
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
/// <param name="value">The value for which to search.</param>
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
public static int Count<T>(this ReadOnlySpan<T> source, T value) where T : IEquatable<T>
{
int count = 0;

foreach(var item in source)
{
if(item.Equals(value))
{
count++;
}
}

return count;
}

/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
/// <typeparam name="T">The element type of the span.</typeparam>
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
/// <param name="value">The value for which to search.</param>
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
public static int Count<T>(this ReadOnlySpan<T> source, ReadOnlySpan<T> value) where T : IEquatable<T>
{
int count = 0;
int current = 0;

while((current = source.IndexOf(value)) != -1)
{
source = source.Slice(current + value.Length);
count++;
}

return count;
}
#endif
}
}
24 changes: 24 additions & 0 deletions src/Extensions/Span/Linq/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,29 @@ public static int Count<T>(this Span<T> source, Predicate<T> predicate)
{
return ReadOnlySpanExtensions.Count(source, predicate);
}

#if !NET8_0_OR_GREATER
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
/// <typeparam name="T">The element type of the span.</typeparam>
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
/// <param name="value">The value for which to search.</param>
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
public static int Count<T>(this Span<T> source, T value) where T : IEquatable<T>
{
return ReadOnlySpanExtensions.Count(source, value);
}

/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
/// <typeparam name="T">The element type of the span.</typeparam>
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
/// <param name="value">The value for which to search.</param>
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
public static int Count<T>(this Span<T> source, ReadOnlySpan<T> value) where T : IEquatable<T>
{
return ReadOnlySpanExtensions.Count(source, value);
}
#endif
}
}
6 changes: 5 additions & 1 deletion src/SpanExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
<PackageTags>Span;Performance;Extension;String</PackageTags>
<PackageReleaseNotes>https://github.com/draconware-dev/SpanExtensions.Net/blob/main/Changelog.md</PackageReleaseNotes>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<PackageId>SpanExtensions.Net</PackageId>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>

<Target Name="BlokcCompilationDotNet9Onwards" BeforeTargets="Compile">
<Error Condition="'$(TargetFramework)' == 'net9.0'" Text="Error: This project does not support versions past .NET 8. Please upgrade to the latest version of SpanExtensions.Net. Due to the introduction of the new variable-length Split methods in .Net 9, the rich set of Split extension methods provided by this package has been removed in favour of the new built-in versions. If you wish to continue using our rich set of methds over the new Split versions, they are still available under 'ReadOnlySpanExtensions.Split(...)' and 'SpanExtensions.Split(...)' respectively." />
</Target>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>portable</DebugType>
Expand Down
Loading