Skip to content

Commit e11ce8b

Browse files
committed
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 9162166 commit e11ce8b

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
@@ -6,6 +6,34 @@ library engine.utilities.dart;
66

77
import 'java_core.dart';
88

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

pkg/analyzer/test/generated/source_factory_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ foo:http://www.google.com
182182
isTrue);
183183
expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')),
184184
isFalse);
185+
// Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126)
186+
expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse);
187+
expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse);
185188
});
186189
});
187190
});

0 commit comments

Comments
 (0)