Skip to content

Use byte literals instead of casts #16291

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 11 commits into from
Aug 7, 2014
12 changes: 6 additions & 6 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ mod tests {
109
];
assert_eq!("".as_bytes(), &[]);
assert_eq!("abc".as_bytes(), &['a' as u8, 'b' as u8, 'c' as u8]);
assert_eq!("abc".as_bytes(), b"abc");
assert_eq!("ศไทย中华Việt Nam".as_bytes(), v.as_slice());
}

Expand All @@ -1475,11 +1475,11 @@ mod tests {
fn test_as_ptr() {
let buf = "hello".as_ptr();
unsafe {
assert_eq!(*buf.offset(0), 'h' as u8);
assert_eq!(*buf.offset(1), 'e' as u8);
assert_eq!(*buf.offset(2), 'l' as u8);
assert_eq!(*buf.offset(3), 'l' as u8);
assert_eq!(*buf.offset(4), 'o' as u8);
assert_eq!(*buf.offset(0), b'h');
assert_eq!(*buf.offset(1), b'e');
assert_eq!(*buf.offset(2), b'l');
assert_eq!(*buf.offset(3), b'l');
assert_eq!(*buf.offset(4), b'o');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ mod tests {
fn test_push_bytes() {
let mut s = String::from_str("ABC");
unsafe {
s.push_bytes([ 'D' as u8 ]);
s.push_bytes([b'D']);
}
assert_eq!(s.as_slice(), "ABCD");
}
Expand Down
18 changes: 9 additions & 9 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
// Decide what sign to put in front
match sign {
SignNeg | SignAll if neg => {
buf[end] = '-' as u8;
buf[end] = b'-';
end += 1;
}
SignAll => {
buf[end] = '+' as u8;
buf[end] = b'+';
end += 1;
}
_ => ()
Expand All @@ -218,7 +218,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
// Now emit the fractional part, if any
deccum = num.fract();
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
buf[end] = '.' as u8;
buf[end] = b'.';
end += 1;
let mut dig = 0u;

Expand Down Expand Up @@ -269,8 +269,8 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
// If reached left end of number, have to
// insert additional digit:
if i < 0
|| buf[i as uint] == '-' as u8
|| buf[i as uint] == '+' as u8 {
|| buf[i as uint] == b'-'
|| buf[i as uint] == b'+' {
for j in range(i as uint + 1, end).rev() {
buf[j + 1] = buf[j];
}
Expand All @@ -280,7 +280,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
}

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

// Either increment the digit,
// or set to 0 if max and carry the 1.
Expand All @@ -306,14 +306,14 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
let mut i = buf_max_i;

// discover trailing zeros of fractional part
while i > start_fractional_digits && buf[i] == '0' as u8 {
while i > start_fractional_digits && buf[i] == b'0' {
i -= 1;
}

// Only attempt to truncate digits if buf has fractional digits
if i >= start_fractional_digits {
// If buf ends with '.', cut that too.
if buf[i] == '.' as u8 { i -= 1 }
if buf[i] == b'.' { i -= 1 }

// only resize buf if we actually remove digits
if i < buf_max_i {
Expand All @@ -323,7 +323,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
} // If exact and trailing '.', just cut that
else {
let max_i = end - 1;
if buf[max_i] == '.' as u8 {
if buf[max_i] == b'.' {
end = max_i;
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ macro_rules! radix {
}
}

radix!(Binary, 2, "0b", x @ 0 .. 2 => '0' as u8 + x)
radix!(Octal, 8, "0o", x @ 0 .. 7 => '0' as u8 + x)
radix!(Decimal, 10, "", x @ 0 .. 9 => '0' as u8 + x)
radix!(LowerHex, 16, "0x", x @ 0 .. 9 => '0' as u8 + x,
x @ 10 ..15 => 'a' as u8 + (x - 10))
radix!(UpperHex, 16, "0x", x @ 0 .. 9 => '0' as u8 + x,
x @ 10 ..15 => 'A' as u8 + (x - 10))
radix!(Binary, 2, "0b", x @ 0 .. 2 => b'0' + x)
radix!(Octal, 8, "0o", x @ 0 .. 7 => b'0' + x)
radix!(Decimal, 10, "", x @ 0 .. 9 => b'0' + x)
radix!(LowerHex, 16, "0x", x @ 0 .. 9 => b'0' + x,
x @ 10 ..15 => b'a' + (x - 10))
radix!(UpperHex, 16, "0x", x @ 0 .. 9 => b'0' + x,
x @ 10 ..15 => b'A' + (x - 10))

/// A radix with in the range of `2..36`.
#[deriving(Clone, PartialEq)]
Expand All @@ -129,8 +129,8 @@ impl GenericRadix for Radix {
fn base(&self) -> u8 { self.base }
fn digit(&self, x: u8) -> u8 {
match x {
x @ 0 ..9 => '0' as u8 + x,
x if x < self.base() => 'a' as u8 + (x - 10),
x @ 0 ..9 => b'0' + x,
x if x < self.base() => b'a' + (x - 10),
x => fail!("number not in the range 0..{}: {}", self.base() - 1, x),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ impl<'a> StrSlice<'a> for &'a str {
fn lines_any(&self) -> AnyLines<'a> {
self.lines().map(|line| {
let l = line.len();
if l > 0 && line.as_bytes()[l - 1] == '\r' as u8 { line.slice(0, l - 1) }
if l > 0 && line.as_bytes()[l - 1] == b'\r' { line.slice(0, l - 1) }
else { line }
})
}
Expand Down
38 changes: 19 additions & 19 deletions src/libdebug/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ impl<'a> ReprVisitor<'a> {
}

pub fn write_escaped_slice(&mut self, slice: &str) -> bool {
try!(self, self.writer.write(['"' as u8]));
try!(self, self.writer.write([b'"']));
for ch in slice.chars() {
if !self.write_escaped_char(ch, true) { return false }
}
try!(self, self.writer.write(['"' as u8]));
try!(self, self.writer.write([b'"']));
true
}

Expand All @@ -188,7 +188,7 @@ impl<'a> ReprVisitor<'a> {
inner: *const TyDesc) -> bool {
let mut p = ptr as *const u8;
let (sz, al) = unsafe { ((*inner).size, (*inner).align) };
try!(self, self.writer.write(['[' as u8]));
try!(self, self.writer.write([b'[']));
let mut first = true;
let mut left = len;
// unit structs have 0 size, and don't loop forever.
Expand All @@ -203,7 +203,7 @@ impl<'a> ReprVisitor<'a> {
p = align(unsafe { p.offset(sz as int) as uint }, al) as *const u8;
left -= dec;
}
try!(self, self.writer.write([']' as u8]));
try!(self, self.writer.write([b']']));
true
}

Expand Down Expand Up @@ -263,9 +263,9 @@ impl<'a> TyVisitor for ReprVisitor<'a> {

fn visit_char(&mut self) -> bool {
self.get::<char>(|this, &ch| {
try!(this, this.writer.write(['\'' as u8]));
try!(this, this.writer.write([b'\'']));
if !this.write_escaped_char(ch, false) { return false }
try!(this, this.writer.write(['\'' as u8]));
try!(this, this.writer.write([b'\'']));
true
})
}
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
}

fn visit_rptr(&mut self, mtbl: uint, inner: *const TyDesc) -> bool {
try!(self, self.writer.write(['&' as u8]));
try!(self, self.writer.write([b'&']));
self.write_mut_qualifier(mtbl);
self.get::<*const u8>(|this, p| {
this.visit_ptr_inner(*p, inner)
Expand All @@ -319,7 +319,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {

fn visit_evec_slice(&mut self, mtbl: uint, inner: *const TyDesc) -> bool {
self.get::<raw::Slice<()>>(|this, s| {
try!(this, this.writer.write(['&' as u8]));
try!(this, this.writer.write([b'&']));
this.write_mut_qualifier(mtbl);
let size = unsafe {
if (*inner).size == 0 { 1 } else { (*inner).size }
Expand All @@ -338,7 +338,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {

fn visit_enter_rec(&mut self, _n_fields: uint,
_sz: uint, _align: uint) -> bool {
try!(self, self.writer.write(['{' as u8]));
try!(self, self.writer.write([b'{']));
true
}

Expand All @@ -356,7 +356,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {

fn visit_leave_rec(&mut self, _n_fields: uint,
_sz: uint, _align: uint) -> bool {
try!(self, self.writer.write(['}' as u8]));
try!(self, self.writer.write([b'}']));
true
}

Expand All @@ -365,9 +365,9 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
try!(self, self.writer.write(name.as_bytes()));
if n_fields != 0 {
if named_fields {
try!(self, self.writer.write(['{' as u8]));
try!(self, self.writer.write([b'{']));
} else {
try!(self, self.writer.write(['(' as u8]));
try!(self, self.writer.write([b'(']));
}
}
true
Expand All @@ -390,17 +390,17 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
_sz: uint, _align: uint) -> bool {
if n_fields != 0 {
if named_fields {
try!(self, self.writer.write(['}' as u8]));
try!(self, self.writer.write([b'}']));
} else {
try!(self, self.writer.write([')' as u8]));
try!(self, self.writer.write([b')']));
}
}
true
}

fn visit_enter_tup(&mut self, _n_fields: uint,
_sz: uint, _align: uint) -> bool {
try!(self, self.writer.write(['(' as u8]));
try!(self, self.writer.write([b'(']));
true
}

Expand All @@ -415,9 +415,9 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
fn visit_leave_tup(&mut self, _n_fields: uint,
_sz: uint, _align: uint) -> bool {
if _n_fields == 1 {
try!(self, self.writer.write([',' as u8]));
try!(self, self.writer.write([b',']));
}
try!(self, self.writer.write([')' as u8]));
try!(self, self.writer.write([b')']));
true
}

Expand Down Expand Up @@ -455,7 +455,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
if write {
try!(self, self.writer.write(name.as_bytes()));
if n_fields > 0 {
try!(self, self.writer.write(['(' as u8]));
try!(self, self.writer.write([b'(']));
}
}
true
Expand Down Expand Up @@ -487,7 +487,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
match self.var_stk[self.var_stk.len() - 1] {
Matched => {
if n_fields > 0 {
try!(self, self.writer.write([')' as u8]));
try!(self, self.writer.write([b')']));
}
}
_ => ()
Expand Down
4 changes: 2 additions & 2 deletions src/libgetopts/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl Matches {
}

fn is_arg(arg: &str) -> bool {
arg.len() > 1 && arg.as_bytes()[0] == '-' as u8
arg.len() > 1 && arg.as_bytes()[0] == b'-'
}

fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> {
Expand Down Expand Up @@ -555,7 +555,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
} else {
let mut names;
let mut i_arg = None;
if cur.as_bytes()[1] == '-' as u8 {
if cur.as_bytes()[1] == b'-' {
let tail = cur.as_slice().slice(2, curlen);
let tail_eq: Vec<&str> = tail.split('=').collect();
if tail_eq.len() <= 1 {
Expand Down
2 changes: 1 addition & 1 deletion src/libnum/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ impl BigInt {
if buf.is_empty() { return None; }
let mut sign = Plus;
let mut start = 0;
if buf[0] == ('-' as u8) {
if buf[0] == b'-' {
sign = Minus;
start = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libregex/test/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn gen_text(n: uint) -> String {
.collect::<Vec<u8>>();
for (i, b) in bytes.mut_iter().enumerate() {
if i % 20 == 0 {
*b = '\n' as u8
*b = b'\n'
}
}
String::from_utf8(bytes).unwrap()
Expand Down
14 changes: 7 additions & 7 deletions src/libserialize/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ impl<'a> ToBase64 for &'a [u8] {
match config.line_length {
Some(line_length) =>
if cur_length >= line_length {
v.push('\r' as u8);
v.push('\n' as u8);
v.push(b'\r');
v.push(b'\n');
cur_length = 0;
},
None => ()
Expand All @@ -114,8 +114,8 @@ impl<'a> ToBase64 for &'a [u8] {
match config.line_length {
Some(line_length) =>
if cur_length >= line_length {
v.push('\r' as u8);
v.push('\n' as u8);
v.push(b'\r');
v.push(b'\n');
},
None => ()
}
Expand All @@ -130,8 +130,8 @@ impl<'a> ToBase64 for &'a [u8] {
v.push(bytes[((n >> 18) & 63) as uint]);
v.push(bytes[((n >> 12) & 63) as uint]);
if config.pad {
v.push('=' as u8);
v.push('=' as u8);
v.push(b'=');
v.push(b'=');
}
}
2 => {
Expand All @@ -141,7 +141,7 @@ impl<'a> ToBase64 for &'a [u8] {
v.push(bytes[((n >> 12) & 63) as uint]);
v.push(bytes[((n >> 6 ) & 63) as uint]);
if config.pad {
v.push('=' as u8);
v.push(b'=');
}
}
_ => fail!("Algebra is broken, please alert the math police")
Expand Down
10 changes: 5 additions & 5 deletions src/libserialize/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ impl<'a> FromHex for &'a str {
for (idx, byte) in self.bytes().enumerate() {
buf <<= 4;

match byte as char {
'A'..'F' => buf |= byte - ('A' as u8) + 10,
'a'..'f' => buf |= byte - ('a' as u8) + 10,
'0'..'9' => buf |= byte - ('0' as u8),
' '|'\r'|'\n'|'\t' => {
match byte {
b'A'..b'F' => buf |= byte - b'A' + 10,
b'a'..b'f' => buf |= byte - b'a' + 10,
b'0'..b'9' => buf |= byte - b'0',
b' '|b'\r'|b'\n'|b'\t' => {
buf >>= 4;
continue
}
Expand Down
Loading