Range.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. namespace System
  5. {
  6. public readonly struct Range : IEquatable<Range>
  7. {
  8. public Index Start { get; }
  9. public Index End { get; }
  10. private Range(Index start, Index end)
  11. {
  12. Start = start;
  13. End = end;
  14. }
  15. public override bool Equals(object value)
  16. {
  17. if (value is Range)
  18. {
  19. Range r = (Range)value;
  20. return r.Start.Equals(Start) && r.End.Equals(End);
  21. }
  22. return false;
  23. }
  24. public bool Equals (Range other) => other.Start.Equals(Start) && other.End.Equals(End);
  25. public override int GetHashCode()
  26. {
  27. return HashCode.Combine(Start.GetHashCode(), End.GetHashCode());
  28. }
  29. public override string ToString()
  30. {
  31. return Start + ".." + End;
  32. }
  33. public static Range Create(Index start, Index end) => new Range(start, end);
  34. public static Range FromStart(Index start) => new Range(start, new Index(0, fromEnd: true));
  35. public static Range ToEnd(Index end) => new Range(new Index(0, fromEnd: false), end);
  36. public static Range All() => new Range(new Index(0, fromEnd: false), new Index(0, fromEnd: true));
  37. }
  38. }