Line data Source code
1 : // Copyright (c) 2013, 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 :
5 : import 'internal_style.dart';
6 : import 'style.dart';
7 :
8 : class ParsedPath {
9 : /// The [InternalStyle] that was used to parse this path.
10 : InternalStyle style;
11 :
12 : /// The absolute root portion of the path, or `null` if the path is relative.
13 : /// On POSIX systems, this will be `null` or "/". On Windows, it can be
14 : /// `null`, "//" for a UNC path, or something like "C:\" for paths with drive
15 : /// letters.
16 : String root;
17 :
18 : /// Whether this path is root-relative.
19 : ///
20 : /// See [Context.isRootRelative].
21 : bool isRootRelative;
22 :
23 : /// The path-separated parts of the path. All but the last will be
24 : /// directories.
25 : List<String> parts;
26 :
27 : /// The path separators preceding each part.
28 : ///
29 : /// The first one will be an empty string unless the root requires a separator
30 : /// between it and the path. The last one will be an empty string unless the
31 : /// path ends with a trailing separator.
32 : List<String> separators;
33 :
34 : /// The file extension of the last non-empty part, or "" if it doesn't have
35 : /// one.
36 0 : String get extension => _splitExtension()[1];
37 :
38 : /// `true` if this is an absolute path.
39 5 : bool get isAbsolute => root != null;
40 :
41 : factory ParsedPath.parse(String path, InternalStyle style) {
42 : // Remove the root prefix, if any.
43 5 : var root = style.getRoot(path);
44 5 : var isRootRelative = style.isRootRelative(path);
45 10 : if (root != null) path = path.substring(root.length);
46 :
47 : // Split the parts on path separators.
48 5 : var parts = <String>[];
49 5 : var separators = <String>[];
50 :
51 : var start = 0;
52 :
53 15 : if (path.isNotEmpty && style.isSeparator(path.codeUnitAt(0))) {
54 0 : separators.add(path[0]);
55 : start = 1;
56 : } else {
57 5 : separators.add('');
58 : }
59 :
60 15 : for (var i = start; i < path.length; i++) {
61 10 : if (style.isSeparator(path.codeUnitAt(i))) {
62 10 : parts.add(path.substring(start, i));
63 10 : separators.add(path[i]);
64 5 : start = i + 1;
65 : }
66 : }
67 :
68 : // Add the final part, if any.
69 10 : if (start < path.length) {
70 10 : parts.add(path.substring(start));
71 5 : separators.add('');
72 : }
73 :
74 5 : return new ParsedPath._(style, root, isRootRelative, parts, separators);
75 : }
76 :
77 : ParsedPath._(
78 5 : this.style, this.root, this.isRootRelative, this.parts, this.separators);
79 :
80 : String get basename {
81 0 : var copy = this.clone();
82 0 : copy.removeTrailingSeparators();
83 0 : if (copy.parts.isEmpty) return root == null ? '' : root;
84 0 : return copy.parts.last;
85 : }
86 :
87 0 : String get basenameWithoutExtension => _splitExtension()[0];
88 :
89 : bool get hasTrailingSeparator =>
90 0 : !parts.isEmpty && (parts.last == '' || separators.last != '');
91 :
92 : void removeTrailingSeparators() {
93 25 : while (!parts.isEmpty && parts.last == '') {
94 0 : parts.removeLast();
95 0 : separators.removeLast();
96 : }
97 40 : if (separators.length > 0) separators[separators.length - 1] = '';
98 : }
99 :
100 : void normalize({bool canonicalize: false}) {
101 : // Handle '.', '..', and empty parts.
102 : var leadingDoubles = 0;
103 5 : var newParts = <String>[];
104 10 : for (var part in parts) {
105 10 : if (part == '.' || part == '') {
106 : // Do nothing. Ignore it.
107 5 : } else if (part == '..') {
108 : // Pop the last part off.
109 0 : if (newParts.length > 0) {
110 0 : newParts.removeLast();
111 : } else {
112 : // Backed out past the beginning, so preserve the "..".
113 0 : leadingDoubles++;
114 : }
115 : } else {
116 5 : newParts.add(canonicalize ? style.canonicalizePart(part) : part);
117 : }
118 : }
119 :
120 : // A relative path can back out from the start directory.
121 5 : if (!isAbsolute) {
122 0 : newParts.insertAll(0, new List.filled(leadingDoubles, '..'));
123 : }
124 :
125 : // If we collapsed down to nothing, do ".".
126 10 : if (newParts.length == 0 && !isAbsolute) {
127 0 : newParts.add('.');
128 : }
129 :
130 : // Canonicalize separators.
131 5 : var newSeparators = new List<String>.generate(
132 15 : newParts.length, (_) => style.separator, growable: true);
133 10 : newSeparators.insert(0, isAbsolute &&
134 10 : newParts.length > 0 &&
135 15 : style.needsSeparator(root) ? style.separator : '');
136 :
137 5 : parts = newParts;
138 5 : separators = newSeparators;
139 :
140 : // Normalize the Windows root if needed.
141 20 : if (root != null && style == Style.windows) {
142 0 : if (canonicalize) root = root.toLowerCase();
143 0 : root = root.replaceAll('/', '\\');
144 : }
145 5 : removeTrailingSeparators();
146 : }
147 :
148 : String toString() {
149 5 : var builder = new StringBuffer();
150 15 : if (root != null) builder.write(root);
151 20 : for (var i = 0; i < parts.length; i++) {
152 15 : builder.write(separators[i]);
153 15 : builder.write(parts[i]);
154 : }
155 15 : builder.write(separators.last);
156 :
157 5 : return builder.toString();
158 : }
159 :
160 : /// Splits the last non-empty part of the path into a `[basename, extension`]
161 : /// pair.
162 : ///
163 : /// Returns a two-element list. The first is the name of the file without any
164 : /// extension. The second is the extension or "" if it has none.
165 : List<String> _splitExtension() {
166 0 : var file = parts.lastWhere((p) => p != '', orElse: () => null);
167 :
168 0 : if (file == null) return ['', ''];
169 0 : if (file == '..') return ['..', ''];
170 :
171 0 : var lastDot = file.lastIndexOf('.');
172 :
173 : // If there is no dot, or it's the first character, like '.bashrc', it
174 : // doesn't count.
175 0 : if (lastDot <= 0) return [file, ''];
176 :
177 0 : return [file.substring(0, lastDot), file.substring(lastDot)];
178 : }
179 :
180 0 : ParsedPath clone() => new ParsedPath._(style, root, isRootRelative,
181 0 : new List.from(parts), new List.from(separators));
182 : }
|