Skip to content

Commit 8fe73f1

Browse files
committed
auto merge of #16291 : nham/rust/byte_literals, r=alexcrichton
This replaces many instances chars being casted to u8 with byte literals.
2 parents b9308d1 + 96d1712 commit 8fe73f1

File tree

26 files changed

+131
-134
lines changed

26 files changed

+131
-134
lines changed

src/libcollections/str.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ mod tests {
14571457
109
14581458
];
14591459
assert_eq!("".as_bytes(), &[]);
1460-
assert_eq!("abc".as_bytes(), &['a' as u8, 'b' as u8, 'c' as u8]);
1460+
assert_eq!("abc".as_bytes(), b"abc");
14611461
assert_eq!("ศไทย中华Việt Nam".as_bytes(), v.as_slice());
14621462
}
14631463

@@ -1475,11 +1475,11 @@ mod tests {
14751475
fn test_as_ptr() {
14761476
let buf = "hello".as_ptr();
14771477
unsafe {
1478-
assert_eq!(*buf.offset(0), 'h' as u8);
1479-
assert_eq!(*buf.offset(1), 'e' as u8);
1480-
assert_eq!(*buf.offset(2), 'l' as u8);
1481-
assert_eq!(*buf.offset(3), 'l' as u8);
1482-
assert_eq!(*buf.offset(4), 'o' as u8);
1478+
assert_eq!(*buf.offset(0), b'h');
1479+
assert_eq!(*buf.offset(1), b'e');
1480+
assert_eq!(*buf.offset(2), b'l');
1481+
assert_eq!(*buf.offset(3), b'l');
1482+
assert_eq!(*buf.offset(4), b'o');
14831483
}
14841484
}
14851485

src/libcollections/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ mod tests {
10401040
fn test_push_bytes() {
10411041
let mut s = String::from_str("ABC");
10421042
unsafe {
1043-
s.push_bytes([ 'D' as u8 ]);
1043+
s.push_bytes([b'D']);
10441044
}
10451045
assert_eq!(s.as_slice(), "ABCD");
10461046
}

src/libcore/fmt/float.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
198198
// Decide what sign to put in front
199199
match sign {
200200
SignNeg | SignAll if neg => {
201-
buf[end] = '-' as u8;
201+
buf[end] = b'-';
202202
end += 1;
203203
}
204204
SignAll => {
205-
buf[end] = '+' as u8;
205+
buf[end] = b'+';
206206
end += 1;
207207
}
208208
_ => ()
@@ -218,7 +218,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
218218
// Now emit the fractional part, if any
219219
deccum = num.fract();
220220
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
221-
buf[end] = '.' as u8;
221+
buf[end] = b'.';
222222
end += 1;
223223
let mut dig = 0u;
224224

@@ -269,8 +269,8 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
269269
// If reached left end of number, have to
270270
// insert additional digit:
271271
if i < 0
272-
|| buf[i as uint] == '-' as u8
273-
|| buf[i as uint] == '+' as u8 {
272+
|| buf[i as uint] == b'-'
273+
|| buf[i as uint] == b'+' {
274274
for j in range(i as uint + 1, end).rev() {
275275
buf[j + 1] = buf[j];
276276
}
@@ -280,7 +280,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
280280
}
281281

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

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

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

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

318318
// only resize buf if we actually remove digits
319319
if i < buf_max_i {
@@ -323,7 +323,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
323323
} // If exact and trailing '.', just cut that
324324
else {
325325
let max_i = end - 1;
326-
if buf[max_i] == '.' as u8 {
326+
if buf[max_i] == b'.' {
327327
end = max_i;
328328
}
329329
}

src/libcore/fmt/num.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ macro_rules! radix {
104104
}
105105
}
106106

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

115115
/// A radix with in the range of `2..36`.
116116
#[deriving(Clone, PartialEq)]
@@ -129,8 +129,8 @@ impl GenericRadix for Radix {
129129
fn base(&self) -> u8 { self.base }
130130
fn digit(&self, x: u8) -> u8 {
131131
match x {
132-
x @ 0 ..9 => '0' as u8 + x,
133-
x if x < self.base() => 'a' as u8 + (x - 10),
132+
x @ 0 ..9 => b'0' + x,
133+
x if x < self.base() => b'a' + (x - 10),
134134
x => fail!("number not in the range 0..{}: {}", self.base() - 1, x),
135135
}
136136
}

src/libcore/str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ impl<'a> StrSlice<'a> for &'a str {
17551755
fn lines_any(&self) -> AnyLines<'a> {
17561756
self.lines().map(|line| {
17571757
let l = line.len();
1758-
if l > 0 && line.as_bytes()[l - 1] == '\r' as u8 { line.slice(0, l - 1) }
1758+
if l > 0 && line.as_bytes()[l - 1] == b'\r' { line.slice(0, l - 1) }
17591759
else { line }
17601760
})
17611761
}

src/libdebug/repr.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ impl<'a> ReprVisitor<'a> {
165165
}
166166

167167
pub fn write_escaped_slice(&mut self, slice: &str) -> bool {
168-
try!(self, self.writer.write(['"' as u8]));
168+
try!(self, self.writer.write([b'"']));
169169
for ch in slice.chars() {
170170
if !self.write_escaped_char(ch, true) { return false }
171171
}
172-
try!(self, self.writer.write(['"' as u8]));
172+
try!(self, self.writer.write([b'"']));
173173
true
174174
}
175175

@@ -188,7 +188,7 @@ impl<'a> ReprVisitor<'a> {
188188
inner: *const TyDesc) -> bool {
189189
let mut p = ptr as *const u8;
190190
let (sz, al) = unsafe { ((*inner).size, (*inner).align) };
191-
try!(self, self.writer.write(['[' as u8]));
191+
try!(self, self.writer.write([b'[']));
192192
let mut first = true;
193193
let mut left = len;
194194
// unit structs have 0 size, and don't loop forever.
@@ -203,7 +203,7 @@ impl<'a> ReprVisitor<'a> {
203203
p = align(unsafe { p.offset(sz as int) as uint }, al) as *const u8;
204204
left -= dec;
205205
}
206-
try!(self, self.writer.write([']' as u8]));
206+
try!(self, self.writer.write([b']']));
207207
true
208208
}
209209

@@ -263,9 +263,9 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
263263

264264
fn visit_char(&mut self) -> bool {
265265
self.get::<char>(|this, &ch| {
266-
try!(this, this.writer.write(['\'' as u8]));
266+
try!(this, this.writer.write([b'\'']));
267267
if !this.write_escaped_char(ch, false) { return false }
268-
try!(this, this.writer.write(['\'' as u8]));
268+
try!(this, this.writer.write([b'\'']));
269269
true
270270
})
271271
}
@@ -310,7 +310,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
310310
}
311311

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

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

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

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

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

@@ -365,9 +365,9 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
365365
try!(self, self.writer.write(name.as_bytes()));
366366
if n_fields != 0 {
367367
if named_fields {
368-
try!(self, self.writer.write(['{' as u8]));
368+
try!(self, self.writer.write([b'{']));
369369
} else {
370-
try!(self, self.writer.write(['(' as u8]));
370+
try!(self, self.writer.write([b'(']));
371371
}
372372
}
373373
true
@@ -390,17 +390,17 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
390390
_sz: uint, _align: uint) -> bool {
391391
if n_fields != 0 {
392392
if named_fields {
393-
try!(self, self.writer.write(['}' as u8]));
393+
try!(self, self.writer.write([b'}']));
394394
} else {
395-
try!(self, self.writer.write([')' as u8]));
395+
try!(self, self.writer.write([b')']));
396396
}
397397
}
398398
true
399399
}
400400

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

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

@@ -455,7 +455,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
455455
if write {
456456
try!(self, self.writer.write(name.as_bytes()));
457457
if n_fields > 0 {
458-
try!(self, self.writer.write(['(' as u8]));
458+
try!(self, self.writer.write([b'(']));
459459
}
460460
}
461461
true
@@ -487,7 +487,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
487487
match self.var_stk[self.var_stk.len() - 1] {
488488
Matched => {
489489
if n_fields > 0 {
490-
try!(self, self.writer.write([')' as u8]));
490+
try!(self, self.writer.write([b')']));
491491
}
492492
}
493493
_ => ()

src/libgetopts/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl Matches {
372372
}
373373

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

378378
fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> {
@@ -555,7 +555,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
555555
} else {
556556
let mut names;
557557
let mut i_arg = None;
558-
if cur.as_bytes()[1] == '-' as u8 {
558+
if cur.as_bytes()[1] == b'-' {
559559
let tail = cur.as_slice().slice(2, curlen);
560560
let tail_eq: Vec<&str> = tail.split('=').collect();
561561
if tail_eq.len() <= 1 {

src/libnum/bigint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ impl BigInt {
13761376
if buf.is_empty() { return None; }
13771377
let mut sign = Plus;
13781378
let mut start = 0;
1379-
if buf[0] == ('-' as u8) {
1379+
if buf[0] == b'-' {
13801380
sign = Minus;
13811381
start = 1;
13821382
}

src/libregex/test/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn gen_text(n: uint) -> String {
159159
.collect::<Vec<u8>>();
160160
for (i, b) in bytes.mut_iter().enumerate() {
161161
if i % 20 == 0 {
162-
*b = '\n' as u8
162+
*b = b'\n'
163163
}
164164
}
165165
String::from_utf8(bytes).unwrap()

src/libserialize/base64.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ impl<'a> ToBase64 for &'a [u8] {
8989
match config.line_length {
9090
Some(line_length) =>
9191
if cur_length >= line_length {
92-
v.push('\r' as u8);
93-
v.push('\n' as u8);
92+
v.push(b'\r');
93+
v.push(b'\n');
9494
cur_length = 0;
9595
},
9696
None => ()
@@ -114,8 +114,8 @@ impl<'a> ToBase64 for &'a [u8] {
114114
match config.line_length {
115115
Some(line_length) =>
116116
if cur_length >= line_length {
117-
v.push('\r' as u8);
118-
v.push('\n' as u8);
117+
v.push(b'\r');
118+
v.push(b'\n');
119119
},
120120
None => ()
121121
}
@@ -130,8 +130,8 @@ impl<'a> ToBase64 for &'a [u8] {
130130
v.push(bytes[((n >> 18) & 63) as uint]);
131131
v.push(bytes[((n >> 12) & 63) as uint]);
132132
if config.pad {
133-
v.push('=' as u8);
134-
v.push('=' as u8);
133+
v.push(b'=');
134+
v.push(b'=');
135135
}
136136
}
137137
2 => {
@@ -141,7 +141,7 @@ impl<'a> ToBase64 for &'a [u8] {
141141
v.push(bytes[((n >> 12) & 63) as uint]);
142142
v.push(bytes[((n >> 6 ) & 63) as uint]);
143143
if config.pad {
144-
v.push('=' as u8);
144+
v.push(b'=');
145145
}
146146
}
147147
_ => fail!("Algebra is broken, please alert the math police")

src/libserialize/hex.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ impl<'a> FromHex for &'a str {
112112
for (idx, byte) in self.bytes().enumerate() {
113113
buf <<= 4;
114114

115-
match byte as char {
116-
'A'..'F' => buf |= byte - ('A' as u8) + 10,
117-
'a'..'f' => buf |= byte - ('a' as u8) + 10,
118-
'0'..'9' => buf |= byte - ('0' as u8),
119-
' '|'\r'|'\n'|'\t' => {
115+
match byte {
116+
b'A'..b'F' => buf |= byte - b'A' + 10,
117+
b'a'..b'f' => buf |= byte - b'a' + 10,
118+
b'0'..b'9' => buf |= byte - b'0',
119+
b' '|b'\r'|b'\n'|b'\t' => {
120120
buf >>= 4;
121121
continue
122122
}

0 commit comments

Comments
 (0)