| 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; | 5 library path.internal_style; |
| 6 | 6 |
| 7 import 'context.dart'; | 7 import 'context.dart'; |
| 8 import 'style.dart'; | 8 import 'style.dart'; |
| 9 | 9 |
| 10 /// The internal interface for the [Style] type. | 10 /// The internal interface for the [Style] type. |
| 11 /// | 11 /// |
| 12 /// Users should be able to pass around instances of [Style] like an enum, but | 12 /// 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 | 13 /// the members that [Context] uses should be hidden from them. Those members |
| 14 /// are defined on this class instead. | 14 /// are defined on this class instead. |
| 15 abstract class InternalStyle extends Style { | 15 abstract class InternalStyle extends Style { |
| 16 /// The path separator for this style. On POSIX, this is `/`. On Windows, | 16 /// The default path separator for this style. |
| 17 /// it's `\`. | 17 /// |
| 18 /// On POSIX, this is `/`. On Windows, it's `\`. |
| 18 String get separator; | 19 String get separator; |
| 19 | 20 |
| 20 /// The [Pattern] that can be used to match a separator for a path in this | 21 /// All valid path separators for this style. |
| 21 /// style. Windows allows both "/" and "\" as path separators even though "\" | 22 /// |
| 22 /// is the canonical one. | 23 /// These are assumed to each be a single character. |
| 23 Pattern get separatorPattern; | 24 List<String> get separators; |
| 24 | 25 |
| 25 /// The [Pattern] that matches path components that need a separator after | 26 /// Returns whether this path component needs a separator after it. |
| 26 /// them. | |
| 27 /// | 27 /// |
| 28 /// Windows and POSIX styles just need separators when the previous component | 28 /// Windows and POSIX styles just need separators when the previous component |
| 29 /// doesn't already end in a separator, but the URL always needs to place a | 29 /// doesn't already end in a separator, but the URL always needs to place a |
| 30 /// separator between the root and the first component, even if the root | 30 /// separator between the root and the first component, even if the root |
| 31 /// already ends in a separator character. For example, to join "file://" and | 31 /// already ends in a separator character. For example, to join "file://" and |
| 32 /// "usr", an additional "/" is needed (making "file:///usr"). | 32 /// "usr", an additional "/" is needed (making "file:///usr"). |
| 33 Pattern get needsSeparatorPattern; | 33 bool needsSeparator(String path); |
| 34 | |
| 35 /// The [Pattern] that can be used to match the root prefix of an absolute | |
| 36 /// path in this style. | |
| 37 Pattern get rootPattern; | |
| 38 | |
| 39 /// The [Pattern] that can be used to match the root prefix of a root-relative | |
| 40 /// path in this style. | |
| 41 /// | |
| 42 /// This can be null to indicate that this style doesn't support root-relative | |
| 43 /// paths. | |
| 44 final Pattern relativeRootPattern = null; | |
| 45 | 34 |
| 46 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, | 35 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, |
| 47 /// returns `null`. | 36 /// returns `null`. |
| 48 String getRoot(String path) { | 37 String getRoot(String path); |
| 49 // TODO(rnystrom): Use firstMatch() when #7080 is fixed. | |
| 50 var matches = rootPattern.allMatches(path); | |
| 51 if (matches.isNotEmpty) return matches.first[0]; | |
| 52 return getRelativeRoot(path); | |
| 53 } | |
| 54 | 38 |
| 55 /// Gets the root prefix of [path] if it's root-relative. | 39 /// Gets the root prefix of [path] if it's root-relative. |
| 56 /// | 40 /// |
| 57 /// If [path] is relative or absolute and not root-relative, returns `null`. | 41 /// If [path] is relative or absolute and not root-relative, returns `null`. |
| 58 String getRelativeRoot(String path) { | 42 String getRelativeRoot(String path); |
| 59 if (relativeRootPattern == null) return null; | |
| 60 // TODO(rnystrom): Use firstMatch() when #7080 is fixed. | |
| 61 var matches = relativeRootPattern.allMatches(path); | |
| 62 if (matches.isEmpty) return null; | |
| 63 return matches.first[0]; | |
| 64 } | |
| 65 | 43 |
| 66 /// Returns the path represented by [uri] in this style. | 44 /// Returns the path represented by [uri] in this style. |
| 67 String pathFromUri(Uri uri); | 45 String pathFromUri(Uri uri); |
| 68 | 46 |
| 69 /// Returns the URI that represents the relative path made of [parts]. | 47 /// Returns the URI that represents the relative path made of [parts]. |
| 70 Uri relativePathToUri(String path) => | 48 Uri relativePathToUri(String path) => |
| 71 new Uri(pathSegments: context.split(path)); | 49 new Uri(pathSegments: context.split(path)); |
| 72 | 50 |
| 73 /// Returns the URI that represents [path], which is assumed to be absolute. | 51 /// Returns the URI that represents [path], which is assumed to be absolute. |
| 74 Uri absolutePathToUri(String path); | 52 Uri absolutePathToUri(String path); |
| 75 } | 53 } |
| OLD | NEW |