Skip to content

Removing some mutable fields and unsafe blocks #5787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,10 @@ pub fn escape_unicode(c: char) -> ~str {
else { ('U', 8u) });
assert!(str::len(s) <= pad);
let mut out = ~"\\";
unsafe {
str::push_str(&mut out, str::from_char(c));
for uint::range(str::len(s), pad) |_i|
{ str::push_str(&mut out, ~"0"); }
str::push_str(&mut out, s);
}
str::push_str(&mut out, str::from_char(c));
for uint::range(str::len(s), pad) |_i|
{ str::push_str(&mut out, ~"0"); }
str::push_str(&mut out, s);
out
}

Expand Down
62 changes: 26 additions & 36 deletions src/libcore/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,51 +76,43 @@ pub trait Streaming {
impl<A:IterBytes> Hash for A {
#[inline(always)]
fn hash_keyed(&self, k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for self.iter_bytes(true) |bytes| {
s.input(bytes);
}
s.result_u64()
let s = &State(k0, k1);
for self.iter_bytes(true) |bytes| {
s.input(bytes);
}
s.result_u64()
}
}

fn hash_keyed_2<A: IterBytes,
B: IterBytes>(a: &A, b: &B, k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}

fn hash_keyed_3<A: IterBytes,
B: IterBytes,
C: IterBytes>(a: &A, b: &B, c: &C, k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}

fn hash_keyed_4<A: IterBytes,
B: IterBytes,
C: IterBytes,
D: IterBytes>(a: &A, b: &B, c: &C, d: &D, k0: u64, k1: u64)
-> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
for d.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
for d.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}

fn hash_keyed_5<A: IterBytes,
Expand All @@ -129,15 +121,13 @@ fn hash_keyed_5<A: IterBytes,
D: IterBytes,
E: IterBytes>(a: &A, b: &B, c: &C, d: &D, e: &E,
k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
for d.iter_bytes(true) |bytes| { s.input(bytes); }
for e.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
for d.iter_bytes(true) |bytes| { s.input(bytes); }
for e.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}

// Implement State as SipState
Expand Down
80 changes: 33 additions & 47 deletions src/libcore/num/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,8 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
deccum /= radix_gen;
deccum = deccum.round_to_zero();

unsafe { // FIXME: Pureness workaround (#4568)
buf.push(char::from_digit(current_digit.to_int() as uint, radix)
.unwrap() as u8);
}
buf.push(char::from_digit(current_digit.to_int() as uint, radix)
.unwrap() as u8);

// No more digits to calculate for the non-fractional part -> break
if deccum == _0 { break; }
Expand All @@ -247,21 +245,15 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
// Decide what sign to put in front
match sign {
SignNeg | SignAll if neg => {
unsafe { // FIXME: Pureness workaround (#4568)
buf.push('-' as u8);
}
buf.push('-' as u8);
}
SignAll => {
unsafe { // FIXME: Pureness workaround (#4568)
buf.push('+' as u8);
}
buf.push('+' as u8);
}
_ => ()
}

unsafe { // FIXME: Pureness workaround (#4568)
vec::reverse(buf);
}
vec::reverse(buf);

// Remember start of the fractional digits.
// Points one beyond end of buf if none get generated,
Expand All @@ -271,9 +263,7 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
// Now emit the fractional part, if any
deccum = num.fractional_part();
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
unsafe { // FIXME: Pureness workaround (#4568)
buf.push('.' as u8);
}
buf.push('.' as u8);
let mut dig = 0u;

// calculate new digits while
Expand All @@ -299,10 +289,8 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
current_digit_signed
};

unsafe { // FIXME: Pureness workaround (#4568)
buf.push(char::from_digit(
current_digit.to_int() as uint, radix).unwrap() as u8);
}
buf.push(char::from_digit(
current_digit.to_int() as uint, radix).unwrap() as u8);

// Decrease the deccumulator one fractional digit at a time
deccum = deccum.fractional_part();
Expand All @@ -320,33 +308,31 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
char::from_digit(val, radix).unwrap() as u8
};

unsafe { // FIXME: Pureness workaround (#4568)
let extra_digit = ascii2value(buf.pop());
if extra_digit >= radix / 2 { // -> need to round
let mut i: int = buf.len() as int - 1;
loop {
// If reached left end of number, have to
// insert additional digit:
if i < 0
|| buf[i] == '-' as u8
|| buf[i] == '+' as u8 {
buf.insert((i + 1) as uint, value2ascii(1));
break;
}

// Skip the '.'
if buf[i] == '.' as u8 { i -= 1; loop; }

// Either increment the digit,
// or set to 0 if max and carry the 1.
let current_digit = ascii2value(buf[i]);
if current_digit < (radix - 1) {
buf[i] = value2ascii(current_digit+1);
break;
} else {
buf[i] = value2ascii(0);
i -= 1;
}
let extra_digit = ascii2value(buf.pop());
if extra_digit >= radix / 2 { // -> need to round
let mut i: int = buf.len() as int - 1;
loop {
// If reached left end of number, have to
// insert additional digit:
if i < 0
|| buf[i] == '-' as u8
|| buf[i] == '+' as u8 {
buf.insert((i + 1) as uint, value2ascii(1));
break;
}

// Skip the '.'
if buf[i] == '.' as u8 { i -= 1; loop; }

// Either increment the digit,
// or set to 0 if max and carry the 1.
let current_digit = ascii2value(buf[i]);
if current_digit < (radix - 1) {
buf[i] = value2ascii(current_digit+1);
break;
} else {
buf[i] = value2ascii(0);
i -= 1;
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ pub enum Result<T, U> {
pub fn get<T:Copy,U>(res: &Result<T, U>) -> T {
match *res {
Ok(copy t) => t,
Err(ref the_err) => unsafe {
Err(ref the_err) =>
fail!(fmt!("get called on error result: %?", *the_err))
}
}
}

Expand All @@ -56,9 +55,8 @@ pub fn get<T:Copy,U>(res: &Result<T, U>) -> T {
pub fn get_ref<'a, T, U>(res: &'a Result<T, U>) -> &'a T {
match *res {
Ok(ref t) => t,
Err(ref the_err) => unsafe {
Err(ref the_err) =>
fail!(fmt!("get_ref called on error result: %?", *the_err))
}
}
}

Expand Down
58 changes: 24 additions & 34 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,11 +1020,9 @@ pub fn any(ss: &str, pred: &fn(char) -> bool) -> bool {
/// Apply a function to each character
pub fn map(ss: &str, ff: &fn(char) -> char) -> ~str {
let mut result = ~"";
unsafe {
reserve(&mut result, len(ss));
for ss.each_char |cc| {
str::push_char(&mut result, ff(cc));
}
reserve(&mut result, len(ss));
for ss.each_char |cc| {
str::push_char(&mut result, ff(cc));
}
result
}
Expand Down Expand Up @@ -1660,20 +1658,18 @@ pub fn to_utf16(s: &str) -> ~[u16] {
// Arithmetic with u32 literals is easier on the eyes than chars.
let mut ch = ch as u32;

unsafe {
if (ch & 0xFFFF_u32) == ch {
// The BMP falls through (assuming non-surrogate, as it
// should)
assert!(ch <= 0xD7FF_u32 || ch >= 0xE000_u32);
u.push(ch as u16)
} else {
// Supplementary planes break into surrogates.
assert!(ch >= 0x1_0000_u32 && ch <= 0x10_FFFF_u32);
ch -= 0x1_0000_u32;
let w1 = 0xD800_u16 | ((ch >> 10) as u16);
let w2 = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
u.push_all(~[w1, w2])
}
if (ch & 0xFFFF_u32) == ch {
// The BMP falls through (assuming non-surrogate, as it
// should)
assert!(ch <= 0xD7FF_u32 || ch >= 0xE000_u32);
u.push(ch as u16)
} else {
// Supplementary planes break into surrogates.
assert!(ch >= 0x1_0000_u32 && ch <= 0x10_FFFF_u32);
ch -= 0x1_0000_u32;
let w1 = 0xD800_u16 | ((ch >> 10) as u16);
let w2 = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
u.push_all(~[w1, w2])
}
}
u
Expand Down Expand Up @@ -1705,16 +1701,14 @@ pub fn utf16_chars(v: &[u16], f: &fn(char)) {

pub fn from_utf16(v: &[u16]) -> ~str {
let mut buf = ~"";
unsafe {
reserve(&mut buf, vec::len(v));
utf16_chars(v, |ch| push_char(&mut buf, ch));
}
reserve(&mut buf, vec::len(v));
utf16_chars(v, |ch| push_char(&mut buf, ch));
buf
}

pub fn with_capacity(capacity: uint) -> ~str {
let mut buf = ~"";
unsafe { reserve(&mut buf, capacity); }
reserve(&mut buf, capacity);
buf
}

Expand Down Expand Up @@ -2105,23 +2099,19 @@ pub fn capacity(s: &const ~str) -> uint {
/// Escape each char in `s` with char::escape_default.
pub fn escape_default(s: &str) -> ~str {
let mut out: ~str = ~"";
unsafe {
reserve_at_least(&mut out, str::len(s));
for s.each_char |c| {
push_str(&mut out, char::escape_default(c));
}
reserve_at_least(&mut out, str::len(s));
for s.each_char |c| {
push_str(&mut out, char::escape_default(c));
}
out
}

/// Escape each char in `s` with char::escape_unicode.
pub fn escape_unicode(s: &str) -> ~str {
let mut out: ~str = ~"";
unsafe {
reserve_at_least(&mut out, str::len(s));
for s.each_char |c| {
push_str(&mut out, char::escape_unicode(c));
}
reserve_at_least(&mut out, str::len(s));
for s.each_char |c| {
push_str(&mut out, char::escape_unicode(c));
}
out
}
Expand Down
Loading