Range.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. namespace System
  7. {
  8. /// <summary>Represent a range has start and end indexes.</summary>
  9. /// <remarks>
  10. /// Range is used by the C# compiler to support the range syntax.
  11. /// <code>
  12. /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 };
  13. /// int[] subArray1 = someArray[0..2]; // { 1, 2 }
  14. /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 }
  15. /// </code>
  16. /// </remarks>
  17. public readonly struct Range : IEquatable<Range>
  18. {
  19. /// <summary>Represent the inclusive start index of the Range.</summary>
  20. public Index Start { get; }
  21. /// <summary>Represent the exclusive end index of the Range.</summary>
  22. public Index End { get; }
  23. /// <summary>Construct a Range object using the start and end indexes.</summary>
  24. /// <param name="start">Represent the inclusive start index of the range.</param>
  25. /// <param name="end">Represent the exclusive end index of the range.</param>
  26. public Range(Index start, Index end)
  27. {
  28. Start = start;
  29. End = end;
  30. }
  31. /// <summary>Indicates whether the current Range object is equal to another object of the same type.</summary>
  32. /// <param name="value">An object to compare with this object</param>
  33. public override bool Equals(object value)
  34. {
  35. if (value is Range)
  36. {
  37. Range r = (Range)value;
  38. return r.Start.Equals(Start) && r.End.Equals(End);
  39. }
  40. return false;
  41. }
  42. /// <summary>Indicates whether the current Range object is equal to another Range object.</summary>
  43. /// <param name="other">An object to compare with this object</param>
  44. public bool Equals (Range other) => other.Start.Equals(Start) && other.End.Equals(End);
  45. /// <summary>Returns the hash code for this instance.</summary>
  46. public override int GetHashCode()
  47. {
  48. return HashCode.Combine(Start.GetHashCode(), End.GetHashCode());
  49. }
  50. /// <summary>Converts the value of the current Range object to its equivalent string representation.</summary>
  51. public override string ToString()
  52. {
  53. Span<char> span = stackalloc char[2 + (2 * 11)]; // 2 for "..", then for each index 1 for '^' and 10 for longest possible uint
  54. int charsWritten;
  55. int pos = 0;
  56. if (Start.IsFromEnd)
  57. {
  58. span[0] = '^';
  59. pos = 1;
  60. }
  61. bool formatted = ((uint)Start.Value).TryFormat(span.Slice(pos), out charsWritten);
  62. Debug.Assert(formatted);
  63. pos += charsWritten;
  64. span[pos++] = '.';
  65. span[pos++] = '.';
  66. if (End.IsFromEnd)
  67. {
  68. span[pos++] = '^';
  69. }
  70. formatted = ((uint)End.Value).TryFormat(span.Slice(pos), out charsWritten);
  71. Debug.Assert(formatted);
  72. pos += charsWritten;
  73. return new string(span.Slice(0, pos));
  74. }
  75. /// <summary>Create a Range object starting from start index to the end of the collection.</summary>
  76. public static Range StartAt(Index start) => new Range(start, Index.End);
  77. /// <summary>Create a Range object starting from first element in the collection to the end Index.</summary>
  78. public static Range EndAt(Index end) => new Range(Index.Start, end);
  79. /// <summary>Create a Range object starting from first element to the end.</summary>
  80. public static Range All => new Range(Index.Start, Index.End);
  81. /// <summary>Destruct the range object according to a collection length and return the start offset from the beginning and the length of this range.</summary>
  82. /// <param name="length">The length of the collection that the range will be used with. length has to be a positive value</param>
  83. /// <remarks>
  84. /// For performance reason, we don't validate the input length parameter against negative values.
  85. /// It is expected Range will be used with collections which always have non negative length/count.
  86. /// We validate the range is inside the length scope though.
  87. /// </remarks>
  88. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  89. public OffsetAndLength GetOffsetAndLength(int length)
  90. {
  91. int start;
  92. Index startIndex = Start;
  93. if (startIndex.IsFromEnd)
  94. start = length - startIndex.Value;
  95. else
  96. start = startIndex.Value;
  97. int end;
  98. Index endIndex = End;
  99. if (endIndex.IsFromEnd)
  100. end = length - endIndex.Value;
  101. else
  102. end = endIndex.Value;
  103. if ((uint)end > (uint)length || (uint)start > (uint)end)
  104. {
  105. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
  106. }
  107. return new OffsetAndLength(start, end - start);
  108. }
  109. public readonly struct OffsetAndLength
  110. {
  111. public int Offset { get; }
  112. public int Length { get; }
  113. public OffsetAndLength(int offset, int length)
  114. {
  115. Offset = offset;
  116. Length = length;
  117. }
  118. public void Deconstruct(out int offset, out int length)
  119. {
  120. offset = Offset;
  121. length = Length;
  122. }
  123. }
  124. }
  125. }