Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/StringSegment.cs @ 16300

Last change on this file since 16300 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.5 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20
21namespace ICSharpCode.AvalonEdit.Utils
22{
23  /// <summary>
24  /// Represents a string with a segment.
25  /// Similar to System.ArraySegment&lt;T&gt;, but for strings instead of arrays.
26  /// </summary>
27  public struct StringSegment : IEquatable<StringSegment>
28  {
29    readonly string text;
30    readonly int offset;
31    readonly int count;
32   
33    /// <summary>
34    /// Creates a new StringSegment.
35    /// </summary>
36    public StringSegment(string text, int offset, int count)
37    {
38      if (text == null)
39        throw new ArgumentNullException("text");
40      if (offset < 0 || offset > text.Length)
41        throw new ArgumentOutOfRangeException("offset");
42      if (offset + count > text.Length)
43        throw new ArgumentOutOfRangeException("count");
44      this.text = text;
45      this.offset = offset;
46      this.count = count;
47    }
48   
49    /// <summary>
50    /// Creates a new StringSegment.
51    /// </summary>
52    public StringSegment(string text)
53    {
54      if (text == null)
55        throw new ArgumentNullException("text");
56      this.text = text;
57      this.offset = 0;
58      this.count = text.Length;
59    }
60   
61    /// <summary>
62    /// Gets the string used for this segment.
63    /// </summary>
64    public string Text {
65      get { return text; }
66    }
67   
68    /// <summary>
69    /// Gets the start offset of the segment with the text.
70    /// </summary>
71    public int Offset {
72      get { return offset; }
73    }
74   
75    /// <summary>
76    /// Gets the length of the segment.
77    /// </summary>
78    public int Count {
79      get { return count; }
80    }
81   
82    #region Equals and GetHashCode implementation
83    /// <inheritdoc/>
84    public override bool Equals(object obj)
85    {
86      if (obj is StringSegment)
87        return Equals((StringSegment)obj); // use Equals method below
88      else
89        return false;
90    }
91   
92    /// <inheritdoc/>
93    public bool Equals(StringSegment other)
94    {
95      // add comparisions for all members here
96      return object.ReferenceEquals(this.text, other.text) && offset == other.offset && count == other.count;
97    }
98   
99    /// <inheritdoc/>
100    public override int GetHashCode()
101    {
102      return text.GetHashCode() ^ offset ^ count;
103    }
104   
105    /// <summary>
106    /// Equality operator.
107    /// </summary>
108    public static bool operator ==(StringSegment left, StringSegment right)
109    {
110      return left.Equals(right);
111    }
112   
113    /// <summary>
114    /// Inequality operator.
115    /// </summary>
116    public static bool operator !=(StringSegment left, StringSegment right)
117    {
118      return !left.Equals(right);
119    }
120    #endregion
121  }
122}
Note: See TracBrowser for help on using the repository browser.