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

Side by Side Diff: pkg/path/lib/src/style/windows.dart

Issue 309803003: Avoid using RegExps in path to work around issue 19090. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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.style.windows; 5 library path.style.windows;
6 6
7 import '../internal_style.dart';
7 import '../parsed_path.dart'; 8 import '../parsed_path.dart';
8 import '../internal_style.dart'; 9 import '../utils.dart';
9 10
10 /// The style for Windows paths. 11 /// The style for Windows paths.
11 class WindowsStyle extends InternalStyle { 12 class WindowsStyle extends InternalStyle {
12 WindowsStyle(); 13 WindowsStyle();
13 14
14 final name = 'windows'; 15 final name = 'windows';
15 final separator = '\\'; 16 final separator = '\\';
17 final separators = const ['/', '\\'];
18
19 // Deprecated properties.
20
16 final separatorPattern = new RegExp(r'[/\\]'); 21 final separatorPattern = new RegExp(r'[/\\]');
17 final needsSeparatorPattern = new RegExp(r'[^/\\]$'); 22 final needsSeparatorPattern = new RegExp(r'[^/\\]$');
18 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'); 23 final rootPattern = new RegExp(r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])');
19 final relativeRootPattern = new RegExp(r"^[/\\](?![/\\])"); 24 final relativeRootPattern = new RegExp(r"^[/\\](?![/\\])");
20 25
26 bool needsSeparator(String path) {
27 if (path.isEmpty) return false;
28 return !_isSeparator(path.codeUnitAt(path.length - 1));
29 }
30
31 String getRoot(String path) {
32 var root = _getRoot(path);
33 return root == null ? getRelativeRoot(path) : root;
34 }
35
36 String getRelativeRoot(String path) {
37 if (path.isEmpty) return null;
38 if (!_isSeparator(path.codeUnitAt(0))) return null;
39 if (path.length > 1 && _isSeparator(path.codeUnitAt(1))) return null;
40 return path[0];
41 }
42
21 String pathFromUri(Uri uri) { 43 String pathFromUri(Uri uri) {
22 if (uri.scheme != '' && uri.scheme != 'file') { 44 if (uri.scheme != '' && uri.scheme != 'file') {
23 throw new ArgumentError("Uri $uri must have scheme 'file:'."); 45 throw new ArgumentError("Uri $uri must have scheme 'file:'.");
24 } 46 }
25 47
26 var path = uri.path; 48 var path = uri.path;
27 if (uri.host == '') { 49 if (uri.host == '') {
28 // Drive-letter paths look like "file:///C:/path/to/file". The 50 // Drive-letter paths look like "file:///C:/path/to/file". The
29 // replaceFirst removes the extra initial slash. 51 // replaceFirst removes the extra initial slash.
30 if (path.startsWith('/')) path = path.replaceFirst("/", ""); 52 if (path.startsWith('/')) path = path.replaceFirst("/", "");
(...skipping 28 matching lines...) Expand all
59 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently 81 // If the path is a bare root (e.g. "C:\"), [parsed.parts] will currently
60 // be empty. We add an empty component so the URL constructor produces 82 // be empty. We add an empty component so the URL constructor produces
61 // "file:///C:/", with a trailing slash. We also add an empty component if 83 // "file:///C:/", with a trailing slash. We also add an empty component if
62 // the URL otherwise has a trailing slash. 84 // the URL otherwise has a trailing slash.
63 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) { 85 if (parsed.parts.length == 0 || parsed.hasTrailingSeparator) {
64 parsed.parts.add(""); 86 parsed.parts.add("");
65 } 87 }
66 88
67 // Get rid of the trailing "\" in "C:\" because the URI constructor will 89 // Get rid of the trailing "\" in "C:\" because the URI constructor will
68 // add a separator on its own. 90 // add a separator on its own.
69 parsed.parts.insert(0, parsed.root.replaceAll(separatorPattern, "")); 91 parsed.parts.insert(0,
92 parsed.root.replaceAll("/", "").replaceAll("\\", ""));
70 93
71 return new Uri(scheme: 'file', pathSegments: parsed.parts); 94 return new Uri(scheme: 'file', pathSegments: parsed.parts);
72 } 95 }
73 } 96 }
97
98 // A helper method for [getRoot] that doesn't handle relative roots.
99 String _getRoot(String path) {
100 if (path.length < 3) return null;
101
102 // We aren't using a RegExp for this because they're slow (issue 19090). If
103 // we could, we'd match against r"r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'".
104
105 // Try roots like "C:\".
106 if (isAlphabetic(path.codeUnitAt(0))) {
107 if (path.codeUnitAt(1) != 0x3a) return null; // ":"
Bob Nystrom 2014/06/02 22:43:05 ":". Or, better, make a little class with constan
nweiz 2014/06/02 23:13:25 Done.
108 if (!_isSeparator(path.codeUnitAt(2))) return null;
109 return path.substring(0, 3);
110 }
111
112 // Try roots like "\\server\share".
113 if (!path.startsWith('\\\\')) return null;
114
115 var start = 2;
116 // The server is one or more non-"\" characters.
117 while (start < path.length && path.codeUnitAt(start) != 0x5c) {
118 start++;
119 }
120 if (start == 2 || start == path.length) return null;
121
122 // The share is one or more non-"\" characters.
123 start += 1;
124 if (path.codeUnitAt(start) == 0x5c) return null;
125 start += 1;
126 while (start < path.length && path.codeUnitAt(start) != 0x5c) {
127 start++;
128 }
129
130 return path.substring(0, start);
131 }
132
133 /// Returns whether [char] is a separator ("/" or "\\").
134 bool _isSeparator(int char) => char == 0x2f || char == 0x5c;
74 } 135 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698