Skip to content

Commit 551c19d

Browse files
authored
Merge pull request #23 from draconware-dev/dev
Version 1.4.2
2 parents abc1fc9 + c95b634 commit 551c19d

File tree

8 files changed

+92
-2
lines changed

8 files changed

+92
-2
lines changed

.github/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SpanExtensions
22

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

57
## About
68
**`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.

.github/workflows/format.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ name: Format
44
on:
55
push:
66
branches: [ "dev" ]
7+
paths:
8+
- '**.cs'
79

810
permissions:
911
contents: write

.github/workflows/publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ name: .NET
44
on:
55
push:
66
branches: [ "main" ]
7+
paths:
8+
- '**.cs'
9+
- '**.csproj'
10+
- '**.sln'
711

812
permissions:
913
contents: write

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths:
1010
- '**.cs'
1111
- '**.csproj'
12+
- '**.sln'
1213

1314
jobs:
1415
test:

Changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres not (yet) to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9+
## [1.4.2] - 2024-10-29
10+
11+
### Added
12+
13+
- `(Readonly-)Span<T>.Count(...)` overloads to all versions before .Net 8 matching these introduced in .Net 8.
14+
15+
### Changed
16+
17+
- blocked compilation on .Net 9 due to known incompatibilities, which are to be resolved in version 1.5.
18+
919
## [1.4.1] - 2024-9-9
1020

1121
### Fixed

src/Extensions/ReadOnlySpan/Linq/Count.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,48 @@ public static int Count<T>(this ReadOnlySpan<T> source, Predicate<T> predicate)
4747

4848
return count;
4949
}
50+
51+
#if !NET8_0_OR_GREATER
52+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
53+
/// <typeparam name="T">The element type of the span.</typeparam>
54+
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
55+
/// <param name="value">The value for which to search.</param>
56+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
57+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
58+
public static int Count<T>(this ReadOnlySpan<T> source, T value) where T : IEquatable<T>
59+
{
60+
int count = 0;
61+
62+
foreach(var item in source)
63+
{
64+
if(item.Equals(value))
65+
{
66+
count++;
67+
}
68+
}
69+
70+
return count;
71+
}
72+
73+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
74+
/// <typeparam name="T">The element type of the span.</typeparam>
75+
/// <param name="source">A <see cref="ReadOnlySpan{T}"/> whose elements are to be counted.</param>
76+
/// <param name="value">The value for which to search.</param>
77+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
78+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
79+
public static int Count<T>(this ReadOnlySpan<T> source, ReadOnlySpan<T> value) where T : IEquatable<T>
80+
{
81+
int count = 0;
82+
int current = 0;
83+
84+
while((current = source.IndexOf(value)) != -1)
85+
{
86+
source = source.Slice(current + value.Length);
87+
count++;
88+
}
89+
90+
return count;
91+
}
92+
#endif
5093
}
5194
}

src/Extensions/Span/Linq/Count.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,29 @@ public static int Count<T>(this Span<T> source, Predicate<T> predicate)
2929
{
3030
return ReadOnlySpanExtensions.Count(source, predicate);
3131
}
32+
33+
#if !NET8_0_OR_GREATER
34+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
35+
/// <typeparam name="T">The element type of the span.</typeparam>
36+
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
37+
/// <param name="value">The value for which to search.</param>
38+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
39+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
40+
public static int Count<T>(this Span<T> source, T value) where T : IEquatable<T>
41+
{
42+
return ReadOnlySpanExtensions.Count(source, value);
43+
}
44+
45+
/// <summary>Counts the number of times the specified <paramref name="value"/> occurs in the <paramref name="source"/>.</summary>
46+
/// <typeparam name="T">The element type of the span.</typeparam>
47+
/// <param name="source">A <see cref="Span{T}"/> whose elements are to be counted.</param>
48+
/// <param name="value">The value for which to search.</param>
49+
/// <returns>The number of elements eqaul to <paramref name="value"/> in <paramref name="source"/>.</returns>
50+
/// <exception cref="OverflowException">The number of elements in <paramref name="source"/> is larger than <see cref="int.MaxValue"/>.</exception>
51+
public static int Count<T>(this Span<T> source, ReadOnlySpan<T> value) where T : IEquatable<T>
52+
{
53+
return ReadOnlySpanExtensions.Count(source, value);
54+
}
55+
#endif
3256
}
3357
}

src/SpanExtensions.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
<PackageTags>Span;Performance;Extension;String</PackageTags>
2323
<PackageReleaseNotes>https://github.com/draconware-dev/SpanExtensions.Net/blob/main/Changelog.md</PackageReleaseNotes>
2424
<PackageLicenseFile>LICENSE</PackageLicenseFile>
25-
<Version>1.4.1</Version>
25+
<Version>1.4.2</Version>
2626
<PackageId>SpanExtensions.Net</PackageId>
2727
<PackageReadmeFile>README.md</PackageReadmeFile>
2828
<PackageIcon>icon.png</PackageIcon>
2929
</PropertyGroup>
30+
31+
<Target Name="BlokcCompilationDotNet9Onwards" BeforeTargets="Compile">
32+
<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." />
33+
</Target>
3034

3135
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
3236
<DebugType>portable</DebugType>

0 commit comments

Comments
 (0)