Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: runtime/bin/path.dart

Issue 10417053: Add Path class to dart:io, and add unit tests for it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix 2 bugs. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/io_sources.gypi ('k') | runtime/bin/path_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 /**
6 * A Path, which is a String interpreted as a sequence of path segments,
7 * which are strings, separated by forward slashes.
8 * Paths are immutable wrappers of a String, that offer member functions for
9 * useful path manipulations and queries. Joining of paths and normalization
10 * interpret '.' and '..' in the usual way.
11 */
12 interface Path default _Path {
13 /**
14 * Creates a Path from the String [source]. [source] is used as-is, so if
15 * the string does not consist of segments separated by forward slashes, the
16 * behavior may not be as expected. Paths are immutable, and constant
17 * Path objects may be constructed from constant Strings.
18 */
19 const Path(String source);
20
21 /**
22 * Creates a Path from a String that uses the native filesystem's conventions.
23 * On Windows, this converts '\' to '/', and adds a '/' before a drive letter.
24 * A path starting with '/c:/' (or any other character instead of 'c') is
25 * treated specially. Backwards links ('..') cannot cancel the drive letter.
26 */
27 Path.fromNative(String source);
28
29 /**
30 * Is this path the empty string?
31 */
32 bool get isEmpty();
33
34 /**
35 * Is this path an absolute path, beginning with a path separator?
36 */
37 bool get isAbsolute();
38
39 /**
40 * Does this path end with a path separator?
41 */
42 bool get hasTrailingSeparator();
43
44 /**
45 * Does this path contain no consecutive path separators, no segments that
46 * are '.' unless the path is exactly '.', and segments that are '..' only
47 * as the leading segments on a relative path?
48 */
49 bool get isCanonical();
50
51 /**
52 * Make a path canonical by dropping segments that are '.', cancelling
53 * segments that are '..' with preceding segments, if possible,
54 * and combining consecutive path separators. Leading '..' segments
55 * are kept on relative paths, and dropped from absolute paths.
56 */
57 Path canonicalize();
58
59 /**
60 * Joins the relative path [further] to this path. Canonicalizes the
61 * resulting joined path using [canonicalize],
62 * interpreting '.' and '..' as directory traversal commands, and removing
63 * consecutive path separators.
64 *
65 * If [further] is an absolute path, an IllegalArgument exception is thrown.
66 *
67 * Examples:
68 * `new Path('/a/b/c').join(new Path('d/e'))` returns the Path object
69 * containing `'a/b/c/d/e'`.
70 *
71 * `new Path('a/b/../c/').join(new Path('d/./e//')` returns the Path
72 * containing `'a/c/d/e/'`.
73 *
74 * `new Path('a/b/c').join(new Path('d/../../e')` returns the Path
75 * containing `'a/b/e'`.
76 *
77 * Note that the join operation does not drop the last segment of the
78 * base path, the way URL joining does. That would be accomplished with
79 * basepath.directoryPath.join(further).
80 *
81 * If you want to avoid joins that traverse
82 * parent directories in the base, you can check whether
83 * `further.canonicalize()` starts with '../' or equals '..'.
84 */
85 Path join(Path further);
86
87
88 /**
89 * Returns a path [:relative:] such that
90 * [:base.join(relative) == this.canonicalize():].
91 * Throws an exception if such a path is impossible.
92 * For example, if [base] is '../../a/b' and [this] is '.'.
93 * The computation is independent of the file system and current directory.
94 */
95 Path relativeTo(Path base);
96
97 /**
98 * Converts a path to a string using the native filesystem's conventions.
99 *
100 * On Windows, converts path separators to backwards slashes, and removes
101 * the leading path separator if the path starts with a drive specification.
102 * For most valid Windows paths, this should be the inverse of the
103 * constructor Path.fromNative.
104 */
105 String toNativePath();
106
107 /**
108 * Returns the path as a string. If this path is constructed using
109 * new Path() or new Path.fromNative() on a non-Windows system, the
110 * returned value is the original string argument to the constructor.
111 */
112 String toString();
113
114 /**
115 * Gets the segments of a Path. Paths beginning or ending with the
116 * path separator do not have leading or terminating empty segments.
117 * Other than that, the segments are just the result of splitting the
118 * path on the path separator.
119 *
120 * new Path('/a/b/c/d').segments() == ['a', 'b', 'c', d'];
121 * new Path(' foo bar //../') == [' foo bar ', '', '..'];
122 */
123 List<String> segments();
124
125 /**
126 * Drops the final path separator and whatever follows it from this Path,
127 * and returns the resulting Path object. If the only path separator in
128 * this Path is the first character, returns '/' instead of the empty string.
129 * If there is no path separator in the Path, returns the empty string.
130 *
131 * new Path('../images/dot.gif').directoryPath == '../images'
132 * new Path('/usr/geoffrey/www/').directoryPath == '/usr/geoffrey/www'
133 * new Path('lost_file_old').directoryPath == ''
134 * new Path('/src').directoryPath == '/'
135 * Note: new Path('/D:/src').directoryPath == '/D:'
136 */
137 Path get directoryPath();
138
139 /**
140 * The part of the path after the last path separator, or the entire path if
141 * it contains no path separator.
142 *
143 * new Path('images/DSC_0027.jpg).filename == 'DSC_0027.jpg'
144 * new Path('users/fred/').filename == ''
145 */
146 String get filename();
147
148 /**
149 * The part of [filename] before the last '.', or the entire filename if it
150 * contains no '.'.
151 *
152 * new Path('/c:/My Documents/Heidi.txt').filenameWithoutExtension
153 * would return 'Heidi'.
154 * new Path('not what I would call a path').filenameWithoutExtension
155 * would return 'not what I would call a path'.
156 */
157 String get filenameWithoutExtension();
158
159 /**
160 * The part of [filename] after the last '.', or '' if [filename]
161 * contains no '.'.
162 *
163 * new Path('tiger.svg').extension == 'svg'
164 * new Path('/src/dart/dart_secrets').extension == ''
165 */
166 String get extension();
167 }
OLDNEW
« no previous file with comments | « runtime/bin/io_sources.gypi ('k') | runtime/bin/path_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698