|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +#pragma warning disable CS0436 // Type conflicts with imported type |
| 8 | +#pragma warning disable S3427 // Method overloads with default parameter values should not overlap |
| 9 | +#pragma warning disable SA1642 // Constructor summary documentation should begin with standard text |
| 10 | +#pragma warning disable IDE0011 // Add braces |
| 11 | +#pragma warning disable SA1623 // Property summary documentation should match accessors |
| 12 | +#pragma warning disable IDE0023 // Use block body for conversion operator |
| 13 | +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one |
| 14 | +#pragma warning disable LA0001 // Use the 'Microsoft.Shared.Diagnostics.Throws' class instead of explicitly throwing exception for improved performance |
| 15 | +#pragma warning disable CA1305 // Specify IFormatProvider |
| 16 | + |
| 17 | +namespace System |
| 18 | +{ |
| 19 | + internal readonly struct Index : IEquatable<Index> |
| 20 | + { |
| 21 | + private readonly int _value; |
| 22 | + |
| 23 | + /// <summary>Construct an Index using a value and indicating if the index is from the start or from the end.</summary> |
| 24 | + /// <param name="value">The index value. it has to be zero or positive number.</param> |
| 25 | + /// <param name="fromEnd">Indicating if the index is from the start or from the end.</param> |
| 26 | + /// <remarks> |
| 27 | + /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. |
| 28 | + /// </remarks> |
| 29 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 30 | + public Index(int value, bool fromEnd = false) |
| 31 | + { |
| 32 | + if (value < 0) |
| 33 | + { |
| 34 | + ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 35 | + } |
| 36 | + |
| 37 | + if (fromEnd) |
| 38 | + _value = ~value; |
| 39 | + else |
| 40 | + _value = value; |
| 41 | + } |
| 42 | + |
| 43 | + // The following private constructors mainly created for perf reason to avoid the checks |
| 44 | + private Index(int value) |
| 45 | + { |
| 46 | + _value = value; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary>Create an Index pointing at first element.</summary> |
| 50 | + public static Index Start => new Index(0); |
| 51 | + |
| 52 | + /// <summary>Create an Index pointing at beyond last element.</summary> |
| 53 | + public static Index End => new Index(~0); |
| 54 | + |
| 55 | + /// <summary>Create an Index from the start at the position indicated by the value.</summary> |
| 56 | + /// <param name="value">The index value from the start.</param> |
| 57 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 58 | + public static Index FromStart(int value) |
| 59 | + { |
| 60 | + if (value < 0) |
| 61 | + { |
| 62 | + ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 63 | + } |
| 64 | + |
| 65 | + return new Index(value); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary>Create an Index from the end at the position indicated by the value.</summary> |
| 69 | + /// <param name="value">The index value from the end.</param> |
| 70 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 71 | + public static Index FromEnd(int value) |
| 72 | + { |
| 73 | + if (value < 0) |
| 74 | + { |
| 75 | + ThrowValueArgumentOutOfRange_NeedNonNegNumException(); |
| 76 | + } |
| 77 | + |
| 78 | + return new Index(~value); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary>Returns the index value.</summary> |
| 82 | + public int Value |
| 83 | + { |
| 84 | + get |
| 85 | + { |
| 86 | + if (_value < 0) |
| 87 | + return ~_value; |
| 88 | + else |
| 89 | + return _value; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary>Indicates whether the index is from the start or the end.</summary> |
| 94 | + public bool IsFromEnd => _value < 0; |
| 95 | + |
| 96 | + /// <summary>Calculate the offset from the start using the giving collection length.</summary> |
| 97 | + /// <param name="length">The length of the collection that the Index will be used with. length has to be a positive value.</param> |
| 98 | + /// <remarks> |
| 99 | + /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. |
| 100 | + /// we don't validate either the returned offset is greater than the input length. |
| 101 | + /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and |
| 102 | + /// then used to index a collection will get out of range exception which will be same affect as the validation. |
| 103 | + /// </remarks> |
| 104 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 105 | + public int GetOffset(int length) |
| 106 | + { |
| 107 | + int offset = _value; |
| 108 | + if (IsFromEnd) |
| 109 | + { |
| 110 | + // offset = length - (~value) |
| 111 | + // offset = length + (~(~value) + 1) |
| 112 | + // offset = length + value + 1 |
| 113 | + |
| 114 | + offset += length + 1; |
| 115 | + } |
| 116 | + |
| 117 | + return offset; |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary>Indicates whether the current Index object is equal to another object of the same type.</summary> |
| 121 | + /// <param name="value">An object to compare with this object.</param> |
| 122 | + public override bool Equals([NotNullWhen(true)] object? value) => value is Index && _value == ((Index)value)._value; |
| 123 | + |
| 124 | + /// <summary>Indicates whether the current Index object is equal to another Index object.</summary> |
| 125 | + /// <param name="other">An object to compare with this object.</param> |
| 126 | + public bool Equals(Index other) => _value == other._value; |
| 127 | + |
| 128 | + /// <summary>Returns the hash code for this instance.</summary> |
| 129 | + public override int GetHashCode() => _value; |
| 130 | + |
| 131 | + /// <summary>Converts integer number to an Index.</summary> |
| 132 | + public static implicit operator Index(int value) => FromStart(value); |
| 133 | + |
| 134 | + /// <summary>Converts the value of the current Index object to its equivalent string representation.</summary> |
| 135 | + public override string ToString() |
| 136 | + { |
| 137 | + if (IsFromEnd) |
| 138 | + return ToStringFromEnd(); |
| 139 | + |
| 140 | + return ((uint)Value).ToString(); |
| 141 | + } |
| 142 | + |
| 143 | + private static void ThrowValueArgumentOutOfRange_NeedNonNegNumException() |
| 144 | + { |
| 145 | + throw new ArgumentOutOfRangeException("value", "value must be non-negative"); |
| 146 | + } |
| 147 | + |
| 148 | + private string ToStringFromEnd() |
| 149 | + { |
| 150 | +#if (!NETSTANDARD2_0 && !NETFRAMEWORK) |
| 151 | + Span<char> span = stackalloc char[11]; // 1 for ^ and 10 for longest possible uint value |
| 152 | + bool formatted = ((uint)Value).TryFormat(span.Slice(1), out int charsWritten); |
| 153 | + span[0] = '^'; |
| 154 | + return new string(span.Slice(0, charsWritten + 1)); |
| 155 | +#else |
| 156 | + return '^' + Value.ToString(); |
| 157 | +#endif |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments