|
| 1 | +#if NETFRAMEWORK |
| 2 | +// Polyfill types and extension methods for .NET Framework 4.6.2 compatibility. |
| 3 | +// These are automatically available on .NET 6+ and are excluded via #if. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +// ── IsExternalInit: required by C# 9 records and init-only properties ── |
| 11 | +namespace System.Runtime.CompilerServices |
| 12 | +{ |
| 13 | + internal static class IsExternalInit { } |
| 14 | +} |
| 15 | + |
| 16 | +// ── Index / Range: required by C# 8 range operators (e.g. str[1..], arr[^1]) ── |
| 17 | +namespace System |
| 18 | +{ |
| 19 | + internal readonly struct Index : IEquatable<Index> |
| 20 | + { |
| 21 | + private readonly int _value; |
| 22 | + |
| 23 | + public Index(int value, bool fromEnd = false) |
| 24 | + { |
| 25 | + _value = fromEnd ? ~value : value; |
| 26 | + } |
| 27 | + |
| 28 | + public int Value => _value < 0 ? ~_value : _value; |
| 29 | + public bool IsFromEnd => _value < 0; |
| 30 | + |
| 31 | + public static Index Start => new Index(0); |
| 32 | + public static Index End => new Index(~0); |
| 33 | + |
| 34 | + public int GetOffset(int length) |
| 35 | + { |
| 36 | + var offset = _value; |
| 37 | + if (IsFromEnd) offset += length; |
| 38 | + return offset; |
| 39 | + } |
| 40 | + |
| 41 | + public static implicit operator Index(int value) => new Index(value); |
| 42 | + public bool Equals(Index other) => _value == other._value; |
| 43 | + public override bool Equals(object? obj) => obj is Index other && Equals(other); |
| 44 | + public override int GetHashCode() => _value; |
| 45 | + public override string ToString() => IsFromEnd ? $"^{Value}" : Value.ToString(); |
| 46 | + } |
| 47 | + |
| 48 | + internal readonly struct Range : IEquatable<Range> |
| 49 | + { |
| 50 | + public Index Start { get; } |
| 51 | + public Index End { get; } |
| 52 | + |
| 53 | + public Range(Index start, Index end) { Start = start; End = end; } |
| 54 | + |
| 55 | + public static Range StartAt(Index start) => new Range(start, Index.End); |
| 56 | + public static Range EndAt(Index end) => new Range(Index.Start, end); |
| 57 | + public static Range All => new Range(Index.Start, Index.End); |
| 58 | + |
| 59 | + public (int Offset, int Length) GetOffsetAndLength(int length) |
| 60 | + { |
| 61 | + var start = Start.GetOffset(length); |
| 62 | + var end = End.GetOffset(length); |
| 63 | + return (start, end - start); |
| 64 | + } |
| 65 | + |
| 66 | + public bool Equals(Range other) => Start.Equals(other.Start) && End.Equals(other.End); |
| 67 | + public override bool Equals(object? obj) => obj is Range other && Equals(other); |
| 68 | + public override int GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode(); |
| 69 | + public override string ToString() => $"{Start}..{End}"; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// ── RuntimeHelpers.GetSubArray: required when using range operators on arrays ── |
| 74 | +namespace System.Runtime.CompilerServices |
| 75 | +{ |
| 76 | + internal static class RuntimeHelpersPolyfill |
| 77 | + { |
| 78 | + // The compiler calls RuntimeHelpers.GetSubArray<T> for array[range]. |
| 79 | + // We provide it here so that pattern compiles on .NET Framework. |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +namespace System |
| 84 | +{ |
| 85 | + internal static class RuntimeHelpersEx |
| 86 | + { |
| 87 | + public static T[] GetSubArray<T>(T[] array, Range range) |
| 88 | + { |
| 89 | + var (offset, length) = range.GetOffsetAndLength(array.Length); |
| 90 | + var dest = new T[length]; |
| 91 | + Array.Copy(array, offset, dest, 0, length); |
| 92 | + return dest; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// ── Extension methods for .NET Framework ── |
| 98 | +namespace MiniSoftware |
| 99 | +{ |
| 100 | + internal static class NetFxPolyfills |
| 101 | + { |
| 102 | + // string.Contains(string, StringComparison) — .NET Core 2.1+ |
| 103 | + public static bool Contains(this string s, string value, StringComparison comparison) |
| 104 | + => s.IndexOf(value, comparison) >= 0; |
| 105 | + |
| 106 | + // string.Split(char, StringSplitOptions) — .NET Core 2.1+ |
| 107 | + public static string[] Split(this string s, char separator, StringSplitOptions options) |
| 108 | + => s.Split(new[] { separator }, options); |
| 109 | + |
| 110 | + // string.StartsWith(char) — .NET Core only |
| 111 | + public static bool StartsWith(this string s, char c) |
| 112 | + => s.Length > 0 && s[0] == c; |
| 113 | + |
| 114 | + // string.EndsWith(char) — .NET Core only |
| 115 | + public static bool EndsWith(this string s, char c) |
| 116 | + => s.Length > 0 && s[s.Length - 1] == c; |
| 117 | + |
| 118 | + // Stream.Write(byte[]) — Stream.Write(ReadOnlySpan<byte>) is .NET Core 2.1+ |
| 119 | + public static void Write(this Stream stream, byte[] buffer) |
| 120 | + => stream.Write(buffer, 0, buffer.Length); |
| 121 | + |
| 122 | + // KeyValuePair Deconstruct — .NET Core 2.0+ |
| 123 | + public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value) |
| 124 | + { |
| 125 | + key = kvp.Key; |
| 126 | + value = kvp.Value; |
| 127 | + } |
| 128 | + |
| 129 | + // Math.Clamp — .NET Core 2.0+ |
| 130 | + public static int Clamp(int value, int min, int max) => |
| 131 | + value < min ? min : value > max ? max : value; |
| 132 | + public static float Clamp(float value, float min, float max) => |
| 133 | + value < min ? min : value > max ? max : value; |
| 134 | + public static double Clamp(double value, double min, double max) => |
| 135 | + value < min ? min : value > max ? max : value; |
| 136 | + |
| 137 | + // Array.Fill — .NET Core 2.0+ |
| 138 | + public static void Fill<T>(T[] array, T value) |
| 139 | + { |
| 140 | + for (int i = 0; i < array.Length; i++) array[i] = value; |
| 141 | + } |
| 142 | + public static void Fill<T>(T[] array, T value, int startIndex, int count) |
| 143 | + { |
| 144 | + for (int i = startIndex; i < startIndex + count; i++) array[i] = value; |
| 145 | + } |
| 146 | + |
| 147 | + // Encoding.Latin1 — .NET 5+ |
| 148 | + public static Encoding Latin1 => Encoding.GetEncoding(28591); |
| 149 | + } |
| 150 | +} |
| 151 | +#endif |
0 commit comments