2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
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
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
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.
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
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.
52
/// Equivalence function for
53
/// <see cref="RawText">RawText</see>
56
public abstract class RawTextComparator : SequenceComparator<RawText>
58
private sealed class _RawTextComparator_56 : RawTextComparator
60
public _RawTextComparator_56()
64
public override bool Equals(RawText a, int ai, RawText b, int bi)
68
int @as = a.lines.Get(ai);
69
int bs = b.lines.Get(bi);
70
int ae = a.lines.Get(ai + 1);
71
int be = b.lines.Get(bi + 1);
72
if (ae - @as != be - bs)
78
if (a.content[@as++] != b.content[bs++])
86
protected internal override int HashRegion(byte[] raw, int ptr, int end)
89
for (; ptr < end; ptr++)
91
hash = ((hash << 5) + hash) + (raw[ptr] & unchecked((int)(0xff)));
97
/// <summary>No special treatment.</summary>
98
/// <remarks>No special treatment.</remarks>
99
public static readonly RawTextComparator DEFAULT = new _RawTextComparator_56();
101
private sealed class _RawTextComparator_87 : RawTextComparator
103
public _RawTextComparator_87()
107
public override bool Equals(RawText a, int ai, RawText b, int bi)
111
int @as = a.lines.Get(ai);
112
int bs = b.lines.Get(bi);
113
int ae = a.lines.Get(ai + 1);
114
int be = b.lines.Get(bi + 1);
115
ae = RawCharUtil.TrimTrailingWhitespace(a.content, @as, ae);
116
be = RawCharUtil.TrimTrailingWhitespace(b.content, bs, be);
117
while (@as < ae && bs < be)
119
byte ac = a.content[@as];
120
byte bc = b.content[bs];
121
while (@as < ae - 1 && RawCharUtil.IsWhitespace(ac))
126
while (bs < be - 1 && RawCharUtil.IsWhitespace(bc))
138
return @as == ae && bs == be;
141
protected internal override int HashRegion(byte[] raw, int ptr, int end)
144
for (; ptr < end; ptr++)
147
if (!RawCharUtil.IsWhitespace(c))
149
hash = ((hash << 5) + hash) + (c & unchecked((int)(0xff)));
156
/// <summary>Ignores all whitespace.</summary>
157
/// <remarks>Ignores all whitespace.</remarks>
158
public static readonly RawTextComparator WS_IGNORE_ALL = new _RawTextComparator_87
161
private sealed class _RawTextComparator_138 : RawTextComparator
163
public _RawTextComparator_138()
167
public override bool Equals(RawText a, int ai, RawText b, int bi)
171
int @as = a.lines.Get(ai);
172
int bs = b.lines.Get(bi);
173
int ae = a.lines.Get(ai + 1);
174
int be = b.lines.Get(bi + 1);
175
@as = RawCharUtil.TrimLeadingWhitespace(a.content, @as, ae);
176
bs = RawCharUtil.TrimLeadingWhitespace(b.content, bs, be);
177
if (ae - @as != be - bs)
183
if (a.content[@as++] != b.content[bs++])
191
protected internal override int HashRegion(byte[] raw, int ptr, int end)
194
ptr = RawCharUtil.TrimLeadingWhitespace(raw, ptr, end);
195
for (; ptr < end; ptr++)
197
hash = ((hash << 5) + hash) + (raw[ptr] & unchecked((int)(0xff)));
203
/// <summary>Ignores leading whitespace.</summary>
204
/// <remarks>Ignores leading whitespace.</remarks>
205
public static readonly RawTextComparator WS_IGNORE_LEADING = new _RawTextComparator_138
208
private sealed class _RawTextComparator_173 : RawTextComparator
210
public _RawTextComparator_173()
214
public override bool Equals(RawText a, int ai, RawText b, int bi)
218
int @as = a.lines.Get(ai);
219
int bs = b.lines.Get(bi);
220
int ae = a.lines.Get(ai + 1);
221
int be = b.lines.Get(bi + 1);
222
ae = RawCharUtil.TrimTrailingWhitespace(a.content, @as, ae);
223
be = RawCharUtil.TrimTrailingWhitespace(b.content, bs, be);
224
if (ae - @as != be - bs)
230
if (a.content[@as++] != b.content[bs++])
238
protected internal override int HashRegion(byte[] raw, int ptr, int end)
241
end = RawCharUtil.TrimTrailingWhitespace(raw, ptr, end);
242
for (; ptr < end; ptr++)
244
hash = ((hash << 5) + hash) + (raw[ptr] & unchecked((int)(0xff)));
250
/// <summary>Ignores trailing whitespace.</summary>
251
/// <remarks>Ignores trailing whitespace.</remarks>
252
public static readonly RawTextComparator WS_IGNORE_TRAILING = new _RawTextComparator_173
255
private sealed class _RawTextComparator_208 : RawTextComparator
257
public _RawTextComparator_208()
261
public override bool Equals(RawText a, int ai, RawText b, int bi)
265
int @as = a.lines.Get(ai);
266
int bs = b.lines.Get(bi);
267
int ae = a.lines.Get(ai + 1);
268
int be = b.lines.Get(bi + 1);
269
ae = RawCharUtil.TrimTrailingWhitespace(a.content, @as, ae);
270
be = RawCharUtil.TrimTrailingWhitespace(b.content, bs, be);
271
while (@as < ae && bs < be)
273
byte ac = a.content[@as];
274
byte bc = b.content[bs];
279
if (RawCharUtil.IsWhitespace(ac))
281
@as = RawCharUtil.TrimLeadingWhitespace(a.content, @as, ae);
287
if (RawCharUtil.IsWhitespace(bc))
289
bs = RawCharUtil.TrimLeadingWhitespace(b.content, bs, be);
296
return @as == ae && bs == be;
299
protected internal override int HashRegion(byte[] raw, int ptr, int end)
302
end = RawCharUtil.TrimTrailingWhitespace(raw, ptr, end);
306
hash = ((hash << 5) + hash) + (c & unchecked((int)(0xff)));
307
if (RawCharUtil.IsWhitespace(c))
309
ptr = RawCharUtil.TrimLeadingWhitespace(raw, ptr, end);
320
/// <summary>Ignores whitespace occurring between non-whitespace characters.</summary>
321
/// <remarks>Ignores whitespace occurring between non-whitespace characters.</remarks>
322
public static readonly RawTextComparator WS_IGNORE_CHANGE = new _RawTextComparator_208
325
public override int Hash(RawText seq, int lno)
327
int begin = seq.lines.Get(lno + 1);
328
int end = seq.lines.Get(lno + 2);
329
return HashRegion(seq.content, begin, end);
332
public override Edit ReduceCommonStartEnd(RawText a, RawText b, Edit e)
334
// This is a faster exact match based form that tries to improve
335
// performance for the common case of the header and trailer of
336
// a text file not changing at all. After this fast path we use
337
// the slower path based on the super class' using equals() to
338
// allow for whitespace ignore modes to still work.
339
if (e.beginA == e.endA || e.beginB == e.endB)
343
byte[] aRaw = a.content;
344
byte[] bRaw = b.content;
345
int aPtr = a.lines.Get(e.beginA + 1);
346
int bPtr = a.lines.Get(e.beginB + 1);
347
int aEnd = a.lines.Get(e.endA + 1);
348
int bEnd = b.lines.Get(e.endB + 1);
349
// This can never happen, but the JIT doesn't know that. If we
350
// define this assertion before the tight while loops below it
351
// should be able to skip the array bound checks on access.
353
if (aPtr < 0 || bPtr < 0 || aEnd > aRaw.Length || bEnd > bRaw.Length)
355
throw new IndexOutOfRangeException();
357
while (aPtr < aEnd && bPtr < bEnd && aRaw[aPtr] == bRaw[bPtr])
362
while (aPtr < aEnd && bPtr < bEnd && aRaw[aEnd - 1] == bRaw[bEnd - 1])
367
e.beginA = FindForwardLine(a.lines, e.beginA, aPtr);
368
e.beginB = FindForwardLine(b.lines, e.beginB, bPtr);
369
e.endA = FindReverseLine(a.lines, e.endA, aEnd);
370
bool partialA = aEnd < a.lines.Get(e.endA + 1);
373
bEnd += a.lines.Get(e.endA + 1) - aEnd;
375
e.endB = FindReverseLine(b.lines, e.endB, bEnd);
376
if (!partialA && bEnd < b.lines.Get(e.endB + 1))
380
return base.ReduceCommonStartEnd(a, b, e);
383
private static int FindForwardLine(IntList lines, int idx, int ptr)
385
int end = lines.Size() - 2;
386
while (idx < end && lines.Get(idx + 2) <= ptr)
393
private static int FindReverseLine(IntList lines, int idx, int ptr)
395
while (0 < idx && ptr <= lines.Get(idx))
402
/// <summary>Compute a hash code for a region.</summary>
403
/// <remarks>Compute a hash code for a region.</remarks>
404
/// <param name="raw">the raw file content.</param>
405
/// <param name="ptr">first byte of the region to hash.</param>
406
/// <param name="end">1 past the last byte of the region.</param>
407
/// <returns>hash code for the region <code>[ptr, end)</code> of raw.</returns>
408
protected internal abstract int HashRegion(byte[] raw, int ptr, int end);