40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
namespace Upsilon.Text
|
|
{
|
|
public struct TextSpan
|
|
{
|
|
public int StartLine { get; }
|
|
public int StartPosition { get; }
|
|
public int EndLine { get; }
|
|
public int EndPosition { get; }
|
|
|
|
public TextSpan(int startLine, int startPosition, int endLine, int endPosition)
|
|
{
|
|
StartLine = startLine;
|
|
StartPosition = startPosition;
|
|
EndLine = endLine;
|
|
EndPosition = endPosition;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"({StartLine}, {StartPosition}) - ({EndLine}, {EndPosition})";
|
|
}
|
|
|
|
public static TextSpan Between(TextSpan start, TextSpan end)
|
|
{
|
|
return new TextSpan(start.StartLine, start.StartPosition, end.EndLine, end.EndPosition);
|
|
}
|
|
|
|
public bool Contains(int linePosition, int characterPosition)
|
|
{
|
|
if (StartLine == EndLine && linePosition == StartLine)
|
|
{
|
|
return characterPosition >= StartPosition && characterPosition <= EndPosition;
|
|
}
|
|
if (StartLine == linePosition && StartPosition <= characterPosition) return true;
|
|
if (StartLine < linePosition && EndLine > linePosition) return true;
|
|
if (EndLine == linePosition && EndPosition >= characterPosition) return true;
|
|
return false;
|
|
}
|
|
}
|
|
} |