~ubuntu-branches/ubuntu/raring/monodevelop/raring

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Diff/Edit.cs

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2011-06-29 06:56:25 UTC
  • mfrom: (1.8.1 upstream) (1.3.11 experimental)
  • Revision ID: james.westby@ubuntu.com-20110629065625-7xx19c4vb95j65pl
Tags: 2.5.92+dfsg-1ubuntu1
* Merge from Debian experimental:
 - Dropped patches & changes to debian/control for Moonlight
   + debian/patches/remove_support_for_moonlight.patch,
   + debian/patches/dont_add_moonlight_to_core_addins.patch,
 - Remaining patches:
   + debian/patches/no_appmenu:

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
5
This program and the accompanying materials are made available
 
6
under the terms of the Eclipse Distribution License v1.0 which
 
7
accompanies this distribution, is reproduced below, and is
 
8
available at http://www.eclipse.org/org/documents/edl-v10.php
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
19
- Redistributions in binary form must reproduce the above
 
20
  copyright notice, this list of conditions and the following
 
21
  disclaimer in the documentation and/or other materials provided
 
22
  with the distribution.
 
23
 
 
24
- Neither the name of the Eclipse Foundation, Inc. nor the
 
25
  names of its contributors may be used to endorse or promote
 
26
  products derived from this software without specific prior
 
27
  written permission.
 
28
 
 
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
42
*/
 
43
 
 
44
using NGit.Diff;
 
45
using Sharpen;
 
46
 
 
47
namespace NGit.Diff
 
48
{
 
49
        /// <summary>A modified region detected between two versions of roughly the same content.
 
50
        ///     </summary>
 
51
        /// <remarks>
 
52
        /// A modified region detected between two versions of roughly the same content.
 
53
        /// <p>
 
54
        /// An edit covers the modified region only. It does not cover a common region.
 
55
        /// <p>
 
56
        /// Regions should be specified using 0 based notation, so add 1 to the start and
 
57
        /// end marks for line numbers in a file.
 
58
        /// <p>
 
59
        /// An edit where <code>beginA == endA && beginB &lt; endB</code> is an insert edit,
 
60
        /// that is sequence B inserted the elements in region
 
61
        /// <code>[beginB, endB)</code> at <code>beginA</code>.
 
62
        /// <p>
 
63
        /// An edit where <code>beginA &lt; endA && beginB == endB</code> is a delete edit,
 
64
        /// that is sequence B has removed the elements between
 
65
        /// <code>[beginA, endA)</code>.
 
66
        /// <p>
 
67
        /// An edit where <code>beginA &lt; endA && beginB &lt; endB</code> is a replace edit,
 
68
        /// that is sequence B has replaced the range of elements between
 
69
        /// <code>[beginA, endA)</code> with those found in <code>[beginB, endB)</code>.
 
70
        /// </remarks>
 
71
        public class Edit
 
72
        {
 
73
                /// <summary>Type of edit</summary>
 
74
                public enum Type
 
75
                {
 
76
                        INSERT,
 
77
                        DELETE,
 
78
                        REPLACE,
 
79
                        EMPTY
 
80
                }
 
81
 
 
82
                internal int beginA;
 
83
 
 
84
                internal int endA;
 
85
 
 
86
                internal int beginB;
 
87
 
 
88
                internal int endB;
 
89
 
 
90
                /// <summary>Create a new empty edit.</summary>
 
91
                /// <remarks>Create a new empty edit.</remarks>
 
92
                /// <param name="as">beginA: start and end of region in sequence A; 0 based.</param>
 
93
                /// <param name="bs">beginB: start and end of region in sequence B; 0 based.</param>
 
94
                public Edit(int @as, int bs) : this(@as, @as, bs, bs)
 
95
                {
 
96
                }
 
97
 
 
98
                /// <summary>Create a new edit.</summary>
 
99
                /// <remarks>Create a new edit.</remarks>
 
100
                /// <param name="as">beginA: start of region in sequence A; 0 based.</param>
 
101
                /// <param name="ae">endA: end of region in sequence A; must be &gt;= as.</param>
 
102
                /// <param name="bs">beginB: start of region in sequence B; 0 based.</param>
 
103
                /// <param name="be">endB: end of region in sequence B; must be &gt;= bs.</param>
 
104
                public Edit(int @as, int ae, int bs, int be)
 
105
                {
 
106
                        beginA = @as;
 
107
                        endA = ae;
 
108
                        beginB = bs;
 
109
                        endB = be;
 
110
                }
 
111
 
 
112
                /// <returns>the type of this region</returns>
 
113
                public Edit.Type GetType()
 
114
                {
 
115
                        if (beginA < endA)
 
116
                        {
 
117
                                if (beginB < endB)
 
118
                                {
 
119
                                        return Edit.Type.REPLACE;
 
120
                                }
 
121
                                else
 
122
                                {
 
123
                                        return Edit.Type.DELETE;
 
124
                                }
 
125
                        }
 
126
                        else
 
127
                        {
 
128
                                if (beginB < endB)
 
129
                                {
 
130
                                        return Edit.Type.INSERT;
 
131
                                }
 
132
                                else
 
133
                                {
 
134
                                        return Edit.Type.EMPTY;
 
135
                                }
 
136
                        }
 
137
                }
 
138
 
 
139
                /// <returns>true if the edit is empty (lengths of both a and b is zero).</returns>
 
140
                public bool IsEmpty()
 
141
                {
 
142
                        return beginA == endA && beginB == endB;
 
143
                }
 
144
 
 
145
                /// <returns>start point in sequence A.</returns>
 
146
                public int GetBeginA()
 
147
                {
 
148
                        return beginA;
 
149
                }
 
150
 
 
151
                /// <returns>end point in sequence A.</returns>
 
152
                public int GetEndA()
 
153
                {
 
154
                        return endA;
 
155
                }
 
156
 
 
157
                /// <returns>start point in sequence B.</returns>
 
158
                public int GetBeginB()
 
159
                {
 
160
                        return beginB;
 
161
                }
 
162
 
 
163
                /// <returns>end point in sequence B.</returns>
 
164
                public int GetEndB()
 
165
                {
 
166
                        return endB;
 
167
                }
 
168
 
 
169
                /// <returns>length of the region in A.</returns>
 
170
                public int GetLengthA()
 
171
                {
 
172
                        return endA - beginA;
 
173
                }
 
174
 
 
175
                /// <returns>length of the region in B.</returns>
 
176
                public int GetLengthB()
 
177
                {
 
178
                        return endB - beginB;
 
179
                }
 
180
 
 
181
                /// <summary>Construct a new edit representing the region before cut.</summary>
 
182
                /// <remarks>Construct a new edit representing the region before cut.</remarks>
 
183
                /// <param name="cut">
 
184
                /// the cut point. The beginning A and B points are used as the
 
185
                /// end points of the returned edit.
 
186
                /// </param>
 
187
                /// <returns>
 
188
                /// an edit representing the slice of
 
189
                /// <code>this</code>
 
190
                /// edit that occurs
 
191
                /// before
 
192
                /// <code>cut</code>
 
193
                /// starts.
 
194
                /// </returns>
 
195
                public NGit.Diff.Edit Before(NGit.Diff.Edit cut)
 
196
                {
 
197
                        return new NGit.Diff.Edit(beginA, cut.beginA, beginB, cut.beginB);
 
198
                }
 
199
 
 
200
                /// <summary>Construct a new edit representing the region after cut.</summary>
 
201
                /// <remarks>Construct a new edit representing the region after cut.</remarks>
 
202
                /// <param name="cut">
 
203
                /// the cut point. The ending A and B points are used as the
 
204
                /// starting points of the returned edit.
 
205
                /// </param>
 
206
                /// <returns>
 
207
                /// an edit representing the slice of
 
208
                /// <code>this</code>
 
209
                /// edit that occurs
 
210
                /// after
 
211
                /// <code>cut</code>
 
212
                /// ends.
 
213
                /// </returns>
 
214
                public NGit.Diff.Edit After(NGit.Diff.Edit cut)
 
215
                {
 
216
                        return new NGit.Diff.Edit(cut.endA, endA, cut.endB, endB);
 
217
                }
 
218
 
 
219
                /// <summary>
 
220
                /// Increase
 
221
                /// <see cref="GetEndA()">GetEndA()</see>
 
222
                /// by 1.
 
223
                /// </summary>
 
224
                public virtual void ExtendA()
 
225
                {
 
226
                        endA++;
 
227
                }
 
228
 
 
229
                /// <summary>
 
230
                /// Increase
 
231
                /// <see cref="GetEndB()">GetEndB()</see>
 
232
                /// by 1.
 
233
                /// </summary>
 
234
                public virtual void ExtendB()
 
235
                {
 
236
                        endB++;
 
237
                }
 
238
 
 
239
                /// <summary>Swap A and B, so the edit goes the other direction.</summary>
 
240
                /// <remarks>Swap A and B, so the edit goes the other direction.</remarks>
 
241
                public virtual void Swap()
 
242
                {
 
243
                        int sBegin = beginA;
 
244
                        int sEnd = endA;
 
245
                        beginA = beginB;
 
246
                        endA = endB;
 
247
                        beginB = sBegin;
 
248
                        endB = sEnd;
 
249
                }
 
250
 
 
251
                public override int GetHashCode()
 
252
                {
 
253
                        return beginA ^ endA;
 
254
                }
 
255
 
 
256
                public override bool Equals(object o)
 
257
                {
 
258
                        if (o is NGit.Diff.Edit)
 
259
                        {
 
260
                                NGit.Diff.Edit e = (NGit.Diff.Edit)o;
 
261
                                return this.beginA == e.beginA && this.endA == e.endA && this.beginB == e.beginB 
 
262
                                        && this.endB == e.endB;
 
263
                        }
 
264
                        return false;
 
265
                }
 
266
 
 
267
                public override string ToString()
 
268
                {
 
269
                        Edit.Type t = GetType();
 
270
                        return t + "(" + beginA + "-" + endA + "," + beginB + "-" + endB + ")";
 
271
                }
 
272
        }
 
273
}