Skip to content

Commit 99c2010

Browse files
pqwhesse
authored andcommitted
Handle comparison of empty URIs (sdk/24126).
Fixes issue where `.packages` provides a key mapped to an empty value (#24126). [email protected] Review URL: https://codereview.chromium.org//1298393004 .
1 parent e2e2e64 commit 99c2010

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

pkg/analyzer/lib/src/generated/utilities_dart.dart

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ library engine.utilities.dart;
99

1010
import 'java_core.dart';
1111

12+
/**
13+
* Check whether [uri1] starts with (or 'is prefixed by') [uri2] by checking
14+
* path segments.
15+
*/
16+
bool startsWith(Uri uri1, Uri uri2) {
17+
List<String> uri1Segments = uri1.pathSegments;
18+
List<String> uri2Segments = uri2.pathSegments.toList();
19+
// Punt if empty (https://github.com/dart-lang/sdk/issues/24126)
20+
if (uri2Segments.isEmpty) {
21+
return false;
22+
}
23+
// Trim trailing empty segments ('/foo/' => ['foo', ''])
24+
if (uri2Segments.last == '') {
25+
uri2Segments.removeLast();
26+
}
27+
28+
if (uri2Segments.length > uri1Segments.length) {
29+
return false;
30+
}
31+
32+
for (int i = 0; i < uri2Segments.length; ++i) {
33+
if (uri2Segments[i] != uri1Segments[i]) {
34+
return false;
35+
}
36+
}
37+
return true;
38+
}
39+
1240
/**
1341
* The enumeration `ParameterKind` defines the different kinds of parameters. There are two
1442
* basic kinds of parameters: required and optional. Optional parameters are further divided into
@@ -38,27 +66,3 @@ class ParameterKind extends Enum<ParameterKind> {
3866
const ParameterKind(String name, int ordinal, this.isOptional)
3967
: super(name, ordinal);
4068
}
41-
42-
/**
43-
* Check whether [uri1] starts with (or 'is prefixed by') [uri2] by checking
44-
* path segments.
45-
*/
46-
bool startsWith(Uri uri1, Uri uri2) {
47-
List<String> uri1Segments = uri1.pathSegments;
48-
List<String> uri2Segments = uri2.pathSegments.toList();
49-
// Trim trailing empty segments ('/foo/' => ['foo', ''])
50-
if (uri2Segments.last == '') {
51-
uri2Segments.removeLast();
52-
}
53-
54-
if (uri2Segments.length > uri1Segments.length) {
55-
return false;
56-
}
57-
58-
for (int i = 0; i < uri2Segments.length; ++i) {
59-
if (uri2Segments[i] != uri1Segments[i]) {
60-
return false;
61-
}
62-
}
63-
return true;
64-
}

pkg/analyzer/test/generated/source_factory_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ foo:http://www.google.com
171171
isTrue);
172172
expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')),
173173
isFalse);
174+
// Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126)
175+
expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse);
176+
expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse);
174177
});
175178
});
176179
});

0 commit comments

Comments
 (0)