forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
54 lines (50 loc) · 2.11 KB
/
common.ts
File metadata and controls
54 lines (50 loc) · 2.11 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { nbformat } from '@jupyterlab/coreutils/lib/nbformat';
export function concatMultilineString(str : nbformat.MultilineString) : string {
if (Array.isArray(str)) {
let result = '';
for (let i = 0; i < str.length; i += 1) {
const s = str[i];
if (i < str.length - 1 && !s.endsWith('\n')) {
result = result.concat(`${s}\n`);
} else {
result = result.concat(s);
}
}
return result.trim();
}
return str.toString().trim();
}
export function formatStreamText(str: string) : string {
// Go through the string, looking for \r's that are not followed by \n. This is
// a special case that means replace the string before. This is necessary to
// get an html display of this string to behave correctly.
// Note: According to this:
// https://jsperf.com/javascript-concat-vs-join/2.
// Concat is way faster than array join for building up a string.
let result = '';
let previousLinePos = 0;
for (let i = 0; i < str.length; i += 1) {
if (str[i] === '\r') {
// See if this is a line feed. If so, leave alone. This is goofy windows \r\n
if (i < str.length - 1 && str[i + 1] === '\n') {
// This line is legit, output it and convert to '\n' only.
result += str.substr(previousLinePos, (i - previousLinePos));
result += '\n';
previousLinePos = i + 2;
i += 1;
} else {
// This line should replace the previous one. Skip our \r
previousLinePos = i + 1;
}
} else if (str[i] === '\n') {
// This line is legit, output it. (Single linefeed)
result += str.substr(previousLinePos, (i - previousLinePos) + 1);
previousLinePos = i + 1;
}
}
result += str.substr(previousLinePos, str.length - previousLinePos);
return result;
}