Skip to content

Commit 50efecb

Browse files
emilhammarstedtstmsujew
authored andcommitted
Normalize and prefix windows paths with Path.separator
- Prefix path with / if on the form of "c:/" into "/c:/" - Normalize windows path-separator paths to use / instead of \\ - Added test section to test normalize windows styled paths with \ Contributed by STMicroelectronics Signed-off-by: Emil HAMMARSTEDT <emil.hammarstedt@st.com>
1 parent e5e6f92 commit 50efecb

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

packages/core/src/common/path.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ describe('Path', () => {
257257
});
258258
}
259259

260+
describe('Normalize path separator', () => {
261+
it('should handle windows styled paths', async () => {
262+
const path = 'C:\\a\\b\\c';
263+
const expected = '/c:/a/b/c';
264+
expect(new Path(path).toString()).eq(expected);
265+
});
266+
267+
it('should prefix drive letter with /', async () => {
268+
const path = 'c:/a/b/c';
269+
const expected = '/c:/a/b/c';
270+
expect(new Path(path).toString()).eq(expected);
271+
});
272+
});
273+
260274
const linuxHome = '/home/test-user';
261275
const windowsHome = '/C:/Users/test-user';
262276

@@ -340,5 +354,6 @@ describe('Path', () => {
340354
const expected = '~/a/b/theia';
341355
expect(Path.untildify(path, '')).eq(expected);
342356
});
357+
343358
});
344359
});

packages/core/src/common/path.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@ export class Path {
5353
} else if (path.length >= 2 && path.charCodeAt(1) === 58 /* ':' */) {
5454
const code = path.charCodeAt(0);
5555
if (code >= 65 /* A */ && code <= 90 /* Z */) {
56-
path = `${String.fromCharCode(code + 32)}:${path.substr(2)}`; // "/c:".length === 3
56+
path = `${String.fromCharCode(code + 32)}:${path.substr(2)}`; // "c:".length === 2
57+
}
58+
if (path.charCodeAt(0) !== 47 /* '/' */) {
59+
path = `${String.fromCharCode(47)}${path}`;
5760
}
5861
}
5962
return path;
6063
}
64+
/**
65+
* Normalize path separator to use Path.separator
66+
* @param Path candidate to normalize
67+
* @returns Normalized string path
68+
*/
69+
static normalizePathSeparator(path: string): string {
70+
return path.split(/[\\]/).join(Path.separator);
71+
}
6172

6273
/**
6374
* Tildify path, replacing `home` with `~` if user's `home` is present at the beginning of the path.
@@ -112,11 +123,12 @@ export class Path {
112123
constructor(
113124
raw: string
114125
) {
126+
raw = Path.normalizePathSeparator(raw);
115127
this.raw = Path.normalizeDrive(raw);
116-
const firstIndex = raw.indexOf(Path.separator);
117-
const lastIndex = raw.lastIndexOf(Path.separator);
128+
const firstIndex = this.raw.indexOf(Path.separator);
129+
const lastIndex = this.raw.lastIndexOf(Path.separator);
118130
this.isAbsolute = firstIndex === 0;
119-
this.base = lastIndex === -1 ? raw : raw.substr(lastIndex + 1);
131+
this.base = lastIndex === -1 ? this.raw : this.raw.substr(lastIndex + 1);
120132
this.isRoot = this.isAbsolute && firstIndex === lastIndex && (!this.base || Path.isDrive(this.base));
121133
this.root = this.computeRoot();
122134

0 commit comments

Comments
 (0)