aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/TextSpan.cs
blob: 83fbc68df0b265c4561c663f9afc473cfc0fa62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) 2013-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Collections.Generic;
using System.Text;

namespace Crow.Text
{
	public struct TextSpan : IEquatable<TextSpan>
	{
		public readonly int Start;
		public readonly int End;
		public TextSpan (int start, int end) {
			Start = start;
			End = end;
		}
		public static TextSpan FromStartAndLength (int start, int length = 0) => new TextSpan (start, start + length);
		public bool IsEmpty => Start == End;
		public int Length => End - Start;

		public bool Equals(TextSpan other)
			=> Start == other.Start && End == other.End;
		public override bool Equals(object obj)
			=> obj is TextSpan ts ? Equals(ts) : false;

		public override int GetHashCode()
			=> HashCode.Combine(Start, End);
		public static bool operator ==(TextSpan left, TextSpan right)
			=> left.Equals (right);
		public static bool operator !=(TextSpan left, TextSpan right)
			=> !left.Equals (right);
		public override string ToString() => $"{Start},{End}";
		public bool Contains (int absolutePosition)
			=> absolutePosition >= Start && absolutePosition < End;
		public bool Contains (TextSpan span)
			=> Start <= span.Start && End >= span.End;
	}
}