-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
modelBuilder.ts
180 lines (154 loc) · 4.8 KB
/
modelBuilder.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IStringStream } from 'vs/platform/files/common/files';
import * as crypto from 'crypto';
import * as strings from 'vs/base/common/strings';
import { TPromise } from 'vs/base/common/winjs.base';
import { CharCode } from 'vs/base/common/charCode';
import { IRawTextSource } from 'vs/editor/common/model/textSource';
const AVOID_SLICED_STRINGS = true;
export interface ModelBuilderResult {
readonly hash: string;
readonly value: IRawTextSource;
}
class ModelLineBasedBuilder {
private hash: crypto.Hash;
private BOM: string;
private lines: string[];
private currLineIndex: number;
constructor() {
this.hash = crypto.createHash('sha1');
this.BOM = '';
this.lines = [];
this.currLineIndex = 0;
}
public acceptLines(lines: string[]): void {
if (this.currLineIndex === 0) {
// Remove the BOM (if present)
if (strings.startsWithUTF8BOM(lines[0])) {
this.BOM = strings.UTF8_BOM_CHARACTER;
lines[0] = lines[0].substr(1);
}
}
for (let i = 0, len = lines.length; i < len; i++) {
let line = lines[i];
if (AVOID_SLICED_STRINGS) {
// See https://bugs.chromium.org/p/v8/issues/detail?id=2869
line = Buffer.from(line).toString();
}
this.lines[this.currLineIndex++] = line;
}
this.hash.update(lines.join('\n') + '\n');
}
public finish(length: number, carriageReturnCnt: number, containsRTL: boolean, isBasicASCII: boolean): ModelBuilderResult {
return {
hash: this.hash.digest('hex'),
value: {
BOM: this.BOM,
lines: this.lines,
length,
containsRTL: containsRTL,
totalCRCount: carriageReturnCnt,
isBasicASCII,
}
};
}
}
export function computeHash(rawText: IRawTextSource): string {
let hash = crypto.createHash('sha1');
for (let i = 0, len = rawText.lines.length; i < len; i++) {
hash.update(rawText.lines[i] + '\n');
}
return hash.digest('hex');
}
export class ModelBuilder {
private leftoverPrevChunk: string;
private leftoverEndsInCR: boolean;
private totalCRCount: number;
private lineBasedBuilder: ModelLineBasedBuilder;
private totalLength: number;
private containsRTL: boolean;
private isBasicASCII: boolean;
public static fromStringStream(stream: IStringStream): TPromise<ModelBuilderResult> {
return new TPromise<ModelBuilderResult>((c, e, p) => {
let done = false;
let builder = new ModelBuilder();
stream.on('data', (chunk) => {
builder.acceptChunk(chunk);
});
stream.on('error', (error) => {
if (!done) {
done = true;
e(error);
}
});
stream.on('end', () => {
if (!done) {
done = true;
c(builder.finish());
}
});
});
}
constructor() {
this.leftoverPrevChunk = '';
this.leftoverEndsInCR = false;
this.totalCRCount = 0;
this.lineBasedBuilder = new ModelLineBasedBuilder();
this.totalLength = 0;
this.containsRTL = false;
this.isBasicASCII = true;
}
private _updateCRCount(chunk: string): void {
// Count how many \r are present in chunk to determine the majority EOL sequence
let chunkCarriageReturnCnt = 0;
let lastCarriageReturnIndex = -1;
while ((lastCarriageReturnIndex = chunk.indexOf('\r', lastCarriageReturnIndex + 1)) !== -1) {
chunkCarriageReturnCnt++;
}
this.totalCRCount += chunkCarriageReturnCnt;
}
public acceptChunk(chunk: string): void {
if (chunk.length === 0) {
return;
}
this.totalLength += chunk.length;
this._updateCRCount(chunk);
if (!this.containsRTL) {
this.containsRTL = strings.containsRTL(chunk);
}
if (this.isBasicASCII) {
this.isBasicASCII = strings.isBasicASCII(chunk);
}
// Avoid dealing with a chunk that ends in \r (push the \r to the next chunk)
if (this.leftoverEndsInCR) {
chunk = '\r' + chunk;
}
if (chunk.charCodeAt(chunk.length - 1) === CharCode.CarriageReturn) {
this.leftoverEndsInCR = true;
chunk = chunk.substr(0, chunk.length - 1);
} else {
this.leftoverEndsInCR = false;
}
let lines = chunk.split(/\r\n|\r|\n/);
if (lines.length === 1) {
// no \r or \n encountered
this.leftoverPrevChunk += lines[0];
return;
}
lines[0] = this.leftoverPrevChunk + lines[0];
this.lineBasedBuilder.acceptLines(lines.slice(0, lines.length - 1));
this.leftoverPrevChunk = lines[lines.length - 1];
}
public finish(): ModelBuilderResult {
let finalLines = [this.leftoverPrevChunk];
if (this.leftoverEndsInCR) {
finalLines.push('');
}
this.lineBasedBuilder.acceptLines(finalLines);
return this.lineBasedBuilder.finish(this.totalLength, this.totalCRCount, this.containsRTL, this.isBasicASCII);
}
}