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

Side by Side Diff: sdk/lib/core/uri.dart

Issue 2664453003: Add Uri.isScheme test function. (Closed)
Patch Set: Address comment. Created 3 years, 10 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
« no previous file with comments | « CHANGELOG.md ('k') | tests/corelib/uri_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) 2012, the Dart project authors. Please see the AUTHORS file 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 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 part of dart.core; 5 part of dart.core;
6 6
7 // Frequently used character codes. 7 // Frequently used character codes.
8 const int _SPACE = 0x20; 8 const int _SPACE = 0x20;
9 const int _PERCENT = 0x25; 9 const int _PERCENT = 0x25;
10 const int _PLUS = 0x2B; 10 const int _PLUS = 0x2B;
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 * Returns the origin of the URI in the form scheme://host:port for the 505 * Returns the origin of the URI in the form scheme://host:port for the
506 * schemes http and https. 506 * schemes http and https.
507 * 507 *
508 * It is an error if the scheme is not "http" or "https", or if the host name 508 * It is an error if the scheme is not "http" or "https", or if the host name
509 * is missing or empty. 509 * is missing or empty.
510 * 510 *
511 * See: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin 511 * See: http://www.w3.org/TR/2011/WD-html5-20110405/origin-0.html#origin
512 */ 512 */
513 String get origin; 513 String get origin;
514 514
515 /// Whether the scheme of this [Uri] is [scheme].
516 ///
517 /// The [scheme] should be the same as the one returned by [Uri.scheme],
518 /// but doesn't have to be case-normalized to lower-case characters.
519 ///
520 /// Example:
521 /// ```dart
522 /// var uri = Uri.parse("http://example.com/");
523 /// print(uri.isScheme("HTTP")); // Prints true.
524 /// ```
525 ///
526 /// A `null` or empty [scheme] string matches a URI with no scheme
527 /// (one where [hasScheme] returns false).
528 bool isScheme(String scheme);
529
515 /** 530 /**
516 * Returns the file path from a file URI. 531 * Returns the file path from a file URI.
517 * 532 *
518 * The returned path has either Windows or non-Windows 533 * The returned path has either Windows or non-Windows
519 * semantics. 534 * semantics.
520 * 535 *
521 * For non-Windows semantics the slash ("/") is used to separate 536 * For non-Windows semantics the slash ("/") is used to separate
522 * path segments. 537 * path segments.
523 * 538 *
524 * For Windows semantics the backslash ("\\") separator is used to 539 * For Windows semantics the backslash ("\\") separator is used to
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 if (scheme == "https") return 443; 1540 if (scheme == "https") return 443;
1526 return 0; 1541 return 0;
1527 } 1542 }
1528 1543
1529 String get path => _path; 1544 String get path => _path;
1530 1545
1531 String get query => _query ?? ""; 1546 String get query => _query ?? "";
1532 1547
1533 String get fragment => _fragment ?? ""; 1548 String get fragment => _fragment ?? "";
1534 1549
1550 bool isScheme(String scheme) {
1551 String thisScheme = this.scheme;
1552 if (scheme == null) return thisScheme.isEmpty;
1553 if (scheme.length != thisScheme.length) return false;
1554 return _compareScheme(scheme, thisScheme);
1555 }
1556
1557 /// Compares scheme characters in [scheme] and at the start of [uri].
1558 ///
1559 /// Returns `true` if [scheme] represents the same scheme as the start of
1560 /// [uri]. That means having the same characters, but possibly different case
1561 /// for letters.
1562 ///
1563 /// This function doesn't check that the characters are valid URI scheme
1564 /// characters. The [uri] is assumed to be valid, so if [scheme] matches
1565 /// it, it has to be valid too.
1566 ///
1567 /// The length should be tested before calling this function,
1568 /// so the scheme part of [uri] is known to have the same length as [scheme].
1569 static bool _compareScheme(String scheme, String uri) {
1570 for (int i = 0; i < scheme.length; i++) {
1571 int schemeChar = scheme.codeUnitAt(i);
1572 int uriChar = uri.codeUnitAt(i);
1573 int delta = schemeChar ^ uriChar;
1574 if (delta != 0) {
1575 if (delta == 0x20) {
1576 // Might be a case difference.
1577 int lowerChar = uriChar | delta;
1578 if (0x61 /*a*/ <= lowerChar && lowerChar <= 0x7a /*z*/) {
1579 continue;
1580 }
1581 }
1582 return false;
1583 }
1584 }
1585 return true;
1586 }
1587
1535 // Report a parse failure. 1588 // Report a parse failure.
1536 static void _fail(String uri, int index, String message) { 1589 static void _fail(String uri, int index, String message) {
1537 throw new FormatException(message, uri, index); 1590 throw new FormatException(message, uri, index);
1538 } 1591 }
1539 1592
1540 static Uri _makeHttpUri(String scheme, 1593 static Uri _makeHttpUri(String scheme,
1541 String authority, 1594 String authority,
1542 String unencodedPath, 1595 String unencodedPath,
1543 Map<String, String> queryParameters) { 1596 Map<String, String> queryParameters) {
1544 var userInfo = ""; 1597 var userInfo = "";
(...skipping 2519 matching lines...) Expand 10 before | Expand all | Expand 10 after
4064 bool get hasAuthority => _hostStart > 0; 4117 bool get hasAuthority => _hostStart > 0;
4065 bool get hasUserInfo => _hostStart > _schemeEnd + 4; 4118 bool get hasUserInfo => _hostStart > _schemeEnd + 4;
4066 bool get hasPort => _hostStart > 0 && _portStart + 1 < _pathStart; 4119 bool get hasPort => _hostStart > 0 && _portStart + 1 < _pathStart;
4067 bool get hasQuery => _queryStart < _fragmentStart; 4120 bool get hasQuery => _queryStart < _fragmentStart;
4068 bool get hasFragment => _fragmentStart < _uri.length; 4121 bool get hasFragment => _fragmentStart < _uri.length;
4069 4122
4070 bool get _isFile => _schemeEnd == 4 && _uri.startsWith("file"); 4123 bool get _isFile => _schemeEnd == 4 && _uri.startsWith("file");
4071 bool get _isHttp => _schemeEnd == 4 && _uri.startsWith("http"); 4124 bool get _isHttp => _schemeEnd == 4 && _uri.startsWith("http");
4072 bool get _isHttps => _schemeEnd == 5 && _uri.startsWith("https"); 4125 bool get _isHttps => _schemeEnd == 5 && _uri.startsWith("https");
4073 bool get _isPackage => _schemeEnd == 7 && _uri.startsWith("package"); 4126 bool get _isPackage => _schemeEnd == 7 && _uri.startsWith("package");
4127 /// Like [isScheme] but expects argument to be case normalized.
4074 bool _isScheme(String scheme) => 4128 bool _isScheme(String scheme) =>
4075 _schemeEnd == scheme.length && _uri.startsWith(scheme); 4129 _schemeEnd == scheme.length && _uri.startsWith(scheme);
4076 4130
4077 bool get hasAbsolutePath => _uri.startsWith("/", _pathStart); 4131 bool get hasAbsolutePath => _uri.startsWith("/", _pathStart);
4078 bool get hasEmptyPath => _pathStart == _queryStart; 4132 bool get hasEmptyPath => _pathStart == _queryStart;
4079 4133
4080 bool get isAbsolute => hasScheme && !hasFragment; 4134 bool get isAbsolute => hasScheme && !hasFragment;
4081 4135
4136 bool isScheme(String scheme) {
4137 if (scheme == null || scheme.isEmpty) return _schemeEnd < 0;
4138 if (scheme.length != _schemeEnd) return false;
4139 return _Uri._compareScheme(scheme, _uri);
4140 }
4141
4082 String get scheme { 4142 String get scheme {
4083 if (_schemeEnd <= 0) return ""; 4143 if (_schemeEnd <= 0) return "";
4084 if (_schemeCache != null) return _schemeCache; 4144 if (_schemeCache != null) return _schemeCache;
4085 if (_isHttp) { 4145 if (_isHttp) {
4086 _schemeCache = "http"; 4146 _schemeCache = "http";
4087 } else if (_isHttps) { 4147 } else if (_isHttps) {
4088 _schemeCache = "https"; 4148 _schemeCache = "https";
4089 } else if (_isFile) { 4149 } else if (_isFile) {
4090 _schemeCache = "file"; 4150 _schemeCache = "file";
4091 } else if (_isPackage) { 4151 } else if (_isPackage) {
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
4506 int delta = (text.codeUnitAt(start + 4) ^ _COLON) * 3; 4566 int delta = (text.codeUnitAt(start + 4) ^ _COLON) * 3;
4507 delta |= text.codeUnitAt(start) ^ 0x64 /*d*/; 4567 delta |= text.codeUnitAt(start) ^ 0x64 /*d*/;
4508 delta |= text.codeUnitAt(start + 1) ^ 0x61 /*a*/; 4568 delta |= text.codeUnitAt(start + 1) ^ 0x61 /*a*/;
4509 delta |= text.codeUnitAt(start + 2) ^ 0x74 /*t*/; 4569 delta |= text.codeUnitAt(start + 2) ^ 0x74 /*t*/;
4510 delta |= text.codeUnitAt(start + 3) ^ 0x61 /*a*/; 4570 delta |= text.codeUnitAt(start + 3) ^ 0x61 /*a*/;
4511 return delta; 4571 return delta;
4512 } 4572 }
4513 4573
4514 /// Helper function returning the length of a string, or `0` for `null`. 4574 /// Helper function returning the length of a string, or `0` for `null`.
4515 int _stringOrNullLength(String s) => (s == null) ? 0 : s.length; 4575 int _stringOrNullLength(String s) => (s == null) ? 0 : s.length;
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | tests/corelib/uri_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698