about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorJesse Jones <jesse9jones@gmail.com>2012-11-17 09:57:14 -0800
committerBrian Anderson <banderson@mozilla.com>2012-11-18 13:25:25 -0800
commit15989ecb139a4ee4a53e6657fd4fc8cee9d729de (patch)
tree832699b483cfe1d7df53a1982a9ca556e00af8c6 /src/libstd
parentc5ab47e7ba8881fb76e2266e105d373b59ed09db (diff)
downloadrust-15989ecb139a4ee4a53e6657fd4fc8cee9d729de.tar.gz
rust-15989ecb139a4ee4a53e6657fd4fc8cee9d729de.zip
Made most of the URL functions pure.
This closes #3782.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/net_url.rs74
1 files changed, 40 insertions, 34 deletions
diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs
index 8ea9513d155..dd76f65a046 100644
--- a/src/libstd/net_url.rs
+++ b/src/libstd/net_url.rs
@@ -27,7 +27,7 @@ type UserInfo = {
 
 pub type Query = ~[(~str, ~str)];
 
-pub fn Url(scheme: ~str, user: Option<UserInfo>, host: ~str,
+pub pure fn Url(scheme: ~str, user: Option<UserInfo>, host: ~str,
        port: Option<~str>, path: ~str, query: Query,
        fragment: Option<~str>) -> Url {
     Url { scheme: move scheme, user: move user, host: move host,
@@ -35,7 +35,7 @@ pub fn Url(scheme: ~str, user: Option<UserInfo>, host: ~str,
          fragment: move fragment }
 }
 
-fn UserInfo(user: ~str, pass: Option<~str>) -> UserInfo {
+pure fn UserInfo(user: ~str, pass: Option<~str>) -> UserInfo {
     {user: move user, pass: move pass}
 }
 
@@ -84,8 +84,9 @@ fn encode_inner(s: &str, full_url: bool) -> ~str {
  *
  * This function is compliant with RFC 3986.
  */
-pub fn encode(s: &str) -> ~str {
-    encode_inner(s, true)
+pub pure fn encode(s: &str) -> ~str {
+    // unsafe only because encode_inner does (string) IO
+    unsafe {encode_inner(s, true)}
 }
 
 /**
@@ -95,8 +96,9 @@ pub fn encode(s: &str) -> ~str {
  * This function is compliant with RFC 3986.
  */
 
-pub fn encode_component(s: &str) -> ~str {
-    encode_inner(s, false)
+pub pure fn encode_component(s: &str) -> ~str {
+    // unsafe only because encode_inner does (string) IO
+    unsafe {encode_inner(s, false)}
 }
 
 fn decode_inner(s: &str, full_url: bool) -> ~str {
@@ -142,15 +144,17 @@ fn decode_inner(s: &str, full_url: bool) -> ~str {
  *
  * This will only decode escape sequences generated by encode_uri.
  */
-pub fn decode(s: &str) -> ~str {
-    decode_inner(s, true)
+pub pure fn decode(s: &str) -> ~str {
+    // unsafe only because decode_inner does (string) IO
+    unsafe {decode_inner(s, true)}
 }
 
 /**
  * Decode a string encoded with percent encoding.
  */
-pub fn decode_component(s: &str) -> ~str {
-    decode_inner(s, false)
+pub pure fn decode_component(s: &str) -> ~str {
+    // unsafe only because decode_inner does (string) IO
+    unsafe {decode_inner(s, false)}
 }
 
 fn encode_plus(s: &str) -> ~str {
@@ -264,19 +268,21 @@ pub fn decode_form_urlencoded(s: ~[u8]) ->
 }
 
 
-fn split_char_first(s: &str, c: char) -> (~str, ~str) {
+pure fn split_char_first(s: &str, c: char) -> (~str, ~str) {
     let len = str::len(s);
     let mut index = len;
     let mut mat = 0;
-    do io::with_str_reader(s) |rdr| {
-        let mut ch : char;
-        while !rdr.eof() {
-            ch = rdr.read_byte() as char;
-            if ch == c {
-                // found a match, adjust markers
-                index = rdr.tell()-1;
-                mat = 1;
-                break;
+    unsafe {
+        do io::with_str_reader(s) |rdr| {
+            let mut ch : char;
+            while !rdr.eof() {
+                ch = rdr.read_byte() as char;
+                if ch == c {
+                    // found a match, adjust markers
+                    index = rdr.tell()-1;
+                    mat = 1;
+                    break;
+                }
             }
         }
     }
@@ -288,7 +294,7 @@ fn split_char_first(s: &str, c: char) -> (~str, ~str) {
     }
 }
 
-fn userinfo_from_str(uinfo: &str) -> UserInfo {
+pure fn userinfo_from_str(uinfo: &str) -> UserInfo {
     let (user, p) = split_char_first(uinfo, ':');
     let pass = if str::len(p) == 0 {
         option::None
@@ -315,12 +321,12 @@ impl UserInfo : Eq {
     pure fn ne(other: &UserInfo) -> bool { !self.eq(other) }
 }
 
-fn query_from_str(rawquery: &str) -> Query {
+pure fn query_from_str(rawquery: &str) -> Query {
     let mut query: Query = ~[];
     if str::len(rawquery) != 0 {
         for str::split_char(rawquery, '&').each |p| {
             let (k, v) = split_char_first(*p, '=');
-            query.push((decode_component(k), decode_component(v)));
+            unsafe {query.push((decode_component(k), decode_component(v)));}
         };
     }
     return query;
@@ -340,7 +346,7 @@ pub pure fn query_to_str(query: Query) -> ~str {
 }
 
 // returns the scheme and the rest of the url, or a parsing error
-pub fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> {
+pub pure fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> {
     for str::each_chari(rawurl) |i,c| {
         match c {
           'A' .. 'Z' | 'a' .. 'z' => loop,
@@ -387,7 +393,7 @@ impl Input : Eq {
 }
 
 // returns userinfo, host, port, and unparsed part, or an error
-fn get_authority(rawurl: &str) ->
+pure fn get_authority(rawurl: &str) ->
     result::Result<(Option<UserInfo>, ~str, Option<~str>, ~str), @~str> {
     if !str::starts_with(rawurl, ~"//") {
         // there is no authority.
@@ -517,7 +523,7 @@ fn get_authority(rawurl: &str) ->
 
     let end = end; // make end immutable so it can be captured
 
-    let host_is_end_plus_one: &fn() -> bool = || {
+    let host_is_end_plus_one: &pure fn() -> bool = || {
         end+1 == len
             && !['?', '#', '/'].contains(&(rawurl[end] as char))
     };
@@ -556,7 +562,7 @@ fn get_authority(rawurl: &str) ->
 
 
 // returns the path and unparsed part of url, or an error
-fn get_path(rawurl: &str, authority : bool) ->
+pure fn get_path(rawurl: &str, authority : bool) ->
     result::Result<(~str, ~str), @~str> {
     let len = str::len(rawurl);
     let mut end = len;
@@ -587,7 +593,7 @@ fn get_path(rawurl: &str, authority : bool) ->
 }
 
 // returns the parsed query and the fragment, if present
-fn get_query_fragment(rawurl: &str) ->
+pure fn get_query_fragment(rawurl: &str) ->
     result::Result<(Query, Option<~str>), @~str> {
     if !str::starts_with(rawurl, ~"?") {
         if str::starts_with(rawurl, ~"#") {
@@ -619,20 +625,20 @@ fn get_query_fragment(rawurl: &str) ->
  *
  */
 
-pub fn from_str(rawurl: &str) -> result::Result<Url, ~str> {
+pub pure fn from_str(rawurl: &str) -> result::Result<Url, ~str> {
     // scheme
     let mut schm = get_scheme(rawurl);
     if result::is_err(&schm) {
         return result::Err(copy *result::get_err(&schm));
     }
-    let (scheme, rest) = result::unwrap(schm);
+    let (scheme, rest) = schm.get();
 
     // authority
     let mut auth = get_authority(rest);
     if result::is_err(&auth) {
         return result::Err(copy *result::get_err(&auth));
     }
-    let (userinfo, host, port, rest) = result::unwrap(auth);
+    let (userinfo, host, port, rest) = auth.get();
 
     // path
     let has_authority = if host == ~"" { false } else { true };
@@ -640,21 +646,21 @@ pub fn from_str(rawurl: &str) -> result::Result<Url, ~str> {
     if result::is_err(&pth) {
         return result::Err(copy *result::get_err(&pth));
     }
-    let (path, rest) = result::unwrap(pth);
+    let (path, rest) = pth.get();
 
     // query and fragment
     let mut qry = get_query_fragment(rest);
     if result::is_err(&qry) {
         return result::Err(copy *result::get_err(&qry));
     }
-    let (query, fragment) = result::unwrap(qry);
+    let (query, fragment) = qry.get();
 
     return result::Ok(Url(scheme, userinfo, host,
                        port, path, query, fragment));
 }
 
 impl Url : FromStr {
-    static fn from_str(s: &str) -> Option<Url> {
+    static pure fn from_str(s: &str) -> Option<Url> {
         match from_str(s) {
             Ok(move url) => Some(url),
             Err(_) => None