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

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

Issue 333163003: Add Uri.replace which creates a new Uri with the same fields as the original, but with some fields … (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. 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
« no previous file with comments | « no previous file | 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 /** 7 /**
8 * A parsed URI, such as a URL. 8 * A parsed URI, such as a URL.
9 * 9 *
10 * **See also:** 10 * **See also:**
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 * If `fragment` is omitted or `null`, the URI will have no fragment part. 474 * If `fragment` is omitted or `null`, the URI will have no fragment part.
475 */ 475 */
476 factory Uri({String scheme : "", 476 factory Uri({String scheme : "",
477 String userInfo : "", 477 String userInfo : "",
478 String host, 478 String host,
479 int port, 479 int port,
480 String path, 480 String path,
481 Iterable<String> pathSegments, 481 Iterable<String> pathSegments,
482 String query, 482 String query,
483 Map<String, String> queryParameters, 483 Map<String, String> queryParameters,
484 fragment}) { 484 String fragment}) {
485 scheme = _makeScheme(scheme, _stringOrNullLength(scheme)); 485 scheme = _makeScheme(scheme, _stringOrNullLength(scheme));
486 userInfo = _makeUserInfo(userInfo, 0, _stringOrNullLength(userInfo)); 486 userInfo = _makeUserInfo(userInfo, 0, _stringOrNullLength(userInfo));
487 host = _makeHost(host, 0, _stringOrNullLength(host), false); 487 host = _makeHost(host, 0, _stringOrNullLength(host), false);
488 // Special case this constructor for backwards compatibility. 488 // Special case this constructor for backwards compatibility.
489 if (query == "") query = null; 489 if (query == "") query = null;
490 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters); 490 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters);
491 fragment = _makeFragment(fragment, 0, _stringOrNullLength(fragment)); 491 fragment = _makeFragment(fragment, 0, _stringOrNullLength(fragment));
492 port = _makePort(port, scheme); 492 port = _makePort(port, scheme);
493 bool isFile = (scheme == "file"); 493 bool isFile = (scheme == "file");
494 if (host == null && 494 if (host == null &&
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 } 812 }
813 } else { 813 } else {
814 // Relative URI. 814 // Relative URI.
815 var pathSegments = path.split(sep); 815 var pathSegments = path.split(sep);
816 _checkWindowsPathReservedCharacters(pathSegments, true); 816 _checkWindowsPathReservedCharacters(pathSegments, true);
817 return new Uri(pathSegments: pathSegments); 817 return new Uri(pathSegments: pathSegments);
818 } 818 }
819 } 819 }
820 820
821 /** 821 /**
822 * Returns a new `Uri` based on this one, but with some parts replaced.
823 *
824 * This method takes the same parameters as the [new Uri] constructor,
825 * and they have the same meaning.
826 *
827 * At most one of [path] and [pathSegments] must be provided.
828 * Likewise, at most one of [query] and [queryParameters] must be provided.
829 *
830 * Each part that is not provided will default to the corresponding
831 * value from this `Uri` instead.
832 *
833 * This method is different from [Uri.resolve] which overrides in a
834 * hierarchial manner,
835 * and can instead replace each part of a `Uri` individually.
836 *
837 * Example:
838 *
839 * Uri uri1 = Uri.parse("a://b@c:4/d/e?f#g");
840 * Uri uri2 = uri1.replace(scheme: "A", path: "D/E/E", fragment: "G");
841 * print(uri2); // prints "A://b@c:4/D/E/E/?f#G"
842 *
843 * This method acts similarly to using the `new Uri` constructor with
844 * some of the arguments taken from this `Uri` . Example:
845 *
846 * Uri uri3 = new Uri(
847 * scheme: "A",
848 * userInfo: uri1.userInfo,
849 * host: uri1.host,
850 * port: uri1.port,
851 * path: "D/E/E",
852 * query: uri1.query,
853 * fragment: "G");
854 * print(uri3); // prints "A://b@c:4/D/E/E/?f#G"
855 * print(uri2 == uri3); // prints true.
856 *
857 * Using this method can be seen as a shorthand for the `Uri` constructor
858 * call above, but may also be slightly faster because the parts taken
859 * from this `Uri` need not be checked for validity again.
860 */
861 Uri replace({String scheme,
862 String userInfo,
863 String host,
864 int port,
865 String path,
866 Iterable<String> pathSegments,
867 String query,
868 Map<String, String> queryParameters,
869 String fragment}) {
870 // Set to true if the scheme has (potentially) changed.
871 // In that case, the default port may also have changed and we need
872 // to check even the existing port.
873 bool schemeChanged = false;
874 if (scheme != null) {
875 scheme = _makeScheme(scheme, scheme.length);
876 schemeChanged = true;
877 } else {
878 scheme = this.scheme;
879 }
880 bool isFile = (scheme == "file");
881 if (userInfo != null) {
882 userInfo = _makeUserInfo(userInfo, 0, userInfo.length);
883 } else {
884 userInfo = this.userInfo;
885 }
886 if (port != null) {
887 port = _makePort(port, scheme);
888 } else {
889 port = this.port;
890 if (schemeChanged) {
891 // The default port might have changed.
892 port = _makePort(port, scheme);
893 }
894 }
895 if (host != null) {
896 host = _makeHost(host, 0, host.length, false);
897 } else if (this.hasAuthority) {
898 host = this.host;
899 } else if (userInfo.isNotEmpty || port != null || isFile) {
900 host = "";
901 }
902
903 bool ensureLeadingSlash = (host != null);
904 if (path != null || pathSegments != null) {
905 path = _makePath(path, 0, _stringOrNullLength(path), pathSegments,
906 ensureLeadingSlash, isFile);
907 } else {
908 path = this.path;
909 if ((isFile || (ensureLeadingSlash && !path.isEmpty)) &&
910 !path.startsWith('/')) {
911 path = "/$path";
912 }
913 }
914
915 if (query != null || queryParameters != null) {
916 query = _makeQuery(query, 0, _stringOrNullLength(query), queryParameters);
917 } else if (this.hasQuery) {
918 query = this.query;
919 }
920
921 if (fragment != null) {
922 fragment = _makeFragment(fragment, 0, fragment.length);
923 } else if (this.hasFragment) {
924 fragment = this.fragment;
925 }
926
927 return new Uri._internal(
928 scheme, userInfo, host, port, path, query, fragment);
929 }
930
931 /**
822 * Returns the URI path split into its segments. Each of the 932 * Returns the URI path split into its segments. Each of the
823 * segments in the returned list have been decoded. If the path is 933 * segments in the returned list have been decoded. If the path is
824 * empty the empty list will be returned. A leading slash `/` does 934 * empty the empty list will be returned. A leading slash `/` does
825 * not affect the segments returned. 935 * not affect the segments returned.
826 * 936 *
827 * The returned list is unmodifiable and will throw [UnsupportedError] on any 937 * The returned list is unmodifiable and will throw [UnsupportedError] on any
828 * calls that would mutate it. 938 * calls that would mutate it.
829 */ 939 */
830 List<String> get pathSegments { 940 List<String> get pathSegments {
831 if (_pathSegments == null) { 941 if (_pathSegments == null) {
(...skipping 1426 matching lines...) Expand 10 before | Expand all | Expand 10 after
2258 0xafff, // 0x30 - 0x3f 1111111111110101 2368 0xafff, // 0x30 - 0x3f 1111111111110101
2259 // @ABCDEFGHIJKLMNO 2369 // @ABCDEFGHIJKLMNO
2260 0xffff, // 0x40 - 0x4f 1111111111111111 2370 0xffff, // 0x40 - 0x4f 1111111111111111
2261 // PQRSTUVWXYZ _ 2371 // PQRSTUVWXYZ _
2262 0x87ff, // 0x50 - 0x5f 1111111111100001 2372 0x87ff, // 0x50 - 0x5f 1111111111100001
2263 // abcdefghijklmno 2373 // abcdefghijklmno
2264 0xfffe, // 0x60 - 0x6f 0111111111111111 2374 0xfffe, // 0x60 - 0x6f 0111111111111111
2265 // pqrstuvwxyz ~ 2375 // pqrstuvwxyz ~
2266 0x47ff]; // 0x70 - 0x7f 1111111111100010 2376 0x47ff]; // 0x70 - 0x7f 1111111111100010
2267 } 2377 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/uri_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698