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

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

Issue 439223002: Add InternalStyle:rootLength to implement isAbsolute and rootPrefix. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add windows test to 'document' \\server root prefix. Created 6 years, 4 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 | « pkg/path/lib/src/style/url.dart ('k') | pkg/path/test/url_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 '../characters.dart' as chars; 7 import '../characters.dart' as chars;
8 import '../internal_style.dart'; 8 import '../internal_style.dart';
9 import '../parsed_path.dart'; 9 import '../parsed_path.dart';
10 import '../utils.dart'; 10 import '../utils.dart';
(...skipping 16 matching lines...) Expand all
27 bool containsSeparator(String path) => path.contains('/'); 27 bool containsSeparator(String path) => path.contains('/');
28 28
29 bool isSeparator(int codeUnit) => 29 bool isSeparator(int codeUnit) =>
30 codeUnit == chars.SLASH || codeUnit == chars.BACKSLASH; 30 codeUnit == chars.SLASH || codeUnit == chars.BACKSLASH;
31 31
32 bool needsSeparator(String path) { 32 bool needsSeparator(String path) {
33 if (path.isEmpty) return false; 33 if (path.isEmpty) return false;
34 return !isSeparator(path.codeUnitAt(path.length - 1)); 34 return !isSeparator(path.codeUnitAt(path.length - 1));
35 } 35 }
36 36
37 String getRoot(String path) { 37 int rootLength(String path) {
38 var root = _getRoot(path); 38 if (path.isEmpty) return 0;
39 return root == null ? getRelativeRoot(path) : root; 39 if (path.codeUnitAt(0) == chars.SLASH) return 1;
40 if (path.codeUnitAt(0) == chars.BACKSLASH) {
41 if (path.length < 2 || path.codeUnitAt(1) != chars.BACKSLASH) return 1;
42 // The path is a network share. Search for up to two '\'s, as they are
43 // the server and share - and part of the root part.
44 var index = path.indexOf('\\', 2);
45 if (index > 0) {
46 index = path.indexOf('\\', index + 1);
47 if (index > 0) return index;
48 }
49 return path.length;
50 }
51 // If the path is of the form 'C:/' or 'C:\', with C being any letter, it's
52 // a root part.
53 if (path.length < 3) return 0;
54 // Check for the letter.
55 if (!isAlphabetic(path.codeUnitAt(0))) return 0;
56 // Check for the ':'.
57 if (path.codeUnitAt(1) != chars.COLON) return 0;
58 // Check for either '/' or '\'.
59 if (!isSeparator(path.codeUnitAt(2))) return 0;
60 return 3;
40 } 61 }
41 62
63 bool isRootRelative(String path) => rootLength(path) == 1;
64
42 String getRelativeRoot(String path) { 65 String getRelativeRoot(String path) {
43 if (path.isEmpty) return null; 66 var length = rootLength(path);
44 if (!isSeparator(path.codeUnitAt(0))) return null; 67 if (length == 1) return path[0];
45 if (path.length > 1 && isSeparator(path.codeUnitAt(1))) return null; 68 return null;
46 return path[0];
47 } 69 }
48 70
49 String pathFromUri(Uri uri) { 71 String pathFromUri(Uri uri) {
50 if (uri.scheme != '' && uri.scheme != 'file') { 72 if (uri.scheme != '' && uri.scheme != 'file') {
51 throw new ArgumentError("Uri $uri must have scheme 'file:'."); 73 throw new ArgumentError("Uri $uri must have scheme 'file:'.");
52 } 74 }
53 75
54 var path = uri.path; 76 var path = uri.path;
55 if (uri.host == '') { 77 if (uri.host == '') {
56 // Drive-letter paths look like "file:///C:/path/to/file". The 78 // Drive-letter paths look like "file:///C:/path/to/file". The
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 115 }
94 116
95 // Get rid of the trailing "\" in "C:\" because the URI constructor will 117 // Get rid of the trailing "\" in "C:\" because the URI constructor will
96 // add a separator on its own. 118 // add a separator on its own.
97 parsed.parts.insert(0, 119 parsed.parts.insert(0,
98 parsed.root.replaceAll("/", "").replaceAll("\\", "")); 120 parsed.root.replaceAll("/", "").replaceAll("\\", ""));
99 121
100 return new Uri(scheme: 'file', pathSegments: parsed.parts); 122 return new Uri(scheme: 'file', pathSegments: parsed.parts);
101 } 123 }
102 } 124 }
103 125 }
104 // A helper method for [getRoot] that doesn't handle relative roots.
105 String _getRoot(String path) {
106 if (path.length < 3) return null;
107
108 // We aren't using a RegExp for this because they're slow (issue 19090). If
109 // we could, we'd match against r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'.
110
111 // Try roots like "C:\".
112 if (isAlphabetic(path.codeUnitAt(0))) {
113 if (path.codeUnitAt(1) != chars.COLON) return null;
114 if (!isSeparator(path.codeUnitAt(2))) return null;
115 return path.substring(0, 3);
116 }
117
118 // Try roots like "\\server\share".
119 if (!path.startsWith('\\\\')) return null;
120
121 var start = 2;
122 // The server is one or more non-"\" characters.
123 while (start < path.length && path.codeUnitAt(start) != chars.BACKSLASH) {
124 start++;
125 }
126 if (start == 2 || start == path.length) return null;
127
128 // The share is one or more non-"\" characters.
129 start += 1;
130 if (path.codeUnitAt(start) == chars.BACKSLASH) return null;
131 start += 1;
132 while (start < path.length && path.codeUnitAt(start) != chars.BACKSLASH) {
133 start++;
134 }
135
136 return path.substring(0, start);
137 }
138 }
OLDNEW
« no previous file with comments | « pkg/path/lib/src/style/url.dart ('k') | pkg/path/test/url_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698