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

Side by Side Diff: pkg/path/lib/src/style/url.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: 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
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.url; 5 library path.style.url;
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 '../utils.dart'; 9 import '../utils.dart';
10 10
(...skipping 18 matching lines...) Expand all
29 bool isSeparator(int codeUnit) => codeUnit == chars.SLASH; 29 bool isSeparator(int codeUnit) => codeUnit == chars.SLASH;
30 30
31 bool needsSeparator(String path) { 31 bool needsSeparator(String path) {
32 if (path.isEmpty) return false; 32 if (path.isEmpty) return false;
33 33
34 // A URL that doesn't end in "/" always needs a separator. 34 // A URL that doesn't end in "/" always needs a separator.
35 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true; 35 if (!isSeparator(path.codeUnitAt(path.length - 1))) return true;
36 36
37 // A URI that's just "scheme://" needs an extra separator, despite ending 37 // A URI that's just "scheme://" needs an extra separator, despite ending
38 // with "/". 38 // with "/".
39 var root = _getRoot(path); 39 return path.endsWith("://");
nweiz 2014/08/04 20:11:58 This isn't sufficient; it fails on "foo/bar://".
Anders Johnsen 2014/08/05 08:57:13 I see.
40 return root != null && root.endsWith('://'); 40 }
41
42 int rootLength(String path) {
43 if (path.isEmpty) return 0;
44 if (isSeparator(path.codeUnitAt(0))) return 1;
45 int index = path.indexOf("://");
46 if (index >= 0) {
47 index = path.indexOf('/', index + 3);
Bob Nystrom 2014/08/04 17:17:32 Document this line please.
Anders Johnsen 2014/08/05 08:57:13 Done.
48 if (index > 0) return index;
49 return path.length;
50 }
nweiz 2014/08/04 20:11:58 I believe this also fails on "foo/bar://".
Anders Johnsen 2014/08/05 08:57:13 Done.
51 return 0;
41 } 52 }
42 53
43 String getRoot(String path) { 54 String getRoot(String path) {
44 var root = _getRoot(path); 55 int length = rootLength(path);
45 return root == null ? getRelativeRoot(path) : root; 56 if (length > 0) return path.substring(0, length);
57 return getRelativeRoot(path);
46 } 58 }
47 59
48 String getRelativeRoot(String path) { 60 String getRelativeRoot(String path) {
49 if (path.isEmpty) return null; 61 if (path.isEmpty) return null;
50 return isSeparator(path.codeUnitAt(0)) ? "/" : null; 62 return isSeparator(path.codeUnitAt(0)) ? "/" : null;
51 } 63 }
52 64
53 String pathFromUri(Uri uri) => uri.toString(); 65 String pathFromUri(Uri uri) => uri.toString();
54 66
55 Uri relativePathToUri(String path) => Uri.parse(path); 67 Uri relativePathToUri(String path) => Uri.parse(path);
56 Uri absolutePathToUri(String path) => Uri.parse(path); 68 Uri absolutePathToUri(String path) => Uri.parse(path);
57
58 // A helper method for [getRoot] that doesn't handle relative roots.
59 String _getRoot(String path) {
60 if (path.isEmpty) return null;
61
62 // We aren't using a RegExp for this because they're slow (issue 19090). If
63 // we could, we'd match against r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*".
64
65 if (!isAlphabetic(path.codeUnitAt(0))) return null;
66 var start = 1;
67 for (; start < path.length; start++) {
68 var char = path.codeUnitAt(start);
69 if (isAlphabetic(char)) continue;
70 if (isNumeric(char)) continue;
71 if (char == chars.MINUS || char == chars.PLUS || char == chars.PERIOD) {
72 continue;
73 }
74
75 break;
76 }
77
78 if (start + 3 > path.length) return null;
79 if (path.substring(start, start + 3) != '://') return null;
80 start += 3;
81
82 // A URL root can end with a non-"/" prefix.
83 while (start < path.length && !isSeparator(path.codeUnitAt(start))) {
84 start++;
85 }
86 return path.substring(0, start);
87 }
88 } 69 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698