From 255193cc1af5e07753906ad18bae077b45b5c3f0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 8 Apr 2013 16:50:34 -0400 Subject: Removing no longer needed unsafe blocks --- src/libstd/base64.rs | 158 ++++++++++++++++---------------- src/libstd/dlist.rs | 9 +- src/libstd/json.rs | 35 ++++---- src/libstd/md4.rs | 4 +- src/libstd/num/bigint.rs | 2 +- src/libstd/sha1.rs | 230 +++++++++++++++++++++++------------------------ src/libstd/sort.rs | 30 +++---- src/libstd/time.rs | 8 +- 8 files changed, 225 insertions(+), 251 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs index 0266f2d8631..781a720b1a4 100644 --- a/src/libstd/base64.rs +++ b/src/libstd/base64.rs @@ -29,47 +29,45 @@ static CHARS: [char, ..64] = [ impl<'self> ToBase64 for &'self [u8] { fn to_base64(&self) -> ~str { let mut s = ~""; - 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") - } + 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 } @@ -99,49 +97,47 @@ impl FromBase64 for ~[u8] { let mut r = vec::with_capacity((len / 4u) * 3u - 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"); + 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"); + } - i += 1u; - }; + i += 1u; + }; - r.push(((n >> 16u) & 0xFFu) as u8); - r.push(((n >> 8u ) & 0xFFu) as u8); - r.push(((n ) & 0xFFu) as u8); - } + r.push(((n >> 16u) & 0xFFu) as u8); + r.push(((n >> 8u ) & 0xFFu) as u8); + r.push(((n ) & 0xFFu) as u8); } r } diff --git a/src/libstd/dlist.rs b/src/libstd/dlist.rs index e7701974097..a490065b835 100644 --- a/src/libstd/dlist.rs +++ b/src/libstd/dlist.rs @@ -99,7 +99,7 @@ pub fn DList() -> @mut DList { /// Creates a new dlist with a single element pub fn from_elem(data: T) -> @mut DList { let list = DList(); - unsafe { list.push(data); } + list.push(data); list } @@ -484,11 +484,8 @@ pub impl DList { /// Get the elements of the list as a vector. O(n). fn to_vec(@mut self) -> ~[T] { let mut v = vec::with_capacity(self.size); - unsafe { - // Take this out of the unchecked when iter's functions are pure - for iter::eachi(&self) |index,data| { - v[index] = *data; - } + for iter::eachi(&self) |index,data| { + v[index] = *data; } v } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 5a2bfd2113b..d733a60f34f 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -342,10 +342,7 @@ pub fn to_writer(wr: @io::Writer, json: &Json) { /// Encodes a json value into a string pub fn to_str(json: &Json) -> ~str { - unsafe { - // ugh, should be safe - io::with_str_writer(|wr| to_writer(wr, json)) - } + io::with_str_writer(|wr| to_writer(wr, json)) } /// Encodes a json value into a io::writer @@ -988,23 +985,21 @@ impl Ord for Json { match *other { Number(_) | String(_) | Boolean(_) | List(_) => false, Object(ref d1) => { - unsafe { - let mut d0_flat = ~[]; - let mut d1_flat = ~[]; - - // FIXME #4430: this is horribly inefficient... - for d0.each |&(k, v)| { - d0_flat.push((@copy *k, @copy *v)); - } - d0_flat.qsort(); - - for d1.each |&(k, v)| { - d1_flat.push((@copy *k, @copy *v)); - } - d1_flat.qsort(); - - d0_flat < d1_flat + let mut d0_flat = ~[]; + let mut d1_flat = ~[]; + + // FIXME #4430: this is horribly inefficient... + for d0.each |&(k, v)| { + d0_flat.push((@copy *k, @copy *v)); } + d0_flat.qsort(); + + for d1.each |&(k, v)| { + d1_flat.push((@copy *k, @copy *v)); + } + d1_flat.qsort(); + + d0_flat < d1_flat } Null => true } diff --git a/src/libstd/md4.rs b/src/libstd/md4.rs index 8f35376a6f1..24dd08c362e 100644 --- a/src/libstd/md4.rs +++ b/src/libstd/md4.rs @@ -29,14 +29,14 @@ pub fn md4(msg: &[u8]) -> Quad { let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]); let mut bitlen = orig_len + 8u64; while (bitlen + 64u64) % 512u64 > 0u64 { - unsafe {msg.push(0u8);} + msg.push(0u8); bitlen += 8u64; } // append length let mut i = 0u64; while i < 8u64 { - unsafe {msg.push((orig_len >> (i * 8u64)) as u8);} + msg.push((orig_len >> (i * 8u64)) as u8); i += 1u64; } diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index f15632b1431..ec5d2cded8d 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -341,7 +341,7 @@ pub impl BigUint { if new_len == v.len() { return BigUint { data: v }; } let mut v = v; - unsafe { v.truncate(new_len); } + v.truncate(new_len); return BigUint { data: v }; } diff --git a/src/libstd/sha1.rs b/src/libstd/sha1.rs index 1a2d4a87d98..f5f7f5e326a 100644 --- a/src/libstd/sha1.rs +++ b/src/libstd/sha1.rs @@ -283,134 +283,132 @@ mod tests { #[test] pub fn test() { - unsafe { - struct Test { - input: ~str, - output: ~[u8], - output_str: ~str, - } + struct Test { + input: ~str, + output: ~[u8], + output_str: ~str, + } - fn a_million_letter_a() -> ~str { - let mut i = 0; - let mut rs = ~""; - while i < 100000 { - str::push_str(&mut rs, ~"aaaaaaaaaa"); - i += 1; - } - return rs; + fn a_million_letter_a() -> ~str { + let mut i = 0; + let mut rs = ~""; + while i < 100000 { + str::push_str(&mut rs, ~"aaaaaaaaaa"); + i += 1; } - // Test messages from FIPS 180-1 + return rs; + } + // Test messages from FIPS 180-1 - let fips_180_1_tests = ~[ - Test { - input: ~"abc", - output: ~[ - 0xA9u8, 0x99u8, 0x3Eu8, 0x36u8, - 0x47u8, 0x06u8, 0x81u8, 0x6Au8, - 0xBAu8, 0x3Eu8, 0x25u8, 0x71u8, - 0x78u8, 0x50u8, 0xC2u8, 0x6Cu8, - 0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8, - ], - output_str: ~"a9993e364706816aba3e25717850c26c9cd0d89d" - }, - Test { - input: - ~"abcdbcdecdefdefgefghfghighij" + - ~"hijkijkljklmklmnlmnomnopnopq", - output: ~[ - 0x84u8, 0x98u8, 0x3Eu8, 0x44u8, - 0x1Cu8, 0x3Bu8, 0xD2u8, 0x6Eu8, - 0xBAu8, 0xAEu8, 0x4Au8, 0xA1u8, - 0xF9u8, 0x51u8, 0x29u8, 0xE5u8, - 0xE5u8, 0x46u8, 0x70u8, 0xF1u8, - ], - output_str: ~"84983e441c3bd26ebaae4aa1f95129e5e54670f1" - }, - Test { - input: a_million_letter_a(), - output: ~[ - 0x34u8, 0xAAu8, 0x97u8, 0x3Cu8, - 0xD4u8, 0xC4u8, 0xDAu8, 0xA4u8, - 0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8, - 0xDBu8, 0xADu8, 0x27u8, 0x31u8, - 0x65u8, 0x34u8, 0x01u8, 0x6Fu8, - ], - output_str: ~"34aa973cd4c4daa4f61eeb2bdbad27316534016f" - }, - ]; - // Examples from wikipedia + let fips_180_1_tests = ~[ + Test { + input: ~"abc", + output: ~[ + 0xA9u8, 0x99u8, 0x3Eu8, 0x36u8, + 0x47u8, 0x06u8, 0x81u8, 0x6Au8, + 0xBAu8, 0x3Eu8, 0x25u8, 0x71u8, + 0x78u8, 0x50u8, 0xC2u8, 0x6Cu8, + 0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8, + ], + output_str: ~"a9993e364706816aba3e25717850c26c9cd0d89d" + }, + Test { + input: + ~"abcdbcdecdefdefgefghfghighij" + + ~"hijkijkljklmklmnlmnomnopnopq", + output: ~[ + 0x84u8, 0x98u8, 0x3Eu8, 0x44u8, + 0x1Cu8, 0x3Bu8, 0xD2u8, 0x6Eu8, + 0xBAu8, 0xAEu8, 0x4Au8, 0xA1u8, + 0xF9u8, 0x51u8, 0x29u8, 0xE5u8, + 0xE5u8, 0x46u8, 0x70u8, 0xF1u8, + ], + output_str: ~"84983e441c3bd26ebaae4aa1f95129e5e54670f1" + }, + Test { + input: a_million_letter_a(), + output: ~[ + 0x34u8, 0xAAu8, 0x97u8, 0x3Cu8, + 0xD4u8, 0xC4u8, 0xDAu8, 0xA4u8, + 0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8, + 0xDBu8, 0xADu8, 0x27u8, 0x31u8, + 0x65u8, 0x34u8, 0x01u8, 0x6Fu8, + ], + output_str: ~"34aa973cd4c4daa4f61eeb2bdbad27316534016f" + }, + ]; + // Examples from wikipedia - let wikipedia_tests = ~[ - Test { - input: ~"The quick brown fox jumps over the lazy dog", - output: ~[ - 0x2fu8, 0xd4u8, 0xe1u8, 0xc6u8, - 0x7au8, 0x2du8, 0x28u8, 0xfcu8, - 0xedu8, 0x84u8, 0x9eu8, 0xe1u8, - 0xbbu8, 0x76u8, 0xe7u8, 0x39u8, - 0x1bu8, 0x93u8, 0xebu8, 0x12u8, - ], - output_str: ~"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", - }, - Test { - input: ~"The quick brown fox jumps over the lazy cog", - output: ~[ - 0xdeu8, 0x9fu8, 0x2cu8, 0x7fu8, - 0xd2u8, 0x5eu8, 0x1bu8, 0x3au8, - 0xfau8, 0xd3u8, 0xe8u8, 0x5au8, - 0x0bu8, 0xd1u8, 0x7du8, 0x9bu8, - 0x10u8, 0x0du8, 0xb4u8, 0xb3u8, - ], - output_str: ~"de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3", - }, - ]; - let tests = fips_180_1_tests + wikipedia_tests; - fn check_vec_eq(v0: ~[u8], v1: ~[u8]) { - assert!((vec::len::(v0) == vec::len::(v1))); - let len = vec::len::(v0); - let mut i = 0u; - while i < len { - let a = v0[i]; - let b = v1[i]; - assert!((a == b)); - i += 1u; - } + let wikipedia_tests = ~[ + Test { + input: ~"The quick brown fox jumps over the lazy dog", + output: ~[ + 0x2fu8, 0xd4u8, 0xe1u8, 0xc6u8, + 0x7au8, 0x2du8, 0x28u8, 0xfcu8, + 0xedu8, 0x84u8, 0x9eu8, 0xe1u8, + 0xbbu8, 0x76u8, 0xe7u8, 0x39u8, + 0x1bu8, 0x93u8, 0xebu8, 0x12u8, + ], + output_str: ~"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", + }, + Test { + input: ~"The quick brown fox jumps over the lazy cog", + output: ~[ + 0xdeu8, 0x9fu8, 0x2cu8, 0x7fu8, + 0xd2u8, 0x5eu8, 0x1bu8, 0x3au8, + 0xfau8, 0xd3u8, 0xe8u8, 0x5au8, + 0x0bu8, 0xd1u8, 0x7du8, 0x9bu8, + 0x10u8, 0x0du8, 0xb4u8, 0xb3u8, + ], + output_str: ~"de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3", + }, + ]; + let tests = fips_180_1_tests + wikipedia_tests; + fn check_vec_eq(v0: ~[u8], v1: ~[u8]) { + assert!((vec::len::(v0) == vec::len::(v1))); + let len = vec::len::(v0); + let mut i = 0u; + while i < len { + let a = v0[i]; + let b = v1[i]; + assert!((a == b)); + i += 1u; } - // Test that it works when accepting the message all at once + } + // Test that it works when accepting the message all at once - let mut sh = sha1::sha1(); - for vec::each(tests) |t| { - sh.input_str(t.input); - let out = sh.result(); - check_vec_eq(t.output, out); + let mut sh = sha1::sha1(); + for vec::each(tests) |t| { + sh.input_str(t.input); + let out = sh.result(); + check_vec_eq(t.output, out); - let out_str = sh.result_str(); - assert!((out_str.len() == 40)); - assert!((out_str == t.output_str)); + let out_str = sh.result_str(); + assert!((out_str.len() == 40)); + assert!((out_str == t.output_str)); - sh.reset(); - } + sh.reset(); + } - // Test that it works when accepting the message in pieces - for vec::each(tests) |t| { - let len = str::len(t.input); - let mut left = len; - while left > 0u { - let take = (left + 1u) / 2u; - sh.input_str(str::slice(t.input, len - left, - take + len - left).to_owned()); - left = left - take; - } - let out = sh.result(); - check_vec_eq(t.output, out); + // Test that it works when accepting the message in pieces + for vec::each(tests) |t| { + let len = str::len(t.input); + let mut left = len; + while left > 0u { + let take = (left + 1u) / 2u; + sh.input_str(str::slice(t.input, len - left, + take + len - left).to_owned()); + left = left - take; + } + let out = sh.result(); + check_vec_eq(t.output, out); - let out_str = sh.result_str(); - assert!((out_str.len() == 40)); - assert!((out_str == t.output_str)); + let out_str = sh.result_str(); + assert!((out_str.len() == 40)); + assert!((out_str == t.output_str)); - sh.reset(); - } + sh.reset(); } } } diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index 40a12895175..39ca9bb5ba6 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -27,7 +27,7 @@ type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool; pub fn merge_sort(v: &const [T], le: Le) -> ~[T] { type Slice = (uint, uint); - unsafe {return merge_sort_(v, (0u, len(v)), le);} + return merge_sort_(v, (0u, len(v)), le); fn merge_sort_(v: &const [T], slice: Slice, le: Le) -> ~[T] { @@ -68,14 +68,11 @@ fn part(arr: &mut [T], left: uint, let mut storage_index: uint = left; let mut i: uint = left; while i < right { - // XXX: Unsafe because borrow check doesn't handle this right - unsafe { - let a: &T = cast::transmute(&mut arr[i]); - let b: &T = cast::transmute(&mut arr[right]); - if compare_func(a, b) { - arr[i] <-> arr[storage_index]; - storage_index += 1; - } + let a: &mut T = &mut arr[i]; + let b: &mut T = &mut arr[right]; + if compare_func(a, b) { + arr[i] <-> arr[storage_index]; + storage_index += 1; } i += 1; } @@ -888,12 +885,9 @@ mod tests { // tjc: funny that we have to use parens fn ile(x: &(&'static str), y: &(&'static str)) -> bool { - unsafe // to_lower is not pure... - { - let x = x.to_lower(); - let y = y.to_lower(); - x <= y - } + let x = x.to_lower(); + let y = y.to_lower(); + x <= y } let names1 = ~["joe bob", "Joe Bob", "Jack Brown", "JOE Bob", @@ -921,10 +915,8 @@ mod test_tim_sort { impl Ord for CVal { fn lt(&self, other: &CVal) -> bool { - unsafe { - let rng = rand::Rng(); - if rng.gen_float() > 0.995 { fail!(~"It's happening!!!"); } - } + let rng = rand::Rng(); + if rng.gen_float() > 0.995 { fail!(~"It's happening!!!"); } (*self).val < other.val } fn le(&self, other: &CVal) -> bool { (*self).val <= other.val } diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 3af193e8748..adfa12594aa 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -176,16 +176,12 @@ pub fn now() -> Tm { /// Parses the time from the string according to the format string. pub fn strptime(s: &str, format: &str) -> Result { - // unsafe only because do_strptime is annoying to make pure - // (it does IO with a str_reader) - unsafe {do_strptime(s, format)} + do_strptime(s, format) } /// Formats the time according to the format string. pub fn strftime(format: &str, tm: &Tm) -> ~str { - // unsafe only because do_strftime is annoying to make pure - // (it does IO with a str_reader) - unsafe { do_strftime(format, tm) } + do_strftime(format, tm) } pub impl Tm { -- cgit 1.4.1-3-g733a5