Skip to content

Commit

Permalink
Handle comparison of empty URIs (sdk/24126).
Browse files Browse the repository at this point in the history
Fixes issue where `.packages` provides a key mapped to an empty value (#24126).

[email protected]

Review URL: https://codereview.chromium.org//1298393004 .
  • Loading branch information
pq authored and whesse committed Aug 20, 2015
1 parent e2e2e64 commit 99c2010
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
52 changes: 28 additions & 24 deletions pkg/analyzer/lib/src/generated/utilities_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ library engine.utilities.dart;

import 'java_core.dart';

/**
* Check whether [uri1] starts with (or 'is prefixed by') [uri2] by checking
* path segments.
*/
bool startsWith(Uri uri1, Uri uri2) {
List<String> uri1Segments = uri1.pathSegments;
List<String> uri2Segments = uri2.pathSegments.toList();
// Punt if empty (https://github.com/dart-lang/sdk/issues/24126)
if (uri2Segments.isEmpty) {
return false;
}
// Trim trailing empty segments ('/foo/' => ['foo', ''])
if (uri2Segments.last == '') {
uri2Segments.removeLast();
}

if (uri2Segments.length > uri1Segments.length) {
return false;
}

for (int i = 0; i < uri2Segments.length; ++i) {
if (uri2Segments[i] != uri1Segments[i]) {
return false;
}
}
return true;
}

/**
* The enumeration `ParameterKind` defines the different kinds of parameters. There are two
* basic kinds of parameters: required and optional. Optional parameters are further divided into
Expand Down Expand Up @@ -38,27 +66,3 @@ class ParameterKind extends Enum<ParameterKind> {
const ParameterKind(String name, int ordinal, this.isOptional)
: super(name, ordinal);
}

/**
* Check whether [uri1] starts with (or 'is prefixed by') [uri2] by checking
* path segments.
*/
bool startsWith(Uri uri1, Uri uri2) {
List<String> uri1Segments = uri1.pathSegments;
List<String> uri2Segments = uri2.pathSegments.toList();
// Trim trailing empty segments ('/foo/' => ['foo', ''])
if (uri2Segments.last == '') {
uri2Segments.removeLast();
}

if (uri2Segments.length > uri1Segments.length) {
return false;
}

for (int i = 0; i < uri2Segments.length; ++i) {
if (uri2Segments[i] != uri1Segments[i]) {
return false;
}
}
return true;
}
3 changes: 3 additions & 0 deletions pkg/analyzer/test/generated/source_factory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ foo:http://www.google.com
isTrue);
expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('/foo/b')),
isFalse);
// Handle odd URIs (https://github.com/dart-lang/sdk/issues/24126)
expect(utils.startsWith(Uri.parse('/foo/bar'), Uri.parse('')), isFalse);
expect(utils.startsWith(Uri.parse(''), Uri.parse('/foo/bar')), isFalse);
});
});
});
Expand Down

0 comments on commit 99c2010

Please sign in to comment.