-
Notifications
You must be signed in to change notification settings - Fork 4k
/
LinePosition.cs
162 lines (140 loc) · 5.18 KB
/
LinePosition.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.Serialization;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Text
{
/// <summary>
/// Immutable representation of a line number and position within a SourceText instance.
/// </summary>
[DataContract]
public readonly struct LinePosition : IEquatable<LinePosition>, IComparable<LinePosition>
{
/// <summary>
/// A <see cref="LinePosition"/> that represents position 0 at line 0.
/// </summary>
public static LinePosition Zero => default(LinePosition);
[DataMember(Order = 0)]
private readonly int _line;
[DataMember(Order = 1)]
private readonly int _character;
/// <summary>
/// Initializes a new instance of a <see cref="LinePosition"/> with the given line and character.
/// </summary>
/// <param name="line">
/// The line of the line position. The first line in a file is defined as line 0 (zero based line numbering).
/// </param>
/// <param name="character">
/// The character position in the line.
/// </param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="line"/> or <paramref name="character"/> is less than zero. </exception>
public LinePosition(int line, int character)
{
if (line < 0)
{
throw new ArgumentOutOfRangeException(nameof(line));
}
if (character < 0)
{
throw new ArgumentOutOfRangeException(nameof(character));
}
_line = line;
_character = character;
}
// internal constructor that supports a line number == -1.
// VB allows users to specify a 1-based line number of 0 when processing
// externalsource directives, which get decremented during conversion to 0-based line numbers.
// in this case the line number can be -1.
internal LinePosition(int character)
{
if (character < 0)
{
throw new ArgumentOutOfRangeException(nameof(character));
}
_line = -1;
_character = character;
}
/// <summary>
/// The line number. The first line in a file is defined as line 0 (zero based line numbering).
/// </summary>
public int Line
{
get { return _line; }
}
/// <summary>
/// The character position within the line.
/// </summary>
public int Character
{
get { return _character; }
}
/// <summary>
/// Determines whether two <see cref="LinePosition"/> are the same.
/// </summary>
public static bool operator ==(LinePosition left, LinePosition right)
{
return left.Equals(right);
}
/// <summary>
/// Determines whether two <see cref="LinePosition"/> are different.
/// </summary>
public static bool operator !=(LinePosition left, LinePosition right)
{
return !left.Equals(right);
}
/// <summary>
/// Determines whether two <see cref="LinePosition"/> are the same.
/// </summary>
/// <param name="other">The object to compare.</param>
public bool Equals(LinePosition other)
{
return other.Line == this.Line && other.Character == this.Character;
}
/// <summary>
/// Determines whether two <see cref="LinePosition"/> are the same.
/// </summary>
/// <param name="obj">The object to compare.</param>
public override bool Equals(object? obj)
{
return obj is LinePosition && Equals((LinePosition)obj);
}
/// <summary>
/// Provides a hash function for <see cref="LinePosition"/>.
/// </summary>
public override int GetHashCode()
{
return Hash.Combine(Line, Character);
}
/// <summary>
/// Provides a string representation for <see cref="LinePosition"/>.
/// </summary>
/// <example>0,10</example>
public override string ToString()
{
return Line + "," + Character;
}
public int CompareTo(LinePosition other)
{
int result = _line.CompareTo(other._line);
return (result != 0) ? result : _character.CompareTo(other.Character);
}
public static bool operator >(LinePosition left, LinePosition right)
{
return left.CompareTo(right) > 0;
}
public static bool operator >=(LinePosition left, LinePosition right)
{
return left.CompareTo(right) >= 0;
}
public static bool operator <(LinePosition left, LinePosition right)
{
return left.CompareTo(right) < 0;
}
public static bool operator <=(LinePosition left, LinePosition right)
{
return left.CompareTo(right) <= 0;
}
}
}