Skip to content

Commit 058cbc6

Browse files
Add conditional compilation for .NET version compatibility
Introduce conditional compilation directives to handle differences between .NET 6.0 (or greater) and older versions of .NET. In `RegexExtensions.cs`: - Conditionally compile `GetOriginalTextDelegate` method and `_textDelegate` field initialization. - Update `AsSpan` method to use `ValueSpan` property directly for .NET 6.0 or greater, otherwise use `_textDelegate`. - Add comments and attributes for obsolescence and inline documentation. In `SplitTests.cs`: - Conditionally compile `Split` method test case with `StringComparison.OrdinalIgnoreCase` to run only for .NET 6.0 or greater, ensuring compatibility and correct behavior across different .NET versions.
1 parent 8d4aa86 commit 058cbc6

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

Source/RegexExtensions.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/// </summary>
66
public static class RegexExtensions
77
{
8+
#if NET6_0_OR_GREATER
9+
#else
810
private static Func<Capture, string> GetOriginalTextDelegate()
911
{
1012
PropertyInfo? textProp = typeof(Capture).GetProperty("Text", BindingFlags.Instance | BindingFlags.NonPublic);
@@ -13,11 +15,7 @@ private static Func<Capture, string> GetOriginalTextDelegate()
1315
MethodInfo method = textProp.GetGetMethod(nonPublic: true)
1416
?? throw new InvalidOperationException("Could not find the Text property getter.");
1517

16-
#if NET6_0_OR_GREATER
17-
return method.CreateDelegate<Func<Capture, string>>();
18-
#else
1918
return (Func<Capture, string>)method.CreateDelegate(typeof(Func<Capture, string>));
20-
#endif
2119
}
2220

2321
// Some older versions of .NET use this instead.
@@ -27,17 +25,31 @@ private static Func<Capture, string> GetOriginalTextDelegate()
2725
: throw new NotSupportedException("Capture: could not find the Text property or _text field.");
2826
}
2927

30-
private static readonly Func<Capture, string> _textDelegate = GetOriginalTextDelegate();
28+
private static readonly Func<Capture, string> _textDelegate
29+
= GetOriginalTextDelegate();
30+
#endif
3131

32+
#if NET6_0_OR_GREATER
33+
/// <summary>
34+
/// Returns a ReadOnlySpan of the capture without creating a new string.
35+
/// </summary>
36+
/// <param name="capture">The capture to get the span from.</param>
37+
/// <remarks>Obsolete: use .ValueSpan instead.</remarks>
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
#else
3240
/// <summary>
3341
/// Returns a ReadOnlySpan of the capture without creating a new string.
3442
/// </summary>
35-
/// <remarks>This is a stop-gap until .NET 6 releases the .ValueSpan property.</remarks>
3643
/// <param name="capture">The capture to get the span from.</param>
44+
#endif
3745
public static ReadOnlySpan<char> AsSpan(this Capture capture)
3846
=> capture is null
3947
? throw new ArgumentNullException(nameof(capture))
48+
#if NET6_0_OR_GREATER
49+
: capture.ValueSpan;
50+
#else
4051
: _textDelegate(capture).AsSpan(capture.Index, capture.Length);
52+
#endif
4153

4254
/// <summary>
4355
/// Gets a group by name.

Tests/SplitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public static void Split(string sequence, StringSplitOptions options = StringSpl
9999
var span = sequence.AsSpan();
100100
Assert.Equal(segments, span.Split(',', options));
101101
Assert.Equal(segments, span.Split(",", options));
102-
//#if NET6_0_OR_GREATER
103-
// Assert.Equal(sequence.Split("I,", options), span.Split("i,", options, StringComparison.OrdinalIgnoreCase));
104-
//#endif
102+
#if NET6_0_OR_GREATER
103+
Assert.Equal(sequence.Split("I,", options), span.Split("i,", options, StringComparison.OrdinalIgnoreCase));
104+
#endif
105105

106106
#pragma warning disable CS0618 // Type or member is obsolete
107107
// Use obsolete values to ensure they still work.

0 commit comments

Comments
 (0)