aspclassic-core/AspClassic.Parser/Span.cs
Jelle Luteijn 484dbfc9d9 progress
2022-05-15 11:19:49 +02:00

80 lines
2.1 KiB
C#

namespace AspClassic.Parser;
/// <summary>
/// Stores the location of a span of text.
/// </summary>
/// <remarks>The end location is exclusive.</remarks>
public struct Span
{
private readonly Location _Start;
private readonly Location _Finish;
/// <summary>
/// The start location of the span.
/// </summary>
public Location Start => _Start;
/// <summary>
/// The end location of the span.
/// </summary>
public Location Finish => _Finish;
/// <summary>
/// Whether the locations in the span are valid.
/// </summary>
public bool IsValid => Start.IsValid && Finish.IsValid;
/// <summary>
/// Constructs a new span with a specific start and end location.
/// </summary>
/// <param name="start">The beginning of the span.</param>
/// <param name="finish">The end of the span.</param>
public Span(Location start, Location finish)
{
this = default(Span);
_Start = start;
_Finish = finish;
}
/// <summary>
/// Compares two specified Span values to see if they are equal.
/// </summary>
/// <param name="left">One span to compare.</param>
/// <param name="right">The other span to compare.</param>
/// <returns>True if the spans are the same, False otherwise.</returns>
public static bool operator ==(Span left, Span right)
{
return left.Start.Index == right.Start.Index && left.Finish.Index == right.Finish.Index;
}
/// <summary>
/// Compares two specified Span values to see if they are not equal.
/// </summary>
/// <param name="left">One span to compare.</param>
/// <param name="right">The other span to compare.</param>
/// <returns>True if the spans are not the same, False otherwise.</returns>
public static bool operator !=(Span left, Span right)
{
return left.Start.Index != right.Start.Index || left.Finish.Index != right.Finish.Index;
}
public override string ToString()
{
return Start.ToString() + " - " + Finish.ToString();
}
public override bool Equals(object obj)
{
if (obj is Span)
{
return this == (Span)obj;
}
return false;
}
public override int GetHashCode()
{
return checked((int)((Start.Index ^ Finish.Index) & 0xFFFFFFFFu));
}
}