Skip to content

Commit 3fb78e2

Browse files
committed
Use byte literals in libstd
1 parent dfdea3f commit 3fb78e2

File tree

13 files changed

+52
-54
lines changed

13 files changed

+52
-54
lines changed

src/libstd/ascii.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Ascii {
119119
/// Check if the character is a space or horizontal tab
120120
#[inline]
121121
pub fn is_blank(&self) -> bool {
122-
self.chr == ' ' as u8 || self.chr == '\t' as u8
122+
self.chr == b' ' || self.chr == b'\t'
123123
}
124124

125125
/// Check if the character is a control character
@@ -150,7 +150,7 @@ impl Ascii {
150150
/// Checks if the character is lowercase
151151
#[inline]
152152
pub fn is_lowercase(&self) -> bool {
153-
(self.chr - 'a' as u8) < 26
153+
(self.chr - b'a') < 26
154154
}
155155

156156
#[inline]
@@ -163,7 +163,7 @@ impl Ascii {
163163
/// Checks if the character is uppercase
164164
#[inline]
165165
pub fn is_uppercase(&self) -> bool {
166-
(self.chr - 'A' as u8) < 26
166+
(self.chr - b'A') < 26
167167
}
168168

169169
/// Checks if the character is punctuation
@@ -175,7 +175,7 @@ impl Ascii {
175175
/// Checks if the character is a valid hex digit
176176
#[inline]
177177
pub fn is_hex(&self) -> bool {
178-
self.is_digit() || ((self.chr | 32u8) - 'a' as u8) < 6
178+
self.is_digit() || ((self.chr | 32u8) - b'a') < 6
179179
}
180180
}
181181

@@ -792,13 +792,13 @@ mod tests {
792792

793793
#[test]
794794
fn test_to_string() {
795-
let s = Ascii{ chr: 't' as u8 }.to_string();
795+
let s = Ascii{ chr: b't' }.to_string();
796796
assert_eq!(s, "t".to_string());
797797
}
798798

799799
#[test]
800800
fn test_show() {
801-
let c = Ascii { chr: 't' as u8 };
801+
let c = Ascii { chr: b't' };
802802
assert_eq!(format!("{}", c), "t".to_string());
803803
}
804804
}

src/libstd/dynamic_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl DynamicLibrary {
114114
}
115115

116116
fn separator() -> u8 {
117-
if cfg!(windows) {';' as u8} else {':' as u8}
117+
if cfg!(windows) {b';'} else {b':'}
118118
}
119119

120120
/// Returns the current search path for dynamic libraries being used by this

src/libstd/io/buffered.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<W: Writer> LineBufferedWriter<W> {
248248

249249
impl<W: Writer> Writer for LineBufferedWriter<W> {
250250
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
251-
match buf.iter().rposition(|&b| b == '\n' as u8) {
251+
match buf.iter().rposition(|&b| b == b'\n') {
252252
Some(i) => {
253253
try!(self.inner.write(buf.slice_to(i + 1)));
254254
try!(self.inner.flush());
@@ -524,15 +524,15 @@ mod test {
524524
assert_eq!(writer.get_ref().get_ref(), &[]);
525525
writer.flush().unwrap();
526526
assert_eq!(writer.get_ref().get_ref(), &[0, 1]);
527-
writer.write([0, '\n' as u8, 1, '\n' as u8, 2]).unwrap();
527+
writer.write([0, b'\n', 1, b'\n', 2]).unwrap();
528528
assert_eq!(writer.get_ref().get_ref(),
529-
&[0, 1, 0, '\n' as u8, 1, '\n' as u8]);
529+
&[0, 1, 0, b'\n', 1, b'\n']);
530530
writer.flush().unwrap();
531531
assert_eq!(writer.get_ref().get_ref(),
532-
&[0, 1, 0, '\n' as u8, 1, '\n' as u8, 2]);
533-
writer.write([3, '\n' as u8]).unwrap();
532+
&[0, 1, 0, b'\n', 1, b'\n', 2]);
533+
writer.write([3, b'\n']).unwrap();
534534
assert_eq!(writer.get_ref().get_ref(),
535-
&[0, 1, 0, '\n' as u8, 1, '\n' as u8, 2, 3, '\n' as u8]);
535+
&[0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
536536
}
537537

538538
#[test]
@@ -579,7 +579,7 @@ mod test {
579579

580580
#[test]
581581
fn test_chars() {
582-
let buf = [195u8, 159u8, 'a' as u8];
582+
let buf = [195u8, 159u8, b'a'];
583583
let mut reader = BufferedReader::with_capacity(1, BufReader::new(buf));
584584
let mut it = reader.chars();
585585
assert_eq!(it.next(), Some(Ok('ß')));

src/libstd/io/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,8 +1241,8 @@ mod test {
12411241
let mut cur = [0u8, .. 2];
12421242
for f in files {
12431243
let stem = f.filestem_str().unwrap();
1244-
let root = stem.as_bytes()[0] - ('0' as u8);
1245-
let name = stem.as_bytes()[1] - ('0' as u8);
1244+
let root = stem.as_bytes()[0] - b'0';
1245+
let name = stem.as_bytes()[1] - b'0';
12461246
assert!(cur[root as uint] < name);
12471247
cur[root as uint] = name;
12481248
}

src/libstd/io/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ pub trait Writer {
11031103
/// that the `write` method is used specifically instead.
11041104
#[inline]
11051105
fn write_line(&mut self, s: &str) -> IoResult<()> {
1106-
self.write_str(s).and_then(|()| self.write(['\n' as u8]))
1106+
self.write_str(s).and_then(|()| self.write([b'\n']))
11071107
}
11081108

11091109
/// Write a single char, encoded as UTF-8.
@@ -1442,7 +1442,7 @@ pub trait Buffer: Reader {
14421442
/// Additionally, this function can fail if the line of input read is not a
14431443
/// valid UTF-8 sequence of bytes.
14441444
fn read_line(&mut self) -> IoResult<String> {
1445-
self.read_until('\n' as u8).and_then(|line|
1445+
self.read_until(b'\n').and_then(|line|
14461446
match String::from_utf8(line) {
14471447
Ok(s) => Ok(s),
14481448
Err(_) => Err(standard_error(InvalidInput)),

src/libstd/io/net/ip.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ impl<'a> Parser<'a> {
161161
fn parse_digit(c: char, radix: u8) -> Option<u8> {
162162
let c = c as u8;
163163
// assuming radix is either 10 or 16
164-
if c >= '0' as u8 && c <= '9' as u8 {
165-
Some(c - '0' as u8)
166-
} else if radix > 10 && c >= 'a' as u8 && c < 'a' as u8 + (radix - 10) {
167-
Some(c - 'a' as u8 + 10)
168-
} else if radix > 10 && c >= 'A' as u8 && c < 'A' as u8 + (radix - 10) {
169-
Some(c - 'A' as u8 + 10)
164+
if c >= b'0' && c <= b'9' {
165+
Some(c - b'0')
166+
} else if radix > 10 && c >= b'a' && c < b'a' + (radix - 10) {
167+
Some(c - b'a' + 10)
168+
} else if radix > 10 && c >= b'A' && c < b'A' + (radix - 10) {
169+
Some(c - b'A' + 10)
170170
} else {
171171
None
172172
}

src/libstd/io/stdio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn print(s: &str) {
239239
/// `\n` character is printed to the console after the string.
240240
pub fn println(s: &str) {
241241
with_task_stdout(|io| {
242-
io.write(s.as_bytes()).and_then(|()| io.write(['\n' as u8]))
242+
io.write(s.as_bytes()).and_then(|()| io.write([b'\n']))
243243
})
244244
}
245245

src/libstd/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ pub mod builtin {
502502
///
503503
/// ```
504504
/// let rust = bytes!("r", 'u', "st", 255);
505-
/// assert_eq!(rust[1], 'u' as u8);
505+
/// assert_eq!(rust[1], b'u');
506506
/// assert_eq!(rust[4], 255);
507507
/// ```
508508
#[macro_export]

src/libstd/num/strconv.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ impl_NumStrConv_Integer!(u64)
138138

139139

140140
// Special value strings as [u8] consts.
141-
static INF_BUF: [u8, ..3] = ['i' as u8, 'n' as u8, 'f' as u8];
142-
static POS_INF_BUF: [u8, ..4] = ['+' as u8, 'i' as u8, 'n' as u8,
143-
'f' as u8];
144-
static NEG_INF_BUF: [u8, ..4] = ['-' as u8, 'i' as u8, 'n' as u8,
145-
'f' as u8];
146-
static NAN_BUF: [u8, ..3] = ['N' as u8, 'a' as u8, 'N' as u8];
141+
static INF_BUF: [u8, ..3] = [b'i', b'n', b'f'];
142+
static POS_INF_BUF: [u8, ..4] = [b'+', b'i', b'n', b'f'];
143+
static NEG_INF_BUF: [u8, ..4] = [b'-', b'i', b'n', b'f'];
144+
static NAN_BUF: [u8, ..3] = [b'N', b'a', b'N'];
147145

148146
/**
149147
* Converts an integral number to its string representation as a byte vector.
@@ -201,8 +199,8 @@ pub fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f:
201199
current_digit_signed
202200
};
203201
buf[cur] = match current_digit.to_u8().unwrap() {
204-
i @ 0..9 => '0' as u8 + i,
205-
i => 'a' as u8 + (i - 10),
202+
i @ 0..9 => b'0' + i,
203+
i => b'a' + (i - 10),
206204
};
207205
cur += 1;
208206

@@ -213,8 +211,8 @@ pub fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f:
213211

214212
// Decide what sign to put in front
215213
match sign {
216-
SignNeg | SignAll if neg => { f('-' as u8); }
217-
SignAll => { f('+' as u8); }
214+
SignNeg | SignAll if neg => { f(b'-'); }
215+
SignAll => { f(b'+'); }
218216
_ => ()
219217
}
220218

@@ -350,10 +348,10 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
350348
// Decide what sign to put in front
351349
match sign {
352350
SignNeg | SignAll if neg => {
353-
buf.push('-' as u8);
351+
buf.push(b'-');
354352
}
355353
SignAll => {
356-
buf.push('+' as u8);
354+
buf.push(b'+');
357355
}
358356
_ => ()
359357
}
@@ -368,7 +366,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
368366
// Now emit the fractional part, if any
369367
deccum = num.fract();
370368
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
371-
buf.push('.' as u8);
369+
buf.push(b'.');
372370
let mut dig = 0u;
373371

374372
// calculate new digits while
@@ -415,14 +413,14 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
415413
// If reached left end of number, have to
416414
// insert additional digit:
417415
if i < 0
418-
|| *buf.get(i as uint) == '-' as u8
419-
|| *buf.get(i as uint) == '+' as u8 {
416+
|| *buf.get(i as uint) == b'-'
417+
|| *buf.get(i as uint) == b'+' {
420418
buf.insert((i + 1) as uint, value2ascii(1));
421419
break;
422420
}
423421

424422
// Skip the '.'
425-
if *buf.get(i as uint) == '.' as u8 { i -= 1; continue; }
423+
if *buf.get(i as uint) == b'.' { i -= 1; continue; }
426424

427425
// Either increment the digit,
428426
// or set to 0 if max and carry the 1.
@@ -448,14 +446,14 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
448446
let mut i = buf_max_i;
449447

450448
// discover trailing zeros of fractional part
451-
while i > start_fractional_digits && *buf.get(i) == '0' as u8 {
449+
while i > start_fractional_digits && *buf.get(i) == b'0' {
452450
i -= 1;
453451
}
454452

455453
// Only attempt to truncate digits if buf has fractional digits
456454
if i >= start_fractional_digits {
457455
// If buf ends with '.', cut that too.
458-
if *buf.get(i) == '.' as u8 { i -= 1 }
456+
if *buf.get(i) == b'.' { i -= 1 }
459457

460458
// only resize buf if we actually remove digits
461459
if i < buf_max_i {
@@ -465,7 +463,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
465463
} // If exact and trailing '.', just cut that
466464
else {
467465
let max_i = buf.len() - 1;
468-
if *buf.get(max_i) == '.' as u8 {
466+
if *buf.get(max_i) == b'.' {
469467
buf = Vec::from_slice(buf.slice(0, max_i));
470468
}
471469
}

src/libstd/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub fn env_as_bytes() -> Vec<(Vec<u8>,Vec<u8>)> {
293293
fn env_convert(input: Vec<Vec<u8>>) -> Vec<(Vec<u8>, Vec<u8>)> {
294294
let mut pairs = Vec::new();
295295
for p in input.iter() {
296-
let mut it = p.as_slice().splitn(1, |b| *b == '=' as u8);
296+
let mut it = p.as_slice().splitn(1, |b| *b == b'=');
297297
let key = Vec::from_slice(it.next().unwrap());
298298
let val = Vec::from_slice(it.next().unwrap_or(&[]));
299299
pairs.push((key, val));

src/libstd/path/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
351351
match self.filename() {
352352
None => None,
353353
Some(name) => Some({
354-
let dot = '.' as u8;
354+
let dot = b'.';
355355
match name.rposition_elem(&dot) {
356356
None | Some(0) => name,
357357
Some(1) if name == b".." => name,
@@ -398,7 +398,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
398398
match self.filename() {
399399
None => None,
400400
Some(name) => {
401-
let dot = '.' as u8;
401+
let dot = b'.';
402402
match name.rposition_elem(&dot) {
403403
None | Some(0) => None,
404404
Some(1) if name == b".." => None,
@@ -474,7 +474,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
474474
assert!(!contains_nul(&extension));
475475

476476
let val = self.filename().and_then(|name| {
477-
let dot = '.' as u8;
477+
let dot = b'.';
478478
let extlen = extension.container_as_bytes().len();
479479
match (name.rposition_elem(&dot), extlen) {
480480
(None, 0) | (Some(0), 0) => None,

src/libstd/path/posix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl GenericPath for Path {
237237
match self.sepidx {
238238
None if b"." == self.repr.as_slice() => false,
239239
None => {
240-
self.repr = vec!['.' as u8];
240+
self.repr = vec![b'.'];
241241
self.sepidx = None;
242242
true
243243
}

src/libstd/path/windows.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,12 @@ impl Path {
737737
let mut comps = comps;
738738
match (comps.is_some(),prefix) {
739739
(false, Some(DiskPrefix)) => {
740-
if s.as_bytes()[0] >= 'a' as u8 && s.as_bytes()[0] <= 'z' as u8 {
740+
if s.as_bytes()[0] >= b'a' && s.as_bytes()[0] <= b'z' {
741741
comps = Some(vec![]);
742742
}
743743
}
744744
(false, Some(VerbatimDiskPrefix)) => {
745-
if s.as_bytes()[4] >= 'a' as u8 && s.as_bytes()[0] <= 'z' as u8 {
745+
if s.as_bytes()[4] >= b'a' && s.as_bytes()[0] <= b'z' {
746746
comps = Some(vec![]);
747747
}
748748
}
@@ -1010,7 +1010,7 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
10101010
} else {
10111011
// \\?\path
10121012
let idx = path.find('\\');
1013-
if idx == Some(2) && path.as_bytes()[1] == ':' as u8 {
1013+
if idx == Some(2) && path.as_bytes()[1] == b':' {
10141014
let c = path.as_bytes()[0];
10151015
if c.is_ascii() && (c as char).is_alphabetic() {
10161016
// \\?\C:\ path
@@ -1033,7 +1033,7 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
10331033
}
10341034
_ => ()
10351035
}
1036-
} else if path.len() > 1 && path.as_bytes()[1] == ':' as u8 {
1036+
} else if path.len() > 1 && path.as_bytes()[1] == b':' {
10371037
// C:
10381038
let c = path.as_bytes()[0];
10391039
if c.is_ascii() && (c as char).is_alphabetic() {

0 commit comments

Comments
 (0)