@@ -5,6 +5,9 @@ import map;
5
5
import map:: { hashmap, str_hash} ;
6
6
import io:: { Reader , ReaderUtil } ;
7
7
import dvec:: DVec ;
8
+ import from_str:: FromStr ;
9
+ import result:: { Err , Ok } ;
10
+ import to_str:: ToStr ;
8
11
9
12
export Url , url, userinfo, query;
10
13
export from_str, to_str;
@@ -338,7 +341,7 @@ fn query_to_str(query: Query) -> ~str {
338
341
}
339
342
340
343
// returns the scheme and the rest of the url, or a parsing error
341
- fn get_scheme(rawurl: ~ str) -> result::Result<(~str, ~str), @~str> {
344
+ fn get_scheme(rawurl: & str) -> result::Result<(~str, ~str), @~str> {
342
345
for str::each_chari(rawurl) |i,c| {
343
346
match c {
344
347
'A' .. 'Z' | 'a' .. 'z' => again,
@@ -384,11 +387,11 @@ impl Input: Eq {
384
387
}
385
388
386
389
// returns userinfo, host, port, and unparsed part, or an error
387
- fn get_authority( rawurl: ~ str ) ->
390
+ fn get_authority( rawurl: & str) ->
388
391
result:: Result <( Option <UserInfo >, ~str, Option <~str>, ~str) , @~str> {
389
392
if !str:: starts_with ( rawurl, ~"//") {
390
393
// there is no authority.
391
- return result:: Ok ( ( option:: None , ~"", option:: None , copy rawurl) ) ;
394
+ return result:: Ok ( ( option:: None , ~"", option:: None , rawurl. to_str ( ) ) ) ;
392
395
}
393
396
394
397
enum State {
@@ -514,7 +517,7 @@ fn get_authority(rawurl: ~str) ->
514
517
515
518
let end = end; // make end immutable so it can be captured
516
519
517
- let host_is_end_plus_one = || {
520
+ let host_is_end_plus_one: & fn ( ) -> bool = || {
518
521
end+1 == len
519
522
&& ![ '?' , '#' , '/' ] . contains ( rawurl[ end] as char )
520
523
} ;
@@ -553,7 +556,7 @@ fn get_authority(rawurl: ~str) ->
553
556
554
557
555
558
// returns the path and unparsed part of url, or an error
556
- fn get_path ( rawurl : ~ str , authority : bool ) ->
559
+ fn get_path ( rawurl : & str , authority : bool ) ->
557
560
result:: Result < ( ~str , ~str ) , @~str > {
558
561
let len = str:: len ( rawurl) ;
559
562
let mut end = len;
@@ -584,7 +587,7 @@ fn get_path(rawurl: ~str, authority : bool) ->
584
587
}
585
588
586
589
// returns the parsed query and the fragment, if present
587
- fn get_query_fragment( rawurl : ~ str ) ->
590
+ fn get_query_fragment ( rawurl : & str ) ->
588
591
result:: Result < ( Query , Option < ~str > ) , @~str > {
589
592
if !str:: starts_with ( rawurl, ~"?") {
590
593
if str:: starts_with ( rawurl, ~"#") {
@@ -616,7 +619,7 @@ fn get_query_fragment(rawurl: ~str) ->
616
619
*
617
620
*/
618
621
619
- fn from_str(rawurl: ~ str) -> result::Result<Url, ~str> {
622
+ fn from_str(rawurl: & str) -> result::Result<Url, ~str> {
620
623
// scheme
621
624
let mut schm = get_scheme(rawurl);
622
625
if result::is_err(schm) {
@@ -650,6 +653,15 @@ fn from_str(rawurl: ~str) -> result::Result<Url, ~str> {
650
653
port, path, query, fragment) ) ;
651
654
}
652
655
656
+ impl Url : FromStr {
657
+ static fn from_str( s : & str ) -> Option < Url > {
658
+ match from_str( s ) {
659
+ Ok ( url ) => Some ( url ) ,
660
+ Err ( _) => None
661
+ }
662
+ }
663
+ }
664
+
653
665
/**
654
666
* Format a `url` as a string
655
667
*
0 commit comments