Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ISegment.cs « MonoDevelop.Core.Text « MonoDevelop.Core « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02c0acfda23262ec8ed47d1923be5780f98b13ce (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// ISegment.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;

namespace MonoDevelop.Core.Text
{
	/// <summary>
	/// An (Offset,Length)-pair.
	/// </summary>
	public interface ISegment
	{
		/// <summary>
		/// Gets the start offset of the segment.
		/// </summary>
		int Offset { get; }

		/// <summary>
		/// Gets the length of the segment.
		/// </summary>
		/// <remarks>For line segments (IDocumentLine), the length does not include the line delimeter.</remarks>
		int Length { get; }

		/// <summary>
		/// Gets the end offset of the segment.
		/// </summary>
		/// <remarks>EndOffset = Offset + Length;</remarks>
		int EndOffset { get; }
	}

	/// <summary>
	/// An (Offset, Length) pair representing a text span.
	/// </summary>
	public struct TextSegment : IEquatable<TextSegment>, ISegment
	{
		public static readonly TextSegment Invalid = new TextSegment (-1, 0);

		readonly int offset;

		/// <summary>
		///  Gets the start offset of the segment. 
		/// </summary>
		/// <value>
		/// The offset.
		/// </value>
		public int Offset {
			get {
				return offset;
			}
		}

		readonly int length;

		/// <summary>
		/// Gets the length of the segment. 
		/// </summary>
		/// <value>
		/// The length.
		/// </value>
		public int Length {
			get {
				return length;
			}
		}

		/// <summary>
		/// Gets the end offset of the segment. 
		/// </summary>
		/// <remarks>
		/// EndOffset = Offset + Length;
		/// </remarks>
		/// <value>
		/// The end offset.
		/// </value>
		public int EndOffset {
			get {
				return Offset + Length;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is empty.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is empty; otherwise, <c>false</c>.
		/// </value>
		public bool IsEmpty {
			get {
				return Length == 0;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is invalid.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is invalid; otherwise, <c>false</c>.
		/// </value>
		public bool IsInvalid {
			get {
				return Offset < 0;
			}
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TextSegment"/> struct.
		/// </summary>
		/// <param name='offset'>
		/// The offset of the segment.
		/// </param>
		/// <param name='length'>
		/// The length of the segment.
		/// </param>
		public TextSegment (int offset, int length)
		{
			this.offset = offset;
			this.length = length;
		}

		public static bool operator == (TextSegment left, TextSegment right)
		{
			return Equals (left, right);
		}

		public static bool operator != (TextSegment left, TextSegment right)
		{
			return !Equals (left, right);
		}

		public static bool Equals (TextSegment left, TextSegment right)
		{
			return left.Offset == right.Offset && left.Length == right.Length;
		}

		/// <summary>
		/// Determines whether this instance is inside the specified offset. 
		/// </summary>
		/// <returns>
		/// <c>true</c> if this instance is inside the specified offset (upper bound inclusive); otherwise, <c>false</c>.
		/// </returns>
		/// <param name='offset'>
		/// The offset offset.
		/// </param>
		public bool IsInside (int offset)
		{
			return Offset <= offset && offset <= EndOffset;
		}

		/// <summary>
		/// Determines whether the specified <see cref="TextSegment"/> is equal to the current <see cref="TextSegment"/>.
		/// </summary>
		/// <param name='other'>
		/// The <see cref="TextSegment"/> to compare with the current <see cref="TextSegment"/>.
		/// </param>
		/// <returns>
		/// <c>true</c> if the specified <see cref="TextSegment"/> is equal to the current
		/// <see cref="TextSegment"/>; otherwise, <c>false</c>.
		/// </returns>
		public bool Equals (TextSegment other)
		{
			return Equals (this, other);
		}

		/// <summary>
		/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="TextSegment"/>.
		/// </summary>
		/// <param name='obj'>
		/// The <see cref="System.Object"/> to compare with the current <see cref="TextSegment"/>.
		/// </param>
		/// <returns>
		/// <c>true</c> if the specified <see cref="System.Object"/> is equal to the current
		/// <see cref="TextSegment"/>; otherwise, <c>false</c>.
		/// </returns>
		public override bool Equals (object obj)
		{
			return obj is ISegment && Equals (this, (ISegment)obj);
		}

		/// <summary>
		/// Serves as a hash function for a <see cref="TextSegment"/> object.
		/// </summary>
		/// <returns>
		/// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table.
		/// </returns>
		public override int GetHashCode ()
		{
			return Offset ^ Length;
		}

		public static TextSegment FromBounds (int startOffset, int endOffset)
		{
			if (startOffset > endOffset)
				throw new ArgumentOutOfRangeException ("endOffset", "endOffset < startOffset");
			return new TextSegment (startOffset, endOffset - startOffset);
		}

		/// <summary>
		/// Returns a <see cref="System.String"/> that represents the current <see cref="TextSegment"/>.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents the current <see cref="TextSegment"/>.
		/// </returns>
		public override string ToString ()
		{
			return string.Format ("[TextSegment: Offset={0}, Length={1}]", Offset, Length);
		}
	}

	/// <summary>
	/// An abstract implementation of the ISegment (Offset, Length) pair representing a text span.
	/// </summary>
	public abstract class AbstractSegment : ISegment
	{
		readonly int offset;

		/// <summary>
		///  Gets the start offset of the segment. 
		/// </summary>
		/// <value>
		/// The offset.
		/// </value>
		public int Offset {
			get {
				return offset;
			}
		}

		readonly int length;

		/// <summary>
		/// Gets the length of the segment. 
		/// </summary>
		/// <value>
		/// The length.
		/// </value>
		public int Length {
			get {
				return length;
			}
		}

		/// <summary>
		/// Gets the end offset of the segment. 
		/// </summary>
		/// <remarks>
		/// EndOffset = Offset + Length;
		/// </remarks>
		/// <value>
		/// The end offset.
		/// </value>
		public int EndOffset {
			get {
				return Offset + Length;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is empty.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is empty; otherwise, <c>false</c>.
		/// </value>
		public bool IsEmpty {
			get {
				return Length == 0;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is invalid.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is invalid; otherwise, <c>false</c>.
		/// </value>
		public bool IsInvalid {
			get {
				return Offset < 0;
			}
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TextSegment"/> struct.
		/// </summary>
		/// <param name='offset'>
		/// The offset of the segment.
		/// </param>
		/// <param name='length'>
		/// The length of the segment.
		/// </param>
		protected AbstractSegment (int offset, int length)
		{
			this.offset = offset;
			this.length = length;
		}


		/// <summary>
		/// Initializes a new instance of the <see cref="TextSegment"/> struct.
		/// </summary>
		protected AbstractSegment (ISegment segment)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			this.offset = segment.Offset;
			this.length = segment.Length;
		}
	}


	/// <summary>
	/// Extension methods for <see cref="ISegment"/>.
	/// </summary>
	public static class ISegmentExtensions
	{
		/// <summary>
		/// Gets whether <paramref name="segment"/> fully contains the specified segment.
		/// </summary>
		/// <remarks>
		/// Use <c>segment.Contains(offset, 0)</c> to detect whether a segment (end inclusive) contains offset;
		/// use <c>segment.Contains(offset, 1)</c> to detect whether a segment (end exclusive) contains offset.
		/// </remarks>
		public static bool Contains (this ISegment segment, int offset, int length)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return segment.Offset <= offset && offset + length <= segment.EndOffset;
		}

		/// <summary>
		/// Gets whether <paramref name="segment"/> fully contains the specified segment.
		/// </summary>
		public static bool Contains (this ISegment segment, ISegment span)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			if (span == null)
				throw new ArgumentNullException ("span");
			return segment.Offset <= span.Offset && span.EndOffset <= segment.EndOffset;
		}

		/// <summary>
		/// Gets whether the offset is within the <paramref name="segment"/>.
		/// </summary>
		public static bool Contains (this ISegment segment, int offset)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			return unchecked((uint)(offset - segment.Offset) < (uint)segment.Length);
		}

		/// <summary>
		/// Determines whether <paramref name="other"/> overlaps this span. Two spans are considered to overlap 
		/// if they have positions in common and neither is empty. Empty spans do not overlap with any 
		/// other span.
		/// </summary>
		public static bool OverlapsWith (this ISegment segment, ISegment other)
		{
			int overlapStart = Math.Max (segment.Offset, other.Offset);
			int overlapEnd = Math.Min (segment.EndOffset, other.EndOffset);
			return overlapStart < overlapEnd;
		}

		public static ISegment AdjustSegment (this ISegment segment, TextChangeEventArgs args)
		{
			if (segment == null)
				throw new ArgumentNullException ("segment");
			if (args.Offset < segment.Offset)
				return new TextSegment (segment.Offset + args.InsertionLength - args.RemovalLength, segment.Length);
			if (args.Offset <= segment.EndOffset)
				return new TextSegment (segment.Offset, segment.Length);
			return segment;
		}

		public static IEnumerable<ISegment> AdjustSegments (this IEnumerable<ISegment> segments, TextChangeEventArgs args)
		{
			if (segments == null)
				throw new ArgumentNullException ("segments");
			foreach (var segment in segments) {
				yield return segment.AdjustSegment (args);
			}
		}
	}
}