OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Utility methods used by more than one library in the package. | 5 /// Utility methods used by more than one library in the package. |
6 library package_config.util; | 6 library package_config.util; |
7 | 7 |
8 import "package:charcode/ascii.dart"; | 8 import "package:charcode/ascii.dart"; |
9 | 9 |
10 // All ASCII characters that are valid in a package name, with space | 10 // All ASCII characters that are valid in a package name, with space |
(...skipping 25 matching lines...) Expand all Loading... |
36 } | 36 } |
37 nonDot += c ^ $dot; | 37 nonDot += c ^ $dot; |
38 } | 38 } |
39 if (nonDot == 0) return string.length; | 39 if (nonDot == 0) return string.length; |
40 return -1; | 40 return -1; |
41 } | 41 } |
42 | 42 |
43 /// Validate that a Uri is a valid package:URI. | 43 /// Validate that a Uri is a valid package:URI. |
44 String checkValidPackageUri(Uri packageUri) { | 44 String checkValidPackageUri(Uri packageUri) { |
45 if (packageUri.scheme != "package") { | 45 if (packageUri.scheme != "package") { |
46 throw new ArgumentError.value(packageUri, "packageUri", | 46 throw new ArgumentError.value( |
47 "Not a package: URI"); | 47 packageUri, "packageUri", "Not a package: URI"); |
48 } | 48 } |
49 if (packageUri.hasAuthority) { | 49 if (packageUri.hasAuthority) { |
50 throw new ArgumentError.value(packageUri, "packageUri", | 50 throw new ArgumentError.value( |
51 "Package URIs must not have a host part"); | 51 packageUri, "packageUri", "Package URIs must not have a host part"); |
52 } | 52 } |
53 if (packageUri.hasQuery) { | 53 if (packageUri.hasQuery) { |
54 // A query makes no sense if resolved to a file: URI. | 54 // A query makes no sense if resolved to a file: URI. |
55 throw new ArgumentError.value(packageUri, "packageUri", | 55 throw new ArgumentError.value( |
56 "Package URIs must not have a query part"); | 56 packageUri, "packageUri", "Package URIs must not have a query part"); |
57 } | 57 } |
58 if (packageUri.hasFragment) { | 58 if (packageUri.hasFragment) { |
59 // We could leave the fragment after the URL when resolving, | 59 // We could leave the fragment after the URL when resolving, |
60 // but it would be odd if "package:foo/foo.dart#1" and | 60 // but it would be odd if "package:foo/foo.dart#1" and |
61 // "package:foo/foo.dart#2" were considered different libraries. | 61 // "package:foo/foo.dart#2" were considered different libraries. |
62 // Keep the syntax open in case we ever get multiple libraries in one file. | 62 // Keep the syntax open in case we ever get multiple libraries in one file. |
63 throw new ArgumentError.value(packageUri, "packageUri", | 63 throw new ArgumentError.value( |
64 "Package URIs must not have a fragment part"); | 64 packageUri, "packageUri", "Package URIs must not have a fragment part"); |
65 } | 65 } |
66 if (packageUri.path.startsWith('/')) { | 66 if (packageUri.path.startsWith('/')) { |
67 throw new ArgumentError.value(packageUri, "packageUri", | 67 throw new ArgumentError.value( |
68 "Package URIs must not start with a '/'"); | 68 packageUri, "packageUri", "Package URIs must not start with a '/'"); |
69 } | 69 } |
70 int firstSlash = packageUri.path.indexOf('/'); | 70 int firstSlash = packageUri.path.indexOf('/'); |
71 if (firstSlash == -1) { | 71 if (firstSlash == -1) { |
72 throw new ArgumentError.value(packageUri, "packageUri", | 72 throw new ArgumentError.value(packageUri, "packageUri", |
73 "Package URIs must start with the package name followed by a '/'"); | 73 "Package URIs must start with the package name followed by a '/'"); |
74 } | 74 } |
75 String packageName = packageUri.path.substring(0, firstSlash); | 75 String packageName = packageUri.path.substring(0, firstSlash); |
76 int badIndex = _findInvalidCharacter(packageName); | 76 int badIndex = _findInvalidCharacter(packageName); |
77 if (badIndex >= 0) { | 77 if (badIndex >= 0) { |
78 if (packageName.isEmpty) { | 78 if (packageName.isEmpty) { |
79 throw new ArgumentError.value(packageUri, "packageUri", | 79 throw new ArgumentError.value( |
80 "Package names mus be non-empty"); | 80 packageUri, "packageUri", "Package names mus be non-empty"); |
81 } | 81 } |
82 if (badIndex == packageName.length) { | 82 if (badIndex == packageName.length) { |
83 throw new ArgumentError.value(packageUri, "packageUri", | 83 throw new ArgumentError.value(packageUri, "packageUri", |
84 "Package names must contain at least one non-'.' character"); | 84 "Package names must contain at least one non-'.' character"); |
85 } | 85 } |
86 assert(badIndex < packageName.length); | 86 assert(badIndex < packageName.length); |
87 int badCharCode = packageName.codeUnitAt(badIndex); | 87 int badCharCode = packageName.codeUnitAt(badIndex); |
88 var badChar = "U+" + badCharCode.toRadixString(16).padLeft(4, '0'); | 88 var badChar = "U+" + badCharCode.toRadixString(16).padLeft(4, '0'); |
89 if (badCharCode >= 0x20 && badCharCode <= 0x7e) { | 89 if (badCharCode >= 0x20 && badCharCode <= 0x7e) { |
90 // Printable character. | 90 // Printable character. |
91 badChar = "'${packageName[badIndex]}' ($badChar)"; | 91 badChar = "'${packageName[badIndex]}' ($badChar)"; |
92 } | 92 } |
93 throw new ArgumentError.value(packageUri, "packageUri", | 93 throw new ArgumentError.value( |
94 "Package names must not contain $badChar"); | 94 packageUri, "packageUri", "Package names must not contain $badChar"); |
95 } | 95 } |
96 return packageName; | 96 return packageName; |
97 } | 97 } |
OLD | NEW |