forked from microsoft/vscode-python-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringUtils.ts
More file actions
71 lines (60 loc) · 2.12 KB
/
stringUtils.ts
File metadata and controls
71 lines (60 loc) · 2.12 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { getOSType, OSType } from './platform';
/**
* Replaces all instances of a substring with a new substring.
*/
export function replaceAll(source: string, substr: string, newSubstr: string): string {
if (!source) {
return source;
}
/** Escaping function from the MDN web docs site
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
* Escapes all the following special characters in a string . * + ? ^ $ { } ( ) | \ \\
*/
function escapeRegExp(unescapedStr: string): string {
return unescapedStr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
return source.replace(new RegExp(escapeRegExp(substr), 'g'), newSubstr);
}
/**
* Removes leading and trailing quotes from a string
*/
export function trimQuotes(source: string): string {
if (!source) {
return source;
}
return source.replace(/(^['"])|(['"]$)/g, '');
}
/**
* Appropriately formats a string so it can be used as an argument for a command in a shell.
* E.g. if an argument contains a space, then it will be enclosed within double quotes.
* @param {String} value.
*/
export function toCommandArgumentForPythonExt(source: string): string {
if (!source) {
return source;
}
return (source.indexOf(' ') >= 0 ||
source.indexOf('&') >= 0 ||
source.indexOf('(') >= 0 ||
source.indexOf(')') >= 0) &&
!source.startsWith('"') &&
!source.endsWith('"')
? `"${source}"`
: source.toString();
}
/**
* Appropriately formats a a file path so it can be used as an argument for a command in a shell.
* E.g. if an argument contains a space, then it will be enclosed within double quotes.
*/
export function fileToCommandArgumentForPythonExt(source: string): string {
if (!source) {
return source;
}
let result = toCommandArgumentForPythonExt(source);
if (getOSType() !== OSType.Windows) {
result = result.replace(/\\/g, '/');
}
return result;
}