-
Notifications
You must be signed in to change notification settings - Fork 17
Add netstandard2.0 target for .NET Framework 4.6.2+ support #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #if NETSTANDARD2_0 | ||
| // ReSharper disable once CheckNamespace | ||
| namespace System.Collections.Generic; | ||
|
|
||
| internal static class DictionaryPolyfills | ||
| { | ||
| public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) | ||
| where TKey : notnull | ||
| { | ||
| if (dictionary.ContainsKey(key)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| dictionary.Add(key, value); | ||
| return true; | ||
| } | ||
|
|
||
| public static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) | ||
| where TKey : notnull | ||
| { | ||
| return dictionary.TryGetValue(key, out var value) ? value : default; | ||
| } | ||
|
|
||
| public static TValue GetValueOrDefault<TKey, TValue>( | ||
| this IReadOnlyDictionary<TKey, TValue> dictionary, | ||
| TKey key, | ||
| TValue defaultValue) | ||
| where TKey : notnull | ||
| { | ||
| return dictionary.TryGetValue(key, out var value) ? value : defaultValue; | ||
| } | ||
|
|
||
| public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value) | ||
| { | ||
| key = pair.Key; | ||
| value = pair.Value; | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #if NETSTANDARD2_0 | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace System; | ||
|
|
||
| /// <summary> | ||
| /// Polyfill for System.Index to enable C# 8 index syntax (e.g., ^1) on netstandard2.0. | ||
| /// </summary> | ||
| internal readonly struct Index : IEquatable<Index> | ||
| { | ||
| readonly int _value; | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public Index(int value, bool fromEnd = false) | ||
| { | ||
| if (value < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); | ||
| } | ||
|
|
||
| _value = fromEnd ? ~value : value; | ||
| } | ||
|
|
||
| Index(int value) | ||
| { | ||
| _value = value; | ||
| } | ||
|
|
||
| public static Index Start => new(0); | ||
| public static Index End => new(~0); | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static Index FromStart(int value) | ||
| { | ||
| if (value < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); | ||
| } | ||
|
|
||
| return new Index(value); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static Index FromEnd(int value) | ||
| { | ||
| if (value < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); | ||
| } | ||
|
|
||
| return new Index(~value); | ||
| } | ||
|
|
||
| public int Value => _value < 0 ? ~_value : _value; | ||
| public bool IsFromEnd => _value < 0; | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public int GetOffset(int length) | ||
| { | ||
| var offset = _value; | ||
| if (IsFromEnd) | ||
| { | ||
| offset += length + 1; | ||
| } | ||
|
|
||
| return offset; | ||
| } | ||
|
|
||
| public override bool Equals(object? value) => value is Index index && _value == index._value; | ||
| public bool Equals(Index other) => _value == other._value; | ||
| public override int GetHashCode() => _value; | ||
|
|
||
| public static implicit operator Index(int value) => FromStart(value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Polyfill for System.Range to enable C# 8 range syntax (e.g., 1..^1) on netstandard2.0. | ||
| /// </summary> | ||
| internal readonly struct Range : IEquatable<Range> | ||
| { | ||
| public Index Start { get; } | ||
| public Index End { get; } | ||
|
|
||
| public Range(Index start, Index end) | ||
| { | ||
| Start = start; | ||
| End = end; | ||
| } | ||
|
|
||
| public static Range StartAt(Index start) => new(start, Index.End); | ||
| public static Range EndAt(Index end) => new(Index.Start, end); | ||
| public static Range All => new(Index.Start, Index.End); | ||
|
|
||
| public override bool Equals(object? value) => value is Range r && r.Start.Equals(Start) && r.End.Equals(End); | ||
| public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); | ||
| public override int GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode(); | ||
|
|
||
| public (int Offset, int Length) GetOffsetAndLength(int length) | ||
| { | ||
| var start = Start.GetOffset(length); | ||
| var end = End.GetOffset(length); | ||
| if ((uint)end > (uint)length || (uint)start > (uint)end) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(length)); | ||
| } | ||
|
|
||
| return (start, end - start); | ||
| } | ||
| } | ||
|
|
||
| internal static class RuntimeCompatibilityExtensions | ||
| { | ||
| public static string Substring(this string s, Range range) | ||
| { | ||
| var (offset, length) = range.GetOffsetAndLength(s.Length); | ||
| return s.Substring(offset, length); | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #if NETSTANDARD2_0 | ||
| // ReSharper disable once CheckNamespace | ||
| namespace System; | ||
|
|
||
| internal static class StringPolyfills | ||
| { | ||
| public static bool Contains(this string source, string value, StringComparison comparisonType) | ||
| { | ||
| return source.IndexOf(value, comparisonType) >= 0; | ||
| } | ||
|
|
||
| public static string Replace(this string source, string oldValue, string? newValue, StringComparison comparisonType) | ||
| { | ||
| if (string.IsNullOrEmpty(oldValue)) | ||
| { | ||
| throw new ArgumentException("String cannot be of zero length.", nameof(oldValue)); | ||
| } | ||
|
|
||
| if (comparisonType == StringComparison.Ordinal) | ||
| { | ||
| return source.Replace(oldValue, newValue ?? string.Empty); | ||
| } | ||
|
|
||
| var result = new Text.StringBuilder(); | ||
| var searchIndex = 0; | ||
|
|
||
| while (searchIndex < source.Length) | ||
| { | ||
| var matchIndex = source.IndexOf(oldValue, searchIndex, comparisonType); | ||
| if (matchIndex < 0) | ||
| { | ||
| result.Append(source, searchIndex, source.Length - searchIndex); | ||
| break; | ||
| } | ||
|
|
||
| result.Append(source, searchIndex, matchIndex - searchIndex); | ||
| result.Append(newValue); | ||
| searchIndex = matchIndex + oldValue.Length; | ||
| } | ||
|
haacked marked this conversation as resolved.
|
||
|
|
||
| return result.ToString(); | ||
| } | ||
|
|
||
| public static int GetHashCode(this string source, StringComparison comparisonType) | ||
| { | ||
| return GetStringComparer(comparisonType).GetHashCode(source); | ||
| } | ||
|
|
||
| static StringComparer GetStringComparer(StringComparison comparisonType) | ||
| { | ||
| return comparisonType switch | ||
| { | ||
| StringComparison.CurrentCulture => StringComparer.CurrentCulture, | ||
| StringComparison.CurrentCultureIgnoreCase => StringComparer.CurrentCultureIgnoreCase, | ||
| StringComparison.InvariantCulture => StringComparer.InvariantCulture, | ||
| StringComparison.InvariantCultureIgnoreCase => StringComparer.InvariantCultureIgnoreCase, | ||
| StringComparison.Ordinal => StringComparer.Ordinal, | ||
| StringComparison.OrdinalIgnoreCase => StringComparer.OrdinalIgnoreCase, | ||
| _ => throw new ArgumentException($"Unknown StringComparison value: {comparisonType}", nameof(comparisonType)) | ||
| }; | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #if NETSTANDARD2_0 | ||
| // ReSharper disable once CheckNamespace | ||
| namespace System; | ||
|
|
||
| internal static class StringPolyfills | ||
| { | ||
| public static int IndexOf(this string source, char value, StringComparison comparisonType) | ||
| { | ||
| return source.IndexOf(value.ToString(), comparisonType); | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.