OLD | NEW |
---|---|
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 Loading... | |
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 int rootLength(String path) { | |
38 if (path.isEmpty) return 0; | |
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) { | |
42 int index = path.indexOf('\\', 2); | |
Bob Nystrom
2014/08/04 17:17:32
Document this and the next indexOf too.
Anders Johnsen
2014/08/05 08:57:14
Done.
| |
43 if (index > 0) { | |
44 index = path.indexOf('\\', index + 1); | |
45 if (index > 0) return index; | |
46 } | |
47 return path.length; | |
nweiz
2014/08/04 20:11:58
I believe Windows considers "\\foo" and "\\foo\bar
Anders Johnsen
2014/08/05 08:57:14
Surprisingly, this is not tested. Can you find som
nweiz
2014/08/05 20:22:26
http://msdn.microsoft.com/en-us/library/gg465305.a
Anders Johnsen
2014/08/06 07:05:15
But saying it's invalid doesn't indicate how we th
nweiz
2014/08/06 21:05:48
SGTM
| |
48 } | |
49 return 1; | |
Bob Nystrom
2014/08/04 17:17:32
Nit, but I would reverse this flow control to redu
Anders Johnsen
2014/08/05 08:57:14
Done.
| |
50 } | |
Bob Nystrom
2014/08/04 17:17:32
These could use some comments too.
Anders Johnsen
2014/08/05 08:57:13
Done.
| |
51 if (path.length < 3) return 0; | |
52 if (!isAlphabetic(path.codeUnitAt(0))) return 0; | |
53 if (path.codeUnitAt(1) != chars.COLON) return 0; | |
54 if (!isSeparator(path.codeUnitAt(2))) return 0; | |
55 return 3; | |
56 } | |
57 | |
37 String getRoot(String path) { | 58 String getRoot(String path) { |
38 var root = _getRoot(path); | 59 int length = rootLength(path); |
39 return root == null ? getRelativeRoot(path) : root; | 60 if (length > 0) return path.substring(0, length); |
61 return getRelativeRoot(path); | |
Bob Nystrom
2014/08/04 17:17:32
This code is the same for all three styles, isn't
nweiz
2014/08/04 20:11:58
+1 for making it a method of InternalStyle.
Anders Johnsen
2014/08/05 08:57:14
Done.
| |
40 } | 62 } |
41 | 63 |
42 String getRelativeRoot(String path) { | 64 String getRelativeRoot(String path) { |
43 if (path.isEmpty) return null; | 65 if (path.isEmpty) return null; |
44 if (!isSeparator(path.codeUnitAt(0))) return null; | 66 if (!isSeparator(path.codeUnitAt(0))) return null; |
45 if (path.length > 1 && isSeparator(path.codeUnitAt(1))) return null; | 67 if (path.length > 1 && isSeparator(path.codeUnitAt(1))) return null; |
46 return path[0]; | 68 return path[0]; |
47 } | 69 } |
48 | 70 |
49 String pathFromUri(Uri uri) { | 71 String pathFromUri(Uri uri) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 } | |
OLD | NEW |