@@ -49,7 +49,7 @@ library path;
49
49
import 'src/context.dart' ;
50
50
import 'src/style.dart' ;
51
51
52
- export 'src/context.dart' hide createInternal ;
52
+ export 'src/context.dart' ;
53
53
export 'src/path_exception.dart' ;
54
54
export 'src/style.dart' ;
55
55
@@ -62,17 +62,27 @@ final windows = new Context(style: Style.windows);
62
62
/// A default context for manipulating URLs.
63
63
final url = new Context (style: Style .url);
64
64
65
- /// The system path context.
65
+ /// The result of [Uri.base] last time the current working directory was
66
+ /// calculated.
66
67
///
67
- /// This differs from a context created with [new Context] in that its
68
- /// [Context.current] is always the current working directory, rather than being
69
- /// set once when the context is created.
70
- final Context context = createInternal ();
68
+ /// This is used to invalidate [_cachedContext] when the working directory has
69
+ /// changed since the last time a function was called.
70
+ Uri _lastBaseUri;
71
+
72
+ /// An internal context for the current OS so we can provide a straight
73
+ /// functional interface and not require users to create one.
74
+ Context get _context {
75
+ if (_cachedContext != null && Uri .base == _lastBaseUri) return _cachedContext;
76
+ _lastBaseUri = Uri .base ;
77
+ _cachedContext = new Context ();
78
+ return _cachedContext;
79
+ }
80
+ Context _cachedContext;
71
81
72
82
/// Returns the [Style] of the current context.
73
83
///
74
84
/// This is the style that all top-level path functions will use.
75
- Style get style => context .style;
85
+ Style get style => _context .style;
76
86
77
87
/// Gets the path to the current working directory.
78
88
///
@@ -92,15 +102,15 @@ String get current {
92
102
93
103
/// Gets the path separator for the current platform. This is `\` on Windows
94
104
/// and `/` on other platforms (including the browser).
95
- String get separator => context .separator;
105
+ String get separator => _context .separator;
96
106
97
107
/// Creates a new path by appending the given path parts to [current] .
98
108
/// Equivalent to [join()] with [current] as the first argument. Example:
99
109
///
100
110
/// path.absolute('path', 'to/foo'); // -> '/your/current/dir/path/to/foo'
101
111
String absolute (String part1, [String part2, String part3, String part4,
102
112
String part5, String part6, String part7]) =>
103
- context .absolute (part1, part2, part3, part4, part5, part6, part7);
113
+ _context .absolute (part1, part2, part3, part4, part5, part6, part7);
104
114
105
115
/// Gets the part of [path] after the last separator.
106
116
///
@@ -110,7 +120,7 @@ String absolute(String part1, [String part2, String part3, String part4,
110
120
/// Trailing separators are ignored.
111
121
///
112
122
/// path.basename('path/to/'); // -> 'to'
113
- String basename (String path) => context .basename (path);
123
+ String basename (String path) => _context .basename (path);
114
124
115
125
/// Gets the part of [path] after the last separator, and without any trailing
116
126
/// file extension.
@@ -121,7 +131,7 @@ String basename(String path) => context.basename(path);
121
131
///
122
132
/// path.basenameWithoutExtension('path/to/foo.dart/'); // -> 'foo'
123
133
String basenameWithoutExtension (String path) =>
124
- context .basenameWithoutExtension (path);
134
+ _context .basenameWithoutExtension (path);
125
135
126
136
/// Gets the part of [path] before the last separator.
127
137
///
@@ -142,7 +152,7 @@ String basenameWithoutExtension(String path) =>
142
152
///
143
153
/// path.dirname('foo'); // -> '.'
144
154
/// path.dirname(''); // -> '.'
145
- String dirname (String path) => context .dirname (path);
155
+ String dirname (String path) => _context .dirname (path);
146
156
147
157
/// Gets the file extension of [path] : the portion of [basename] from the last
148
158
/// `.` to the end (including the `.` itself).
@@ -157,7 +167,7 @@ String dirname(String path) => context.dirname(path);
157
167
///
158
168
/// path.extension('~/.bashrc'); // -> ''
159
169
/// path.extension('~/.notes.txt'); // -> '.txt'
160
- String extension (String path) => context .extension (path);
170
+ String extension (String path) => _context .extension (path);
161
171
162
172
// TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed.
163
173
/// Returns the root of [path] , if it's absolute, or the empty string if it's
@@ -175,7 +185,7 @@ String extension(String path) => context.extension(path);
175
185
/// path.rootPrefix('path/to/foo'); // -> ''
176
186
/// path.rootPrefix('http://dartlang.org/path/to/foo');
177
187
/// // -> 'http://dartlang.org'
178
- String rootPrefix (String path) => context .rootPrefix (path);
188
+ String rootPrefix (String path) => _context .rootPrefix (path);
179
189
180
190
/// Returns `true` if [path] is an absolute path and `false` if it is a
181
191
/// relative path.
@@ -189,13 +199,13 @@ String rootPrefix(String path) => context.rootPrefix(path);
189
199
/// relative to the root of the current URL. Since root-relative paths are still
190
200
/// absolute in every other sense, [isAbsolute] will return true for them. They
191
201
/// can be detected using [isRootRelative] .
192
- bool isAbsolute (String path) => context .isAbsolute (path);
202
+ bool isAbsolute (String path) => _context .isAbsolute (path);
193
203
194
204
/// Returns `true` if [path] is a relative path and `false` if it is absolute.
195
205
/// On POSIX systems, absolute paths start with a `/` (forward slash). On
196
206
/// Windows, an absolute path starts with `\\` , or a drive letter followed by
197
207
/// `:/` or `:\` .
198
- bool isRelative (String path) => context .isRelative (path);
208
+ bool isRelative (String path) => _context .isRelative (path);
199
209
200
210
/// Returns `true` if [path] is a root-relative path and `false` if it's not.
201
211
///
@@ -205,7 +215,7 @@ bool isRelative(String path) => context.isRelative(path);
205
215
/// can be detected using [isRootRelative] .
206
216
///
207
217
/// No POSIX and Windows paths are root-relative.
208
- bool isRootRelative (String path) => context .isRootRelative (path);
218
+ bool isRootRelative (String path) => _context .isRootRelative (path);
209
219
210
220
/// Joins the given path parts into a single path using the current platform's
211
221
/// [separator] . Example:
@@ -222,7 +232,7 @@ bool isRootRelative(String path) => context.isRootRelative(path);
222
232
/// path.join('path', '/to', 'foo'); // -> '/to/foo'
223
233
String join (String part1, [String part2, String part3, String part4,
224
234
String part5, String part6, String part7, String part8]) =>
225
- context .join (part1, part2, part3, part4, part5, part6, part7, part8);
235
+ _context .join (part1, part2, part3, part4, part5, part6, part7, part8);
226
236
227
237
/// Joins the given path parts into a single path using the current platform's
228
238
/// [separator] . Example:
@@ -239,7 +249,7 @@ String join(String part1, [String part2, String part3, String part4,
239
249
/// path.joinAll(['path', '/to', 'foo']); // -> '/to/foo'
240
250
///
241
251
/// For a fixed number of parts, [join] is usually terser.
242
- String joinAll (Iterable <String > parts) => context .joinAll (parts);
252
+ String joinAll (Iterable <String > parts) => _context .joinAll (parts);
243
253
244
254
// TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed.
245
255
/// Splits [path] into its components using the current platform's [separator] .
@@ -262,13 +272,13 @@ String joinAll(Iterable<String> parts) => context.joinAll(parts);
262
272
/// // Browser
263
273
/// path.split('http://dartlang.org/path/to/foo');
264
274
/// // -> ['http://dartlang.org', 'path', 'to', 'foo']
265
- List <String > split (String path) => context .split (path);
275
+ List <String > split (String path) => _context .split (path);
266
276
267
277
/// Normalizes [path] , simplifying it by handling `..` , and `.` , and
268
278
/// removing redundant path separators whenever possible.
269
279
///
270
280
/// path.normalize('path/./to/..//file.text'); // -> 'path/file.txt'
271
- String normalize (String path) => context .normalize (path);
281
+ String normalize (String path) => _context .normalize (path);
272
282
273
283
/// Attempts to convert [path] to an equivalent relative path from the current
274
284
/// directory.
@@ -298,19 +308,19 @@ String normalize(String path) => context.normalize(path);
298
308
/// path.relative('http://dartlang.org', from: 'http://pub.dartlang.org');
299
309
/// // -> 'http://dartlang.org'
300
310
String relative (String path, {String from}) =>
301
- context .relative (path, from: from);
311
+ _context .relative (path, from: from);
302
312
303
313
/// Returns `true` if [child] is a path beneath `parent` , and `false` otherwise.
304
314
///
305
315
/// path.isWithin('/root/path', '/root/path/a'); // -> true
306
316
/// path.isWithin('/root/path', '/root/other'); // -> false
307
317
/// path.isWithin('/root/path', '/root/path') // -> false
308
- bool isWithin (String parent, String child) => context .isWithin (parent, child);
318
+ bool isWithin (String parent, String child) => _context .isWithin (parent, child);
309
319
310
320
/// Removes a trailing extension from the last part of [path] .
311
321
///
312
322
/// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo'
313
- String withoutExtension (String path) => context .withoutExtension (path);
323
+ String withoutExtension (String path) => _context .withoutExtension (path);
314
324
315
325
/// Returns the path represented by [uri] , which may be a [String] or a [Uri] .
316
326
///
@@ -332,7 +342,7 @@ String withoutExtension(String path) => context.withoutExtension(path);
332
342
/// If [uri] is relative, a relative path will be returned.
333
343
///
334
344
/// path.fromUri('path/to/foo'); // -> 'path/to/foo'
335
- String fromUri (uri) => context .fromUri (uri);
345
+ String fromUri (uri) => _context .fromUri (uri);
336
346
337
347
/// Returns the URI that represents [path] .
338
348
///
@@ -355,7 +365,7 @@ String fromUri(uri) => context.fromUri(uri);
355
365
///
356
366
/// path.toUri('path/to/foo')
357
367
/// // -> Uri.parse('path/to/foo')
358
- Uri toUri (String path) => context .toUri (path);
368
+ Uri toUri (String path) => _context .toUri (path);
359
369
360
370
/// Returns a terse, human-readable representation of [uri] .
361
371
///
@@ -378,4 +388,4 @@ Uri toUri(String path) => context.toUri(path);
378
388
/// path.prettyUri('http://dartlang.org/root/path/a/b.dart');
379
389
/// // -> r'a/b.dart'
380
390
/// path.prettyUri('file:///root/path'); // -> 'file:///root/path'
381
- String prettyUri (uri) => context .prettyUri (uri);
391
+ String prettyUri (uri) => _context .prettyUri (uri);
0 commit comments