Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 library package_config._util; | |
|
pquitslund
2015/05/21 18:14:23
Not sure if this is intentional. Should it be pac
Lasse Reichstein Nielsen
2015/05/21 18:30:22
Not sure what I was thinking. It should just be .u
| |
| 5 | |
| 6 import "package:charcode/ascii.dart"; | |
| 7 | |
| 8 /// Tests whether something is a valid Dart identifier/package name. | |
| 9 bool isIdentifier(String string) { | |
| 10 if (string.isEmpty) return false; | |
| 11 int firstChar = string.codeUnitAt(0); | |
| 12 int firstCharLower = firstChar |= 0x20; | |
| 13 if (firstCharLower < $a || firstCharLower > $z) { | |
| 14 if (firstChar != $_ && firstChar != $$) return false; | |
| 15 } | |
| 16 for (int i = 1; i < string.length; i++) { | |
| 17 int char = string.codeUnitAt(i); | |
| 18 int charLower = char | 0x20; | |
| 19 if (charLower < $a || charLower > $z) { // Letters. | |
| 20 if ((char ^ 0x30) <= 9) continue; // Digits. | |
| 21 if (char == $_ || char == $$) continue; // $ and _ | |
| 22 if (firstChar != $_ && firstChar != $$) return false; | |
| 23 } | |
| 24 } | |
| 25 return true; | |
| 26 } | |
| 27 | |
| 28 | |
| 29 /// Validate that a Uri is a valid package:URI. | |
| 30 String checkValidPackageUri(Uri packageUri) { | |
| 31 if (packageUri.scheme != "package") { | |
| 32 throw new ArgumentError.value(packageUri, "packageUri", | |
| 33 "Not a package: URI"); | |
| 34 } | |
| 35 if (packageUri.hasAuthority) { | |
| 36 throw new ArgumentError.value(packageUri, "packageUri", | |
| 37 "Package URIs must not have a host part"); | |
| 38 } | |
| 39 if (packageUri.hasQuery) { | |
| 40 // A query makes no sense if resolved to a file: URI. | |
| 41 throw new ArgumentError.value(packageUri, "packageUri", | |
| 42 "Package URIs must not have a query part"); | |
| 43 } | |
| 44 if (packageUri.hasFragment) { | |
| 45 // We could leave the fragment after the URL when resolving, | |
| 46 // but it would be odd if "package:foo/foo.dart#1" and | |
| 47 // "package:foo/foo.dart#2" were considered different libraries. | |
| 48 // Keep the syntax open in case we ever get multiple libraries in one file. | |
| 49 throw new ArgumentError.value(packageUri, "packageUri", | |
| 50 "Package URIs must not have a fragment part"); | |
| 51 } | |
| 52 if (packageUri.path.startsWith('/')) { | |
| 53 throw new ArgumentError.value(packageUri, "packageUri", | |
| 54 "Package URIs must not start with a '/'"); | |
| 55 } | |
| 56 int firstSlash = packageUri.path.indexOf('/'); | |
| 57 if (firstSlash == -1) { | |
| 58 throw new ArgumentError.value(packageUri, "packageUri", | |
| 59 "Package URIs must start with the package name followed by a '/'"); | |
| 60 } | |
| 61 String packageName = packageUri.path.substring(0, firstSlash); | |
| 62 if (!isIdentifier(packageName)) { | |
| 63 throw new ArgumentError.value(packageUri, "packageUri", | |
| 64 "Package names must be valid identifiers"); | |
| 65 } | |
| 66 return packageName; | |
| 67 } | |
| OLD | NEW |