diff options
| author | Jesse Jones <jesse9jones@gmail.com> | 2012-11-17 10:13:11 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-11-18 13:25:26 -0800 |
| commit | 6d99a2f8a99baedab90fb8bd6cb6e1507fad736b (patch) | |
| tree | 543df1adb01550cf549166c515312cf04c168b24 /src/libstd | |
| parent | 15989ecb139a4ee4a53e6657fd4fc8cee9d729de (diff) | |
| download | rust-6d99a2f8a99baedab90fb8bd6cb6e1507fad736b.tar.gz rust-6d99a2f8a99baedab90fb8bd6cb6e1507fad736b.zip | |
Made more stuff pure.
escape functions in char, io.with_str_reader, base64 and md5sum, cell.empty_cell and is_empty.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/base64.rs | 174 | ||||
| -rw-r--r-- | src/libstd/cell.rs | 4 | ||||
| -rw-r--r-- | src/libstd/md4.rs | 14 |
3 files changed, 97 insertions, 95 deletions
diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs index 9bad4d39750..177074de8f6 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -2,72 +2,73 @@ use io::Reader; pub trait ToBase64 { - fn to_base64() -> ~str; + pure fn to_base64() -> ~str; } impl &[u8]: ToBase64 { - fn to_base64() -> ~str { + pure fn to_base64() -> ~str { let chars = str::chars( ~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ); - let len = self.len(); let mut s = ~""; - str::reserve(&mut s, ((len + 3u) / 4u) * 3u); - - let mut i = 0u; - - while i < len - (len % 3u) { - let n = (self[i] as uint) << 16u | - (self[i + 1u] as uint) << 8u | - (self[i + 2u] as uint); - - // This 24-bit number gets separated into four 6-bit numbers. - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, chars[(n >> 6u) & 63u]); - str::push_char(&mut s, chars[n & 63u]); - - i += 3u; - } - - // Heh, would be cool if we knew this was exhaustive - // (the dream of bounded integer types) - match len % 3 { - 0 => (), - 1 => { - let n = (self[i] as uint) << 16u; - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, '='); - str::push_char(&mut s, '='); - } - 2 => { - let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; - str::push_char(&mut s, chars[(n >> 18u) & 63u]); - str::push_char(&mut s, chars[(n >> 12u) & 63u]); - str::push_char(&mut s, chars[(n >> 6u) & 63u]); - str::push_char(&mut s, '='); - } - _ => fail ~"Algebra is broken, please alert the math police" + unsafe { + let len = self.len(); + str::reserve(&mut s, ((len + 3u) / 4u) * 3u); + + let mut i = 0u; + + while i < len - (len % 3u) { + let n = (self[i] as uint) << 16u | + (self[i + 1u] as uint) << 8u | + (self[i + 2u] as uint); + + // This 24-bit number gets separated into four 6-bit numbers. + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, chars[(n >> 6u) & 63u]); + str::push_char(&mut s, chars[n & 63u]); + + i += 3u; + } + + // Heh, would be cool if we knew this was exhaustive + // (the dream of bounded integer types) + match len % 3 { + 0 => (), + 1 => { + let n = (self[i] as uint) << 16u; + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, '='); + str::push_char(&mut s, '='); + } + 2 => { + let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u; + str::push_char(&mut s, chars[(n >> 18u) & 63u]); + str::push_char(&mut s, chars[(n >> 12u) & 63u]); + str::push_char(&mut s, chars[(n >> 6u) & 63u]); + str::push_char(&mut s, '='); + } + _ => fail ~"Algebra is broken, please alert the math police" + } } - s } } impl &str: ToBase64 { - fn to_base64() -> ~str { + pure fn to_base64() -> ~str { str::to_bytes(self).to_base64() } } pub trait FromBase64 { - fn from_base64() -> ~[u8]; + pure fn from_base64() -> ~[u8]; } impl ~[u8]: FromBase64 { - fn from_base64() -> ~[u8] { + pure fn from_base64() -> ~[u8] { if self.len() % 4u != 0u { fail ~"invalid base64 length"; } let len = self.len(); @@ -80,55 +81,56 @@ impl ~[u8]: FromBase64 { let mut r = vec::with_capacity((len / 4u) * 3u - padding); - let mut i = 0u; - while i < len { - let mut n = 0u; - - for iter::repeat(4u) { - let ch = self[i] as char; - n <<= 6u; - - if ch >= 'A' && ch <= 'Z' { - n |= (ch as uint) - 0x41u; - } else if ch >= 'a' && ch <= 'z' { - n |= (ch as uint) - 0x47u; - } else if ch >= '0' && ch <= '9' { - n |= (ch as uint) + 0x04u; - } else if ch == '+' { - n |= 0x3Eu; - } else if ch == '/' { - n |= 0x3Fu; - } else if ch == '=' { - match len - i { - 1u => { - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - return copy r; - } - 2u => { - r.push(((n >> 10u) & 0xFFu) as u8); - return copy r; - } - _ => fail ~"invalid base64 padding" + unsafe { + let mut i = 0u; + while i < len { + let mut n = 0u; + + for iter::repeat(4u) { + let ch = self[i] as char; + n <<= 6u; + + if ch >= 'A' && ch <= 'Z' { + n |= (ch as uint) - 0x41u; + } else if ch >= 'a' && ch <= 'z' { + n |= (ch as uint) - 0x47u; + } else if ch >= '0' && ch <= '9' { + n |= (ch as uint) + 0x04u; + } else if ch == '+' { + n |= 0x3Eu; + } else if ch == '/' { + n |= 0x3Fu; + } else if ch == '=' { + match len - i { + 1u => { + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + return copy r; + } + 2u => { + r.push(((n >> 10u) & 0xFFu) as u8); + return copy r; + } + _ => fail ~"invalid base64 padding" + } + } else { + fail ~"invalid base64 character"; } - } else { - fail ~"invalid base64 character"; - } - - i += 1u; - }; - - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - r.push(((n ) & 0xFFu) as u8); + + i += 1u; + }; + + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + r.push(((n ) & 0xFFu) as u8); + } } - r } } impl ~str: FromBase64 { - fn from_base64() -> ~[u8] { + pure fn from_base64() -> ~[u8] { str::to_bytes(self).from_base64() } } diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 4f79bf2b316..78027aa8907 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -12,7 +12,7 @@ pub fn Cell<T>(value: T) -> Cell<T> { Cell { value: Some(move value) } } -pub fn empty_cell<T>() -> Cell<T> { +pub pure fn empty_cell<T>() -> Cell<T> { Cell { value: None } } @@ -37,7 +37,7 @@ impl<T> Cell<T> { } /// Returns true if the cell is empty and false if the cell is full. - fn is_empty() -> bool { + pure fn is_empty() -> bool { self.value.is_none() } diff --git a/src/libstd/md4.rs b/src/libstd/md4.rs index 581beb78bdc..d9bc03c311d 100644 --- a/src/libstd/md4.rs +++ b/src/libstd/md4.rs @@ -1,6 +1,6 @@ #[forbid(deprecated_mode)]; -pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { +pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { // subtle: if orig_len is merely uint, then the code below // which performs shifts by 32 bits or more has undefined // results. @@ -10,14 +10,14 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]); let mut bitlen = orig_len + 8u64; while (bitlen + 64u64) % 512u64 > 0u64 { - msg.push(0u8); + unsafe {msg.push(0u8);} bitlen += 8u64; } // append length let mut i = 0u64; while i < 8u64 { - msg.push((orig_len >> (i * 8u64)) as u8); + unsafe {msg.push((orig_len >> (i * 8u64)) as u8);} i += 1u64; } @@ -26,7 +26,7 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { let mut c = 0x98badcfeu32; let mut d = 0x10325476u32; - fn rot(r: int, x: u32) -> u32 { + pure fn rot(r: int, x: u32) -> u32 { let r = r as u32; (x << r) | (x >> (32u32 - r)) } @@ -84,9 +84,9 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} { return {a: a, b: b, c: c, d: d}; } -pub fn md4_str(msg: &[u8]) -> ~str { +pub pure fn md4_str(msg: &[u8]) -> ~str { let {a, b, c, d} = md4(msg); - fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { + pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) { f(a); f(b); f(c); f(d); } let mut result = ~""; @@ -102,7 +102,7 @@ pub fn md4_str(msg: &[u8]) -> ~str { result } -pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) } +pub pure fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) } #[test] fn test_md4() { |
