Index: pkg/path/lib/src/style/windows.dart |
diff --git a/pkg/path/lib/src/style/windows.dart b/pkg/path/lib/src/style/windows.dart |
index 2965f1eee9230ae930e02acd11f42f36b2f82b02..9b9c239d5e11c887ac0e5c98b09f6d203634ba1f 100644 |
--- a/pkg/path/lib/src/style/windows.dart |
+++ b/pkg/path/lib/src/style/windows.dart |
@@ -34,9 +34,31 @@ class WindowsStyle extends InternalStyle { |
return !isSeparator(path.codeUnitAt(path.length - 1)); |
} |
+ int rootLength(String path) { |
+ if (path.isEmpty) return 0; |
+ if (path.codeUnitAt(0) == chars.SLASH) return 1; |
+ if (path.codeUnitAt(0) == chars.BACKSLASH) { |
+ if (path.length >= 2 && path.codeUnitAt(1) == chars.BACKSLASH) { |
+ 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.
|
+ if (index > 0) { |
+ index = path.indexOf('\\', index + 1); |
+ if (index > 0) return index; |
+ } |
+ 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
|
+ } |
+ 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.
|
+ } |
Bob Nystrom
2014/08/04 17:17:32
These could use some comments too.
Anders Johnsen
2014/08/05 08:57:13
Done.
|
+ if (path.length < 3) return 0; |
+ if (!isAlphabetic(path.codeUnitAt(0))) return 0; |
+ if (path.codeUnitAt(1) != chars.COLON) return 0; |
+ if (!isSeparator(path.codeUnitAt(2))) return 0; |
+ return 3; |
+ } |
+ |
String getRoot(String path) { |
- var root = _getRoot(path); |
- return root == null ? getRelativeRoot(path) : root; |
+ int length = rootLength(path); |
+ if (length > 0) return path.substring(0, length); |
+ 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.
|
} |
String getRelativeRoot(String path) { |
@@ -100,39 +122,4 @@ class WindowsStyle extends InternalStyle { |
return new Uri(scheme: 'file', pathSegments: parsed.parts); |
} |
} |
- |
- // A helper method for [getRoot] that doesn't handle relative roots. |
- String _getRoot(String path) { |
- if (path.length < 3) return null; |
- |
- // We aren't using a RegExp for this because they're slow (issue 19090). If |
- // we could, we'd match against r'^(\\\\[^\\]+\\[^\\/]+|[a-zA-Z]:[/\\])'. |
- |
- // Try roots like "C:\". |
- if (isAlphabetic(path.codeUnitAt(0))) { |
- if (path.codeUnitAt(1) != chars.COLON) return null; |
- if (!isSeparator(path.codeUnitAt(2))) return null; |
- return path.substring(0, 3); |
- } |
- |
- // Try roots like "\\server\share". |
- if (!path.startsWith('\\\\')) return null; |
- |
- var start = 2; |
- // The server is one or more non-"\" characters. |
- while (start < path.length && path.codeUnitAt(start) != chars.BACKSLASH) { |
- start++; |
- } |
- if (start == 2 || start == path.length) return null; |
- |
- // The share is one or more non-"\" characters. |
- start += 1; |
- if (path.codeUnitAt(start) == chars.BACKSLASH) return null; |
- start += 1; |
- while (start < path.length && path.codeUnitAt(start) != chars.BACKSLASH) { |
- start++; |
- } |
- |
- return path.substring(0, start); |
- } |
-} |
+} |