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

Side by Side Diff: frog/leg/lib/uri_toremove.dart

Issue 9700095: Remove the leg-specific uri file, originally added because we did not handle compike-time constants… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 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 | « no previous file | lib/isolate/isolate_leg.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
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.
4
5 #library('uri');
6
7 /**
8 * A parsed URI, inspired by:
9 * http://closure-library.googlecode.com/svn/docs/class_goog_Uri.html
10 */
11 // TODO(ngeoffray): Remove this file once Leg support compile-time constants.
12 class Uri {
13 final String scheme;
14 final String userInfo;
15 final String domain;
16 final int port;
17 final String path;
18 final String query;
19 final String fragment;
20
21 Uri.fromString(String uri) : this._fromMatch(_splitRe.firstMatch(uri));
22
23 Uri._fromMatch(Match m) : this(_emptyIfNull(m[_COMPONENT_SCHEME]),
24 _emptyIfNull(m[_COMPONENT_USER_INFO]),
25 _emptyIfNull(m[_COMPONENT_DOMAIN]),
26 _parseIntOrZero(m[_COMPONENT_PORT]),
27 _emptyIfNull(m[_COMPONENT_PATH]),
28 _emptyIfNull(m[_COMPONENT_QUERY_DATA]),
29 _emptyIfNull(m[_COMPONENT_FRAGMENT]));
30
31 const Uri([String this.scheme = "", String this.userInfo ="",
32 String this.domain = "", int this.port = 0,
33 String this.path = "", String this.query = "",
34 String this.fragment = ""]);
35
36 static String _emptyIfNull(String val) => val != null ? val : '';
37
38 static int _parseIntOrZero(String val) {
39 if (val !== null && val != '') {
40 return Math.parseInt(val);
41 } else {
42 return 0;
43 }
44 }
45
46 // NOTE: This code was ported from: closure-library/closure/goog/uri/utils.js
47 static RegExp _regexp;
48 static RegExp get _splitRe() {
49 if (_regexp == null) {
50 _regexp = new RegExp(
51 '^' +
52 '(?:' +
53 '([^:/?#.]+)' + // scheme - ignore special characters
54 // used by other URL parts such as :,
55 // ?, /, #, and .
56 ':)?' +
57 '(?://' +
58 '(?:([^/?#]*)@)?' + // userInfo
59 '([\\w\\d\\-\\u0100-\\uffff.%]*)' +
60 // domain - restrict to letters,
61 // digits, dashes, dots, percent
62 // escapes, and unicode characters.
63 '(?::([0-9]+))?' + // port
64 ')?' +
65 '([^?#]+)?' + // path
66 '(?:\\?([^#]*))?' + // query
67 '(?:#(.*))?' + // fragment
68 '\$');
69 }
70 return _regexp;
71 }
72
73 static final _COMPONENT_SCHEME = 1;
74 static final _COMPONENT_USER_INFO = 2;
75 static final _COMPONENT_DOMAIN = 3;
76 static final _COMPONENT_PORT = 4;
77 static final _COMPONENT_PATH = 5;
78 static final _COMPONENT_QUERY_DATA = 6;
79 static final _COMPONENT_FRAGMENT = 7;
80
81 /**
82 * Determines whether a URI is absolute.
83 *
84 * See: http://tools.ietf.org/html/rfc3986#section-4.3
85 */
86 bool isAbsolute() {
87 if ("" == scheme) return false;
88 if ("" != fragment) return false;
89 return true;
90
91 /* absolute-URI = scheme ":" hier-part [ "?" query ]
92 * hier-part = "//" authority path-abempty
93 * / path-absolute
94 * / path-rootless
95 * / path-empty
96 *
97 * path = path-abempty ; begins with "/" or is empty
98 * / path-absolute ; begins with "/" but not "//"
99 * / path-noscheme ; begins with a non-colon segment
100 * / path-rootless ; begins with a segment
101 * / path-empty ; zero characters
102 *
103 * path-abempty = *( "/" segment )
104 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
105 * path-noscheme = segment-nz-nc *( "/" segment )
106 * path-rootless = segment-nz *( "/" segment )
107 * path-empty = 0<pchar>
108 * segment = *pchar
109 * segment-nz = 1*pchar
110 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
111 * ; non-zero-length segment without any colon ":"
112 *
113 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
114 */
115 }
116
117 Uri resolve(String uri) {
118 return resolveUri(new Uri.fromString(uri));
119 }
120
121 Uri resolveUri(Uri reference) {
122 // From RFC 3986.
123 String targetScheme;
124 String targetUserInfo;
125 String targetDomain;
126 int targetPort;
127 String targetPath;
128 String targetQuery;
129 if (reference.scheme != "") {
130 targetScheme = reference.scheme;
131 targetUserInfo = reference.userInfo;
132 targetDomain = reference.domain;
133 targetPort = reference.port;
134 targetPath = removeDotSegments(reference.path);
135 targetQuery = reference.query;
136 } else {
137 if (reference.hasAuthority()) {
138 targetUserInfo = reference.userInfo;
139 targetDomain = reference.domain;
140 targetPort = reference.port;
141 targetPath = removeDotSegments(reference.path);
142 targetQuery = reference.query;
143 } else {
144 if (reference.path == "") {
145 targetPath = this.path;
146 if (reference.query != "") {
147 targetQuery = reference.query;
148 } else {
149 targetQuery = this.query;
150 }
151 } else {
152 if (reference.path.startsWith("/")) {
153 targetPath = removeDotSegments(reference.path);
154 } else {
155 targetPath = removeDotSegments(merge(this.path, reference.path));
156 }
157 targetQuery = reference.query;
158 }
159 targetUserInfo = this.userInfo;
160 targetDomain = this.domain;
161 targetPort = this.port;
162 }
163 targetScheme = this.scheme;
164 }
165 return new Uri(targetScheme, targetUserInfo, targetDomain, targetPort,
166 targetPath, targetQuery, reference.fragment);
167 }
168
169 bool hasAuthority() {
170 return (userInfo != "") || (domain != "") || (port != 0);
171 }
172
173 String toString() {
174 StringBuffer sb = new StringBuffer();
175 _addIfNonEmpty(sb, scheme, scheme, ':');
176 if (hasAuthority() || (scheme == "file")) {
177 sb.add("//");
178 _addIfNonEmpty(sb, userInfo, userInfo, "@");
179 sb.add(domain === null ? "null" : domain);
180 if (port != 0) {
181 sb.add(":");
182 sb.add(port.toString());
183 }
184 }
185 sb.add(path === null ? "null" : path);
186 _addIfNonEmpty(sb, query, "?", query);
187 _addIfNonEmpty(sb, fragment, "#", fragment);
188 return sb.toString();
189 }
190
191 static void _addIfNonEmpty(StringBuffer sb, String test,
192 String first, String second) {
193 if ("" != test) {
194 sb.add(first === null ? "null" : first);
195 sb.add(second === null ? "null" : second);
196 }
197 }
198 }
199
200 String merge(String base, String reference) {
201 if (base == "") return "/$reference";
202 return base.substring(0, base.lastIndexOf("/") + 1) + "$reference";
203 }
204
205 String removeDotSegments(String path) {
206 List<String> output = [];
207 bool appendSlash = false;
208 for (String segment in path.split("/")) {
209 appendSlash = false;
210 if (segment == "..") {
211 if (!output.isEmpty() &&
212 ((output.length != 1) || (output[0] != ""))) output.removeLast();
213 appendSlash = true;
214 } else if ("." == segment) {
215 appendSlash = true;
216 } else {
217 output.add(segment);
218 }
219 }
220 if (appendSlash) output.add("");
221 return Strings.join(output, "/");
222 }
OLDNEW
« no previous file with comments | « no previous file | lib/isolate/isolate_leg.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698