diff --git a/.github/README.md b/.github/README.md index d3811e8..014f0cb 100644 --- a/.github/README.md +++ b/.github/README.md @@ -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`** and **`Span`** 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. diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 5e2565b..931817e 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -4,6 +4,8 @@ name: Format on: push: branches: [ "dev" ] + paths: + - '**.cs' permissions: contents: write diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 392f778..ee62bc3 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,6 +4,10 @@ name: .NET on: push: branches: [ "main" ] + paths: + - '**.cs' + - '**.csproj' + - '**.sln' permissions: contents: write diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d5a1fbf..224a98b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,6 +9,7 @@ on: paths: - '**.cs' - '**.csproj' + - '**.sln' jobs: test: diff --git a/Changelog.md b/Changelog.md index 5d36bad..afccc62 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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.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 diff --git a/src/Extensions/ReadOnlySpan/Linq/Count.cs b/src/Extensions/ReadOnlySpan/Linq/Count.cs index 175110a..534d95d 100644 --- a/src/Extensions/ReadOnlySpan/Linq/Count.cs +++ b/src/Extensions/ReadOnlySpan/Linq/Count.cs @@ -47,5 +47,48 @@ public static int Count(this ReadOnlySpan source, Predicate predicate) return count; } + +#if !NET8_0_OR_GREATER + /// Counts the number of times the specified occurs in the . + /// The element type of the span. + /// A whose elements are to be counted. + /// The value for which to search. + /// The number of elements eqaul to in . + /// The number of elements in is larger than . + public static int Count(this ReadOnlySpan source, T value) where T : IEquatable + { + int count = 0; + + foreach(var item in source) + { + if(item.Equals(value)) + { + count++; + } + } + + return count; + } + + /// Counts the number of times the specified occurs in the . + /// The element type of the span. + /// A whose elements are to be counted. + /// The value for which to search. + /// The number of elements eqaul to in . + /// The number of elements in is larger than . + public static int Count(this ReadOnlySpan source, ReadOnlySpan value) where T : IEquatable + { + int count = 0; + int current = 0; + + while((current = source.IndexOf(value)) != -1) + { + source = source.Slice(current + value.Length); + count++; + } + + return count; + } +#endif } } \ No newline at end of file diff --git a/src/Extensions/Span/Linq/Count.cs b/src/Extensions/Span/Linq/Count.cs index 34cf62d..8909854 100644 --- a/src/Extensions/Span/Linq/Count.cs +++ b/src/Extensions/Span/Linq/Count.cs @@ -29,5 +29,29 @@ public static int Count(this Span source, Predicate predicate) { return ReadOnlySpanExtensions.Count(source, predicate); } + +#if !NET8_0_OR_GREATER + /// Counts the number of times the specified occurs in the . + /// The element type of the span. + /// A whose elements are to be counted. + /// The value for which to search. + /// The number of elements eqaul to in . + /// The number of elements in is larger than . + public static int Count(this Span source, T value) where T : IEquatable + { + return ReadOnlySpanExtensions.Count(source, value); + } + + /// Counts the number of times the specified occurs in the . + /// The element type of the span. + /// A whose elements are to be counted. + /// The value for which to search. + /// The number of elements eqaul to in . + /// The number of elements in is larger than . + public static int Count(this Span source, ReadOnlySpan value) where T : IEquatable + { + return ReadOnlySpanExtensions.Count(source, value); + } +#endif } } \ No newline at end of file diff --git a/src/SpanExtensions.csproj b/src/SpanExtensions.csproj index 323fd81..370531d 100644 --- a/src/SpanExtensions.csproj +++ b/src/SpanExtensions.csproj @@ -22,11 +22,15 @@ Span;Performance;Extension;String https://github.com/draconware-dev/SpanExtensions.Net/blob/main/Changelog.md LICENSE - 1.4.1 + 1.4.2 SpanExtensions.Net README.md icon.png + + + + portable