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
43 changes: 43 additions & 0 deletions PolyShim.Tests/Net70/RegexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Text.RegularExpressions;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net70;

public class RegexTests
{
[Fact]
public void Count_Test()
{
// Arrange
const string input = "There are 3 numbers: 42 and 100";

// Act & assert
new Regex(@"\d+")
.Count(input)
.Should()
.Be(3);
Regex.Count(input, @"\d+").Should().Be(3);
}

[Fact]
public void Count_Options_Test()
{
// Arrange
const string input = "There are 3 numbers: 42 and 100";

// Act & assert
Regex.Count(input, @"\d+", RegexOptions.None).Should().Be(3);
}

[Fact]
public void Count_OptionsAndTimeout_Test()
{
// Arrange
const string input = "There are 3 numbers: 42 and 100";

// Act & assert
Regex.Count(input, @"\d+", RegexOptions.None, TimeSpan.FromSeconds(1)).Should().Be(3);
}
}
51 changes: 51 additions & 0 deletions PolyShim/Net70/Regex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#if (NETCOREAPP && !NET7_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Text.RegularExpressions;

internal static partial class PolyfillExtensions
{
extension(Regex regex)
{
// https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string)
public int Count(string input)
{
var count = 0;
var match = regex.Match(input);
while (match.Success)
{
count++;
match = match.NextMatch();
}

return count;
}
}

extension(Regex)
{
// https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string-system-string)
public static int Count(string input, string pattern) =>
Regex.Matches(input, pattern).Count;

// https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string-system-string-system-text-regularexpressions-regexoptions)
public static int Count(string input, string pattern, RegexOptions options) =>
Regex.Matches(input, pattern, options).Count;

#if !NETFRAMEWORK || NET45_OR_GREATER
// https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string-system-string-system-text-regularexpressions-regexoptions-system-timespan)
public static int Count(
string input,
string pattern,
RegexOptions options,
TimeSpan matchTimeout
) => Regex.Matches(input, pattern, options, matchTimeout).Count;
#endif
}
}
#endif
9 changes: 7 additions & 2 deletions PolyShim/Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 233
- **Total:** 237
- **Types:** 62
- **Members:** 171
- **Members:** 175

___

Expand Down Expand Up @@ -200,6 +200,11 @@ ___
- [`void NextBytes(Span<byte>)`](https://learn.microsoft.com/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))) <sup><sub>.NET Core 2.1</sub></sup>
- `Range`
- [**[struct]**](https://learn.microsoft.com/dotnet/api/system.range) <sup><sub>.NET Core 3.0</sub></sup>
- `Regex`
- [`int Count(string, string, RegexOptions, TimeSpan)`](https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string-system-string-system-text-regularexpressions-regexoptions-system-timespan)) <sup><sub>.NET 7.0</sub></sup>
- [`int Count(string, string, RegexOptions)`](https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string-system-string-system-text-regularexpressions-regexoptions)) <sup><sub>.NET 7.0</sub></sup>
- [`int Count(string, string)`](https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string-system-string)) <sup><sub>.NET 7.0</sub></sup>
- [`int Count(string)`](https://learn.microsoft.com/dotnet/api/system.text.regularexpressions.regex.count#system-text-regularexpressions-regex-count(system-string)) <sup><sub>.NET 7.0</sub></sup>
- `RequiredMemberAttribute`
- [**[class]**](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.requiredmemberattribute) <sup><sub>.NET 7.0</sub></sup>
- `RequiresAssemblyFilesAttribute`
Expand Down
Loading