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
|
var fs = require('fs');
var child_process = require('child_process');
function cleanAndGetIndexes(text) {
var addRules = {
name: "add",
openTag: "{+",
replaceOpenTag: "",
closeTag: "+}",
replaceCloseTag: "",
}
var delRules = {
name: "del",
openTag: "[-",
replaceOpenTag: "",
closeTag: "-]",
replaceCloseTag: "",
}
var rules = [addRules, delRules];
TagTypeEnum = {
OPENING : 1,
CLOSING : 2
}
function tagLookup(rules, text, pos) {
var seen = false;
var type = null;
var rule = null;
for (var i = 0; i<rules.length; i++) {
var candidateTag = text.slice(pos, pos+rules[i].openTag.length);
// console.log("Comparing "+candidateTag+" with "+rules[i].openTag);
if (rules[i].openTag == candidateTag) {
seen = true;
type = TagTypeEnum.OPENING;
rule = i;
break;
}
var candidateTag = text.slice(pos, pos+rules[i].closeTag.length);
// console.log("Comparing "+candidateTag+" with "+rules[i].closeTag);
if (rules[i].closeTag == candidateTag) {
seen = true;
type = TagTypeEnum.CLOSING;
rule = i;
break;
}
}
return {seen: seen,
rule: rule,
type: type}
}
var finalText = "";
var posInFinalText = 0; // character that is going to be treated
// The position in the original text:
var posInText = 0; // character that is going to be treated
StateEnum = {
OUTSIDE_TAG : 1,
INSIDE_TAG : 2
}
var state = StateEnum.OUTSIDE_TAG;
var zones = [[],[]];
var currentTextBeginPos = 0;
var currentTextEndPos = null;
var currentTagBeginPos = null;
var currentTagEndPos = null;
var currentTag = null;
function forward() {
posInFinalText = posInFinalText + 1;
posInText = posInText + 1;
}
function seenOpeningTag() {
memorizeText();
currentTagBeginPos = posInFinalText;
finalText = finalText.concat(rules[currentTag].replaceOpenTag);
posInFinalText = posInFinalText + rules[currentTag].replaceOpenTag.length;
posInText = posInText + rules[currentTag].openTag.length;
currentTextBeginPos = posInText;
}
function seenClosingTag() {
memorizeText();
finalText = finalText.concat(rules[currentTag].replaceCloseTag);
posInFinalText = posInFinalText + rules[currentTag].replaceCloseTag.length;
posInText = posInText + rules[currentTag].closeTag.length;
currentTagEndPos = posInFinalText - 1;
zones[currentTag].push({begin: currentTagBeginPos, end: currentTagEndPos});
currentTextBeginPos = posInText;
}
function memorizeText() {
currentTextEndPos = posInText - 1;
if (currentTextEndPos >= currentTextBeginPos) {
finalText = finalText.concat(text.slice(currentTextBeginPos,currentTextEndPos+1));
}
}
function end() {
memorizeText();
}
function log_error(string) {
console.log(string);
}
while (posInText < text.length) {
var tag = tagLookup(rules, text, posInText);
if(tag.seen && tag.type == TagTypeEnum.OPENING) {
if (state != StateEnum.OUTSIDE_TAG) {
log_error("Opening tag while not outside tag (tags cannot be nested)");
return null;
}
currentTag = tag.rule;
seenOpeningTag();
state = StateEnum.INSIDE_TAG;
} else if(tag.seen && tag.type == TagTypeEnum.CLOSING) {
if (state != StateEnum.INSIDE_TAG) {
log_error("Closing tag while not inside tag.");
return null;
}
if (currentTag != tag.rule) {
log_error("Closing tag, but not of the same type as previously opened.");
return null;
}
seenClosingTag();
state = StateEnum.OUTSIDE_TAG;
} else {
forward();
}
}
end();
console.log(JSON.stringify(finalText)+JSON.stringify(zones));
return {text: finalText, zones: zones};
}
function diffHandler(req, res, next) {
// console.log("req: "+JSON.stringify(JSON.decycle(req)));
// console.log("");
// console.log("res: "+JSON.stringify(JSON.decycle(res)));
// console.log("");
// console.log("next: "+JSON.stringify(JSON.decycle(next)));
var before = req.body.before;
var after = req.body.after;
if (before === undefined) {
console.log("Warning : Bad request : wrong \"before\"");
//return next(new Error("Bad request : wrong \"before\""));
}
if (after === undefined) {
console.log("Warning : Bad request : wrong \"after\"");
//return next(new Error("Bad request : wrong \"after\""));
}
//console.log("Before: ");
//console.log(before);
//console.log("After: ");
//console.log(after);
// TODO : make async the two creation of temp files + call to wdiff
var before_temp_file = "/tmp/gcc-explorer-before"
fs.writeFileSync(before_temp_file, before);
var after_temp_file = "/tmp/gcc-explorer-after"
fs.writeFileSync(after_temp_file, after);
var wdiff_exe = "/work1/gdevillers/compiler-explorer/external/wdiff-1.2.2/src/wdiff";
var maxSize = 100000;
var wdiffResult = child_process.spawnSync(
"/work1/gdevillers/compiler-explorer/external/wdiff-1.2.2/src/wdiff",
["/tmp/gcc-explorer-before", "/tmp/gcc-explorer-after"],
{maxBuffer: 100000});
res.set('Content-Type', 'application/json');
var cleaned = cleanAndGetIndexes(wdiffResult.stdout.toString());
if (cleaned == null) {
res.end(JSON.stringify({
computedDiff: "Failed to clean the diff",
zones: null
}));
} else {
res.end(JSON.stringify({
computedDiff: cleaned.text+
"\n//// for reference: ////\n"+ // for debug only
wdiffResult.stdout.toString(),
zones: cleaned.zones
//computedDiff: "aaa\nbbb[-ccc-]\n[-ddd-]eee\n[-fff-]\nsafe"
//computedDiff: "[-aaa-]"
//computedDiff: "aaa"
//computedDiff: "aa[--]a"
//computedDiff: "aa[-b-]"
}));
}
}
module.exports = {
diffHandler: diffHandler,
};
|