20
return characters;
21
}
23
>
24
>
export function stringDiff(original: string, modified: string, pretty: boolean): IDiffChange[] {
25
return new LcsDiff(new StringDiffSequence(original), new StringDiffSequence(modified)).ComputeDiff(pretty).changes;
26
}
28
>
export interface ISequence {
29
>
getElements(): Int32Array | number[] | string[];
30
>
getStrictElement?(index: number): string;
31
>
}
32
>
33
>
export interface IDiffChange {
34
>
/**
35
>
* The position of the first element in the original sequence which
36
>
* this change affects.
37
>
*/
38
>
originalStart: number;
39
>
40
>
/**
41
>
* The number of elements from the original sequence which were
42
>
* affected.
43
>
*/
44
>
originalLength: number;
45
>
46
>
/**
47
>
* The position of the first element in the modified sequence which
48
>
* this change affects.
49
>
*/
50
>
modifiedStart: number;
51
>
52
>
/**
53
>
* The number of elements from the modified sequence which were
54
>
* affected (added).
55
>
*/
56
>
modifiedLength: number;
57
>
}
58
>
59
>
export interface IContinueProcessingPredicate {
60
>
(furthestOriginalIndex: number, matchLengthOfLongest: number): boolean;
61
>
}
62
>
63
>
export interface IDiffResult {
64
>
quitEarly: boolean;
65
>
changes: IDiffChange[];
66
>
}
67
>
68
>
//
69
>
// The code below has been ported from a C# implementation in VS
70
>
//
71
>
72
>
class Debug {
73
>
74
>
public static Assert(condition: boolean, message: string): void {
75
if (!condition) {
76
throw new Error(message);
77
}
78
}
80
>
81
>
class MyArray {
82
>
/**
83
>
* Copies a range of elements from an Array starting at the specified source index and pastes
84
>
* them to another Array starting at the specified destination index. The length and the indexes
85
>
* are specified as 64-bit integers.
86
>
* sourceArray:
87
>
* The Array that contains the data to copy.
88
>
* sourceIndex:
89
>
* A 64-bit integer that represents the index in the sourceArray at which copying begins.
90
>
* destinationArray:
91
>
* The Array that receives the data.
92
>
* destinationIndex:
93
>
* A 64-bit integer that represents the index in the destinationArray at which storing begins.
94
>
* length:
95
>
* A 64-bit integer that represents the number of elements to copy.
96
>
*/
97
>
public static Copy(sourceArray: unknown[], sourceIndex: number, destinationArray: unknown[], destinationIndex: number, length: number) {
98
for (let i = 0; i < length; i++) {
99
destinationArray[destinationIndex + i] = sourceArray[sourceIndex + i];
100
}
101
}
102
>
public static Copy2(sourceArray: Int32Array, sourceIndex: number, destinationArray: Int32Array, destinationIndex: number, length: number) {
diff.ts
103
for (let i = 0; i < length; i++) {
104
destinationArray[destinationIndex + i] = sourceArray[sourceIndex + i];
105
}
106
}
108
>
109
>
//*****************************************************************************
110
>
// LcsDiff.cs
111
>
//
112
>
// An implementation of the difference algorithm described in
113
>
// "An O(ND) Difference Algorithm and its variations" by Eugene W. Myers
114
>
//
115
>
// Copyright (C) 2008 Microsoft Corporation @minifier_do_not_preserve
116
>
//*****************************************************************************
117
>
118
>
// Our total memory usage for storing history is (worst-case):
119
>
// 2 * [(MaxDifferencesHistory + 1) * (MaxDifferencesHistory + 1) - 1] * sizeof(int)
120
>
// 2 * [1448*1448 - 1] * 4 = 16773624 = 16MB
121
>
const enum LocalConstants {
122
>
MaxDifferencesHistory = 1447
123
>
}
124
>
125
>
/**
126
>
* A utility class which helps to create the set of DiffChanges from
127
>
* a difference operation. This class accepts original DiffElements and
128
>
* modified DiffElements that are involved in a particular change. The
129
>
* MarkNextChange() method can be called to mark the separation between
130
>
* distinct changes. At the end, the Changes property can be called to retrieve
131
>
* the constructed changes.
132
>
*/
133
>
class DiffChangeHelper {
134
>
135
>
private m_changes: DiffChange[];
136
>
private m_originalStart: number;
137
>
private m_modifiedStart: number;
138
>
private m_originalCount: number;
139
>
private m_modifiedCount: number;
140
>
141
>
/**
142
>
* Constructs a new DiffChangeHelper for the given DiffSequences.
143
>
*/
144
>
constructor() {
145
this.m_changes = [];
146
this.m_originalStart = Constants.MAX_SAFE_SMALL_INTEGER;