Skip to content

Commit 0c2674f

Browse files
committed
libstd: Make URL parsing not require unique strings, and have URLs implement FromStr.
I considered changing FromStr to return a Result<E> parameterized over an error type E, but I decided that was premature abstraction. If you want the URL parsing error, call url::from_str() directly.
1 parent 5aedabf commit 0c2674f

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/libstd/net_url.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import map;
55
import map::{hashmap, str_hash};
66
import io::{Reader, ReaderUtil};
77
import dvec::DVec;
8+
import from_str::FromStr;
9+
import result::{Err, Ok};
10+
import to_str::ToStr;
811

912
export Url, url, userinfo, query;
1013
export from_str, to_str;
@@ -338,7 +341,7 @@ fn query_to_str(query: Query) -> ~str {
338341
}
339342
340343
// 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> {
342345
for str::each_chari(rawurl) |i,c| {
343346
match c {
344347
'A' .. 'Z' | 'a' .. 'z' => again,
@@ -384,11 +387,11 @@ impl Input: Eq {
384387
}
385388

386389
// returns userinfo, host, port, and unparsed part, or an error
387-
fn get_authority(rawurl: ~str) ->
390+
fn get_authority(rawurl: &str) ->
388391
result::Result<(Option<UserInfo>, ~str, Option<~str>, ~str), @~str> {
389392
if !str::starts_with(rawurl, ~"//") {
390393
// 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()));
392395
}
393396

394397
enum State {
@@ -514,7 +517,7 @@ fn get_authority(rawurl: ~str) ->
514517

515518
let end = end; // make end immutable so it can be captured
516519

517-
let host_is_end_plus_one = || {
520+
let host_is_end_plus_one: &fn() -> bool = || {
518521
end+1 == len
519522
&& !['?', '#', '/'].contains(rawurl[end] as char)
520523
};
@@ -553,7 +556,7 @@ fn get_authority(rawurl: ~str) ->
553556

554557

555558
// 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) ->
557560
result::Result<(~str, ~str), @~str> {
558561
let len = str::len(rawurl);
559562
let mut end = len;
@@ -584,7 +587,7 @@ fn get_path(rawurl: ~str, authority : bool) ->
584587
}
585588

586589
// returns the parsed query and the fragment, if present
587-
fn get_query_fragment(rawurl: ~str) ->
590+
fn get_query_fragment(rawurl: &str) ->
588591
result::Result<(Query, Option<~str>), @~str> {
589592
if !str::starts_with(rawurl, ~"?") {
590593
if str::starts_with(rawurl, ~"#") {
@@ -616,7 +619,7 @@ fn get_query_fragment(rawurl: ~str) ->
616619
*
617620
*/
618621
619-
fn from_str(rawurl: ~str) -> result::Result<Url, ~str> {
622+
fn from_str(rawurl: &str) -> result::Result<Url, ~str> {
620623
// scheme
621624
let mut schm = get_scheme(rawurl);
622625
if result::is_err(schm) {
@@ -650,6 +653,15 @@ fn from_str(rawurl: ~str) -> result::Result<Url, ~str> {
650653
port, path, query, fragment));
651654
}
652655

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+
653665
/**
654666
* Format a `url` as a string
655667
*

0 commit comments

Comments
 (0)