OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library path.internal_style; | |
6 | |
7 import 'context.dart'; | 5 import 'context.dart'; |
8 import 'style.dart'; | 6 import 'style.dart'; |
9 | 7 |
10 /// The internal interface for the [Style] type. | 8 /// The internal interface for the [Style] type. |
11 /// | 9 /// |
12 /// Users should be able to pass around instances of [Style] like an enum, but | 10 /// Users should be able to pass around instances of [Style] like an enum, but |
13 /// the members that [Context] uses should be hidden from them. Those members | 11 /// the members that [Context] uses should be hidden from them. Those members |
14 /// are defined on this class instead. | 12 /// are defined on this class instead. |
15 abstract class InternalStyle extends Style { | 13 abstract class InternalStyle extends Style { |
16 /// The default path separator for this style. | 14 /// The default path separator for this style. |
(...skipping 11 matching lines...) Expand all Loading... |
28 /// | 26 /// |
29 /// Windows and POSIX styles just need separators when the previous component | 27 /// Windows and POSIX styles just need separators when the previous component |
30 /// doesn't already end in a separator, but the URL always needs to place a | 28 /// doesn't already end in a separator, but the URL always needs to place a |
31 /// separator between the root and the first component, even if the root | 29 /// separator between the root and the first component, even if the root |
32 /// already ends in a separator character. For example, to join "file://" and | 30 /// already ends in a separator character. For example, to join "file://" and |
33 /// "usr", an additional "/" is needed (making "file:///usr"). | 31 /// "usr", an additional "/" is needed (making "file:///usr"). |
34 bool needsSeparator(String path); | 32 bool needsSeparator(String path); |
35 | 33 |
36 /// Returns the number of characters of the root part. | 34 /// Returns the number of characters of the root part. |
37 /// | 35 /// |
38 /// Returns 0 if the path is relative. | 36 /// Returns 0 if the path is relative and 1 if the path is root-relative. |
39 /// | 37 /// |
40 /// If the path is root-relative, the root length is 1. | 38 /// If [withDrive] is `true`, this should include the drive letter for `file:` |
41 int rootLength(String path); | 39 /// URLs. Non-URL styles may ignore the parameter. |
| 40 int rootLength(String path, {bool withDrive: false}); |
42 | 41 |
43 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, | 42 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, |
44 /// returns `null`. | 43 /// returns `null`. |
45 String getRoot(String path) { | 44 String getRoot(String path) { |
46 var length = rootLength(path); | 45 var length = rootLength(path); |
47 if (length > 0) return path.substring(0, length); | 46 if (length > 0) return path.substring(0, length); |
48 return isRootRelative(path) ? path[0] : null; | 47 return isRootRelative(path) ? path[0] : null; |
49 } | 48 } |
50 | 49 |
51 /// Returns whether [path] is root-relative. | 50 /// Returns whether [path] is root-relative. |
52 /// | 51 /// |
53 /// If [path] is relative or absolute and not root-relative, returns `false`. | 52 /// If [path] is relative or absolute and not root-relative, returns `false`. |
54 bool isRootRelative(String path); | 53 bool isRootRelative(String path); |
55 | 54 |
56 /// Returns the path represented by [uri] in this style. | 55 /// Returns the path represented by [uri] in this style. |
57 String pathFromUri(Uri uri); | 56 String pathFromUri(Uri uri); |
58 | 57 |
59 /// Returns the URI that represents the relative path made of [parts]. | 58 /// Returns the URI that represents the relative path made of [parts]. |
60 Uri relativePathToUri(String path) { | 59 Uri relativePathToUri(String path) { |
61 var segments = context.split(path); | 60 var segments = context.split(path); |
62 | 61 |
63 // Ensure that a trailing slash in the path produces a trailing slash in the | 62 // Ensure that a trailing slash in the path produces a trailing slash in the |
64 // URL. | 63 // URL. |
65 if (isSeparator(path.codeUnitAt(path.length - 1))) segments.add(''); | 64 if (isSeparator(path.codeUnitAt(path.length - 1))) segments.add(''); |
66 return new Uri(pathSegments: segments); | 65 return new Uri(pathSegments: segments); |
67 } | 66 } |
68 | 67 |
69 /// Returns the URI that represents [path], which is assumed to be absolute. | 68 /// Returns the URI that represents [path], which is assumed to be absolute. |
70 Uri absolutePathToUri(String path); | 69 Uri absolutePathToUri(String path); |
| 70 |
| 71 /// Returns whether [codeUnit1] and [codeUnit2] are considered equivalent for |
| 72 /// this style. |
| 73 bool codeUnitsEqual(int codeUnit1, int codeUnit2) => codeUnit1 == codeUnit2; |
| 74 |
| 75 /// Returns whether [path1] and [path2] are equivalent. |
| 76 /// |
| 77 /// This only needs to handle character-by-character comparison; it can assume |
| 78 /// the paths are normalized and contain no `..` components. |
| 79 bool pathsEqual(String path1, String path2) => path1 == path2; |
| 80 |
| 81 int canonicalizeCodeUnit(int codeUnit) => codeUnit; |
| 82 |
| 83 String canonicalizePart(String part) => part; |
71 } | 84 } |
OLD | NEW |