diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-10-11 14:12:50 -0700 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-10-11 14:17:59 -0700 |
| commit | 5a8ba073bcd6ee6fd34ff545845a746cddc4904f (patch) | |
| tree | ac61c449c7178937914d59a8c6c84bed74a52bd8 /src/libstd | |
| parent | 41bce91cb871ba90caf7d3e56243141dd3390bca (diff) | |
| download | rust-5a8ba073bcd6ee6fd34ff545845a746cddc4904f.tar.gz rust-5a8ba073bcd6ee6fd34ff545845a746cddc4904f.zip | |
Make to_str pure and fix const parameters for str-mutating functions
Two separate changes that got intertwined (sorry): Make to_str pure. Closes #3691 In str, change functions like push_char to take an &mut str instead of an &str. Closes #3710
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/json.rs | 29 | ||||
| -rw-r--r-- | src/libstd/map.rs | 3 | ||||
| -rw-r--r-- | src/libstd/net_url.rs | 20 |
3 files changed, 30 insertions, 22 deletions
diff --git a/src/libstd/json.rs b/src/libstd/json.rs index fa7c0286dc1..bb331240b76 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -51,7 +51,7 @@ fn escape_str(s: &str) -> ~str { fn spaces(n: uint) -> ~str { let mut ss = ~""; - for n.times { str::push_str(&ss, " "); } + for n.times { str::push_str(&mut ss, " "); } return ss; } @@ -302,7 +302,8 @@ pub fn to_writer(wr: io::Writer, json: &Json) { } /// Serializes a json value into a string -pub fn to_str(json: &Json) -> ~str { +pub pure fn to_str(json: &Json) -> ~str unsafe { + // ugh, should be safe io::with_str_writer(|wr| to_writer(wr, json)) } @@ -546,14 +547,14 @@ priv impl Parser { if (escape) { match self.ch { - '"' => str::push_char(&res, '"'), - '\\' => str::push_char(&res, '\\'), - '/' => str::push_char(&res, '/'), - 'b' => str::push_char(&res, '\x08'), - 'f' => str::push_char(&res, '\x0c'), - 'n' => str::push_char(&res, '\n'), - 'r' => str::push_char(&res, '\r'), - 't' => str::push_char(&res, '\t'), + '"' => str::push_char(&mut res, '"'), + '\\' => str::push_char(&mut res, '\\'), + '/' => str::push_char(&mut res, '/'), + 'b' => str::push_char(&mut res, '\x08'), + 'f' => str::push_char(&mut res, '\x0c'), + 'n' => str::push_char(&mut res, '\n'), + 'r' => str::push_char(&mut res, '\r'), + 't' => str::push_char(&mut res, '\t'), 'u' => { // Parse \u1234. let mut i = 0u; @@ -582,7 +583,7 @@ priv impl Parser { ~"invalid \\u escape (not four digits)"); } - str::push_char(&res, n as char); + str::push_char(&mut res, n as char); } _ => return self.error(~"invalid escape") } @@ -594,7 +595,7 @@ priv impl Parser { self.bump(); return Ok(res); } - str::push_char(&res, self.ch); + str::push_char(&mut res, self.ch); } } @@ -1166,11 +1167,11 @@ impl <A: ToJson> Option<A>: ToJson { } impl Json: to_str::ToStr { - fn to_str() -> ~str { to_str(&self) } + pure fn to_str() -> ~str { to_str(&self) } } impl Error: to_str::ToStr { - fn to_str() -> ~str { + pure fn to_str() -> ~str { fmt!("%u:%u: %s", self.line, self.col, *self.msg) } } diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 765d40339d3..9f78f98fa31 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -341,7 +341,8 @@ pub mod chained { wr.write_str(~" }"); } - fn to_str() -> ~str { + pure fn to_str() -> ~str unsafe { + // Meh -- this should be safe do io::with_str_writer |wr| { self.to_writer(wr) } } } diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 0ab4d89f363..c3fd3383979 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -94,7 +94,8 @@ pub fn encode(s: &str) -> ~str { * * This function is compliant with RFC 3986. */ -pub fn encode_component(s: &str) -> ~str { + +fn encode_component(s: &str) -> ~str { encode_inner(s, false) } @@ -297,7 +298,7 @@ fn userinfo_from_str(uinfo: &str) -> UserInfo { return UserInfo(user, pass); } -fn userinfo_to_str(userinfo: UserInfo) -> ~str { +pure fn userinfo_to_str(userinfo: UserInfo) -> ~str { if option::is_some(&userinfo.pass) { return str::concat(~[copy userinfo.user, ~":", option::unwrap(copy userinfo.pass), @@ -325,11 +326,15 @@ fn query_from_str(rawquery: &str) -> Query { return query; } -pub fn query_to_str(query: Query) -> ~str { +pub pure fn query_to_str(query: Query) -> ~str { let mut strvec = ~[]; for query.each |kv| { let (k, v) = copy *kv; - strvec += ~[#fmt("%s=%s", encode_component(k), encode_component(v))]; + // This is really safe... + unsafe { + strvec += ~[#fmt("%s=%s", + encode_component(k), encode_component(v))]; + } }; return str::connect(strvec, ~"&"); } @@ -672,7 +677,7 @@ impl Url : FromStr { * result in just "http://somehost.com". * */ -pub fn to_str(url: Url) -> ~str { +pub pure fn to_str(url: Url) -> ~str { let user = if url.user.is_some() { userinfo_to_str(option::unwrap(copy url.user)) } else { @@ -688,7 +693,8 @@ pub fn to_str(url: Url) -> ~str { } else { str::concat(~[~"?", query_to_str(url.query)]) }; - let fragment = if url.fragment.is_some() { + // ugh, this really is safe + let fragment = if url.fragment.is_some() unsafe { str::concat(~[~"#", encode_component( option::unwrap(copy url.fragment))]) } else { @@ -704,7 +710,7 @@ pub fn to_str(url: Url) -> ~str { } impl Url: to_str::ToStr { - pub fn to_str() -> ~str { + pub pure fn to_str() -> ~str { to_str(self) } } |
