|
| 1 | +#if NETSTANDARD2_0 |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | + |
| 4 | +// ReSharper disable once CheckNamespace |
| 5 | +namespace System; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Polyfill for System.Index to enable C# 8 index syntax (e.g., ^1) on netstandard2.0. |
| 9 | +/// </summary> |
| 10 | +internal readonly struct Index : IEquatable<Index> |
| 11 | +{ |
| 12 | + readonly int _value; |
| 13 | + |
| 14 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 15 | + public Index(int value, bool fromEnd = false) |
| 16 | + { |
| 17 | + if (value < 0) |
| 18 | + { |
| 19 | + throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); |
| 20 | + } |
| 21 | + |
| 22 | + _value = fromEnd ? ~value : value; |
| 23 | + } |
| 24 | + |
| 25 | + Index(int value) |
| 26 | + { |
| 27 | + _value = value; |
| 28 | + } |
| 29 | + |
| 30 | + public static Index Start => new(0); |
| 31 | + public static Index End => new(~0); |
| 32 | + |
| 33 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 34 | + public static Index FromStart(int value) |
| 35 | + { |
| 36 | + if (value < 0) |
| 37 | + { |
| 38 | + throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); |
| 39 | + } |
| 40 | + |
| 41 | + return new Index(value); |
| 42 | + } |
| 43 | + |
| 44 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 45 | + public static Index FromEnd(int value) |
| 46 | + { |
| 47 | + if (value < 0) |
| 48 | + { |
| 49 | + throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); |
| 50 | + } |
| 51 | + |
| 52 | + return new Index(~value); |
| 53 | + } |
| 54 | + |
| 55 | + public int Value => _value < 0 ? ~_value : _value; |
| 56 | + public bool IsFromEnd => _value < 0; |
| 57 | + |
| 58 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 59 | + public int GetOffset(int length) |
| 60 | + { |
| 61 | + var offset = _value; |
| 62 | + if (IsFromEnd) |
| 63 | + { |
| 64 | + offset += length + 1; |
| 65 | + } |
| 66 | + |
| 67 | + return offset; |
| 68 | + } |
| 69 | + |
| 70 | + public override bool Equals(object? value) => value is Index index && _value == index._value; |
| 71 | + public bool Equals(Index other) => _value == other._value; |
| 72 | + public override int GetHashCode() => _value; |
| 73 | + |
| 74 | + public static implicit operator Index(int value) => FromStart(value); |
| 75 | +} |
| 76 | + |
| 77 | +/// <summary> |
| 78 | +/// Polyfill for System.Range to enable C# 8 range syntax (e.g., 1..^1) on netstandard2.0. |
| 79 | +/// </summary> |
| 80 | +internal readonly struct Range : IEquatable<Range> |
| 81 | +{ |
| 82 | + public Index Start { get; } |
| 83 | + public Index End { get; } |
| 84 | + |
| 85 | + public Range(Index start, Index end) |
| 86 | + { |
| 87 | + Start = start; |
| 88 | + End = end; |
| 89 | + } |
| 90 | + |
| 91 | + public static Range StartAt(Index start) => new(start, Index.End); |
| 92 | + public static Range EndAt(Index end) => new(Index.Start, end); |
| 93 | + public static Range All => new(Index.Start, Index.End); |
| 94 | + |
| 95 | + public override bool Equals(object? value) => value is Range r && r.Start.Equals(Start) && r.End.Equals(End); |
| 96 | + public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); |
| 97 | + public override int GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode(); |
| 98 | + |
| 99 | + public (int Offset, int Length) GetOffsetAndLength(int length) |
| 100 | + { |
| 101 | + var start = Start.GetOffset(length); |
| 102 | + var end = End.GetOffset(length); |
| 103 | + if ((uint)end > (uint)length || (uint)start > (uint)end) |
| 104 | + { |
| 105 | + throw new ArgumentOutOfRangeException(nameof(length)); |
| 106 | + } |
| 107 | + |
| 108 | + return (start, end - start); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +internal static class RuntimeCompatibilityExtensions |
| 113 | +{ |
| 114 | + public static string Substring(this string s, Range range) |
| 115 | + { |
| 116 | + var (offset, length) = range.GetOffsetAndLength(s.Length); |
| 117 | + return s.Substring(offset, length); |
| 118 | + } |
| 119 | +} |
| 120 | +#endif |
0 commit comments