Skip to content

Commit e8e6b13

Browse files
committed
add uri compare, microsoft#93368
1 parent 815d94e commit e8e6b13

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/vs/base/common/resources.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as extpath from 'vs/base/common/extpath';
77
import * as paths from 'vs/base/common/path';
88
import { URI, uriToFsPath } from 'vs/base/common/uri';
9-
import { equalsIgnoreCase } from 'vs/base/common/strings';
9+
import { equalsIgnoreCase, compare as strCompare, compareIgnoreCase } from 'vs/base/common/strings';
1010
import { Schemas } from 'vs/base/common/network';
1111
import { isLinux, isWindows } from 'vs/base/common/platform';
1212
import { CharCode } from 'vs/base/common/charCode';
@@ -60,6 +60,28 @@ export function isEqual(first: URI | undefined, second: URI | undefined, caseIns
6060
return (p1 === p2 || caseInsensitivePath && equalsIgnoreCase(p1, p2)) && first.query === second.query && (ignoreFragment || first.fragment === second.fragment);
6161
}
6262

63+
export function compare(uri1: URI, uri2: URI, caseInsensitivePath: boolean = _hasToIgnoreCase(uri1), ignoreFragment: boolean = false): number {
64+
// scheme
65+
let ret = strCompare(uri1.scheme, uri2.scheme);
66+
if (ret === 0) {
67+
// authority
68+
ret = compareIgnoreCase(uri1.authority, uri2.authority);
69+
if (ret === 0) {
70+
// path
71+
ret = caseInsensitivePath ? compareIgnoreCase(uri1.path, uri2.path) : strCompare(uri1.path, uri2.path);
72+
// query
73+
if (ret === 0) {
74+
ret = strCompare(uri1.query, uri2.query);
75+
// fragment
76+
if (ret === 0 && !ignoreFragment) {
77+
ret = strCompare(uri1.fragment, uri2.fragment);
78+
}
79+
}
80+
}
81+
}
82+
return ret;
83+
}
84+
6385
/**
6486
* Tests whether a `candidate` URI is a parent or equal of a given `base` URI.
6587
*

src/vs/base/test/common/resources.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
import * as assert from 'assert';
6-
import { dirname, basename, distinctParents, joinPath, isEqual, isEqualOrParent, normalizePath, isAbsolutePath, relativePath, removeTrailingPathSeparator, hasTrailingPathSeparator, resolvePath, addTrailingPathSeparator, getComparisonKey } from 'vs/base/common/resources';
6+
import { dirname, basename, distinctParents, joinPath, isEqual, isEqualOrParent, normalizePath, isAbsolutePath, relativePath, removeTrailingPathSeparator, hasTrailingPathSeparator, resolvePath, addTrailingPathSeparator, getComparisonKey, compare } from 'vs/base/common/resources';
77
import { URI } from 'vs/base/common/uri';
88
import { isWindows } from 'vs/base/common/platform';
99
import { toSlashes } from 'vs/base/common/extpath';
@@ -348,6 +348,7 @@ suite('Resources', () => {
348348

349349
function assertIsEqual(u1: URI, u2: URI, ignoreCase: boolean | undefined, expected: boolean) {
350350
assert.equal(isEqual(u1, u2, ignoreCase), expected, `${u1.toString()}${expected ? '===' : '!=='}${u2.toString()}`);
351+
assert.equal(compare(u1, u2, ignoreCase) === 0, expected);
351352
assert.equal(getComparisonKey(u1, ignoreCase) === getComparisonKey(u2, ignoreCase), expected, `comparison keys ${u1.toString()}, ${u2.toString()}`);
352353
assert.equal(isEqualOrParent(u1, u2, ignoreCase), expected, `isEqualOrParent ${u1.toString()}, ${u2.toString()}`);
353354
}

0 commit comments

Comments
 (0)