Range.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. value is Range r &&
  35. r.Start.Equals(Start) &&
  36. r.End.Equals(End);
  37. /// <summary>Indicates whether the current Range object is equal to another Range object.</summary>
  38. /// <param name="other">An object to compare with this object</param>
  39. public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End);
  40. /// <summary>Returns the hash code for this instance.</summary>
  41. public override int GetHashCode()
  42. {
  43. return HashCode.Combine(Start.GetHashCode(), End.GetHashCode());
  44. }
  45. /// <summary>Converts the value of the current Range object to its equivalent string representation.</summary>
  46. public override string ToString()
  47. {
  48. Span<char> span = stackalloc char[2 + (2 * 11)]; // 2 for "..", then for each index 1 for '^' and 10 for longest possible uint
  49. int charsWritten;
  50. int pos = 0;
  51. if (Start.IsFromEnd)
  52. {
  53. span[0] = '^';
  54. pos = 1;
  55. }
  56. bool formatted = ((uint)Start.Value).TryFormat(span.Slice(pos), out charsWritten);
  57. Debug.Assert(formatted);
  58. pos += charsWritten;
  59. span[pos++] = '.';
  60. span[pos++] = '.';
  61. if (End.IsFromEnd)
  62. {
  63. span[pos++] = '^';
  64. }
  65. formatted = ((uint)End.Value).TryFormat(span.Slice(pos), out charsWritten);
  66. Debug.Assert(formatted);
  67. pos += charsWritten;
  68. return new string(span.Slice(0, pos));
  69. }
  70. /// <summary>Create a Range object starting from start index to the end of the collection.</summary>
  71. public static Range StartAt(Index start) => new Range(start, Index.End);
  72. /// <summary>Create a Range object starting from first element in the collection to the end Index.</summary>
  73. public static Range EndAt(Index end) => new Range(Index.Start, end);
  74. /// <summary>Create a Range object starting from first element to the end.</summary>
  75. public static Range All => new Range(Index.Start, Index.End);
  76. /// <summary>Calculate the start offset and length of range object using a collection length.</summary>
  77. /// <param name="length">The length of the collection that the range will be used with. length has to be a positive value.</param>
  78. /// <remarks>
  79. /// For performance reason, we don't validate the input length parameter against negative values.
  80. /// It is expected Range will be used with collections which always have non negative length/count.
  81. /// We validate the range is inside the length scope though.
  82. /// </remarks>
  83. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  84. public (int Offset, int Length) GetOffsetAndLength(int length)
  85. {
  86. int start;
  87. Index startIndex = Start;
  88. if (startIndex.IsFromEnd)
  89. start = length - startIndex.Value;
  90. else
  91. start = startIndex.Value;
  92. int end;
  93. Index endIndex = End;
  94. if (endIndex.IsFromEnd)
  95. end = length - endIndex.Value;
  96. else
  97. end = endIndex.Value;
  98. if ((uint)end > (uint)length || (uint)start > (uint)end)
  99. {
  100. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
  101. }
  102. return (start, end - start);
  103. }
  104. }
  105. }