-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextSpan.cs
187 lines (161 loc) · 4.34 KB
/
TextSpan.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using UnityEngine;
using System.Collections;
namespace CSharpCodeParser
{
public struct TextPosition
{
public int line;
public int index;
public TextPosition(int line, int index)
{
this.line = line;
this.index = index;
}
public static TextPosition operator + (TextPosition other, int offset)
{
return new TextPosition { line = other.line, index = other.index + offset };
}
public static bool operator == (TextPosition lhs, TextPosition rhs)
{
return lhs.line == rhs.line && lhs.index == rhs.index;
}
public static bool operator != (TextPosition lhs, TextPosition rhs)
{
return lhs.line != rhs.line || lhs.index != rhs.index;
}
public static bool operator < (TextPosition lhs, TextPosition rhs)
{
return lhs.line < rhs.line || lhs.line == rhs.line && lhs.index < rhs.index;
}
public static bool operator <= (TextPosition lhs, TextPosition rhs)
{
return lhs.line < rhs.line || lhs.line == rhs.line && lhs.index <= rhs.index;
}
public static bool operator > (TextPosition lhs, TextPosition rhs)
{
return lhs.line > rhs.line || lhs.line == rhs.line && lhs.index > rhs.index;
}
public static bool operator >= (TextPosition lhs, TextPosition rhs)
{
return lhs.line > rhs.line || lhs.line == rhs.line && lhs.index >= rhs.index;
}
public override bool Equals(object obj)
{
if (!(obj is TextPosition))
return false;
var rhs = (TextPosition) obj;
return line == rhs.line && index == rhs.index;
}
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
var hash = (int)2166136261;
hash = hash * 16777619 ^ line.GetHashCode();
hash = hash * 16777619 ^ index.GetHashCode();
return hash;
}
}
public override string ToString()
{
return "TextPosition (line: " + line + ", index: " + index + ")";
}
}
public struct TextOffset
{
public int lines;
public int indexOffset;
}
public struct TextSpan
{
public int line;
public int index;
public int lineOffset;
public int indexOffset;
public override string ToString()
{
return "TextSpan{ line = " + (line+1) + ", fromChar = " + index + ", lineOffset = " + lineOffset + ", toChar = " + indexOffset + " }";
}
public static TextSpan CreateEmpty(TextPosition position)
{
return new TextSpan { line = position.line, index = position.index };
}
public static TextSpan Create(TextPosition from, TextPosition to)
{
return new TextSpan
{
line = from.line,
index = from.index,
lineOffset = to.line - from.line,
indexOffset = to.index - (to.line == from.line ? from.index : 0)
};
}
public static TextSpan CreateBetween(TextSpan from, TextSpan to)
{
return Create(from.EndPosition, to.StartPosition);
}
public static TextSpan CreateEnclosing(TextSpan from, TextSpan to)
{
return Create(from.StartPosition, to.EndPosition);
}
public static TextSpan Create(TextPosition start, TextOffset length)
{
return new TextSpan
{
line = start.line,
index = start.index,
lineOffset = length.lines,
indexOffset = length.indexOffset
};
}
public TextPosition StartPosition
{
get { return new TextPosition { line = line, index = index }; }
set
{
if (value.line == line + lineOffset)
{
line = value.line;
lineOffset = 0;
indexOffset = index + indexOffset - value.index;
index = value.index;
}
else
{
lineOffset = line + lineOffset - value.line;
line = value.line;
index = value.index;
}
}
}
public TextPosition EndPosition
{
get { return new TextPosition { line = line + lineOffset, index = indexOffset + (lineOffset == 0 ? index : 0) }; }
set
{
if (value.line == line)
{
lineOffset = 0;
indexOffset = value.index - index;
}
else
{
lineOffset = value.line - line;
indexOffset = value.index;
}
}
}
public void Offset(int deltaLines, int deltaIndex)
{
line += deltaLines;
index += deltaIndex;
}
public bool Contains(TextPosition position)
{
return !(position.line < line
|| position.line == line && (position.index < index || lineOffset == 0 && position.index > index + indexOffset)
|| position.line > line + lineOffset
|| position.line == line + lineOffset && position.index > indexOffset);
}
}
}