Skip to content

Commit 67b03fb

Browse files
committed
int audit - libcore::fmt
1 parent dcc6ce2 commit 67b03fb

File tree

7 files changed

+73
-51
lines changed

7 files changed

+73
-51
lines changed

src/libcore/fmt/float.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ pub enum ExponentFormat {
4040
pub enum SignificantDigits {
4141
/// At most the given number of digits will be printed, truncating any
4242
/// trailing zeroes.
43-
DigMax(uint),
43+
DigMax(usize),
4444

4545
/// Precisely the given number of digits will be printed.
46-
DigExact(uint)
46+
DigExact(usize)
4747
}
4848

4949
/// How to emit the sign of a number.
@@ -240,27 +240,27 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
240240
// If reached left end of number, have to
241241
// insert additional digit:
242242
if i < 0
243-
|| buf[i as uint] == b'-'
244-
|| buf[i as uint] == b'+' {
245-
for j in (i as uint + 1..end).rev() {
243+
|| buf[i as usize] == b'-'
244+
|| buf[i as usize] == b'+' {
245+
for j in (i as usize + 1..end).rev() {
246246
buf[j + 1] = buf[j];
247247
}
248-
buf[(i + 1) as uint] = value2ascii(1);
248+
buf[(i + 1) as usize] = value2ascii(1);
249249
end += 1;
250250
break;
251251
}
252252

253253
// Skip the '.'
254-
if buf[i as uint] == b'.' { i -= 1; continue; }
254+
if buf[i as usize] == b'.' { i -= 1; continue; }
255255

256256
// Either increment the digit,
257257
// or set to 0 if max and carry the 1.
258-
let current_digit = ascii2value(buf[i as uint]);
258+
let current_digit = ascii2value(buf[i as usize]);
259259
if current_digit < (radix - 1) {
260-
buf[i as uint] = value2ascii(current_digit+1);
260+
buf[i as usize] = value2ascii(current_digit+1);
261261
break;
262262
} else {
263-
buf[i as uint] = value2ascii(0);
263+
buf[i as usize] = value2ascii(0);
264264
i -= 1;
265265
}
266266
}
@@ -311,7 +311,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
311311

312312
struct Filler<'a> {
313313
buf: &'a mut [u8],
314-
end: &'a mut uint,
314+
end: &'a mut usize,
315315
}
316316

317317
impl<'a> fmt::Write for Filler<'a> {

src/libcore/fmt/mod.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,14 @@ pub trait Write {
110110
/// traits.
111111
#[stable(feature = "rust1", since = "1.0.0")]
112112
pub struct Formatter<'a> {
113-
flags: uint,
113+
#[cfg(not(stage0))]
114+
flags: u32,
115+
#[cfg(stage0)]
116+
flags: usize,
114117
fill: char,
115118
align: rt::v1::Alignment,
116-
width: Option<uint>,
117-
precision: Option<uint>,
119+
width: Option<usize>,
120+
precision: Option<usize>,
118121

119122
buf: &'a mut (Write+'a),
120123
curarg: slice::Iter<'a, ArgumentV1<'a>>,
@@ -140,7 +143,7 @@ pub struct ArgumentV1<'a> {
140143

141144
impl<'a> ArgumentV1<'a> {
142145
#[inline(never)]
143-
fn show_uint(x: &uint, f: &mut Formatter) -> Result {
146+
fn show_usize(x: &usize, f: &mut Formatter) -> Result {
144147
Display::fmt(x, f)
145148
}
146149

@@ -156,15 +159,22 @@ impl<'a> ArgumentV1<'a> {
156159
}
157160
}
158161

162+
#[cfg(stage0)]
159163
#[doc(hidden)]
160164
#[stable(feature = "rust1", since = "1.0.0")]
161165
pub fn from_uint(x: &uint) -> ArgumentV1 {
162-
ArgumentV1::new(x, ArgumentV1::show_uint)
166+
ArgumentV1::new(x, ArgumentV1::show_usize)
167+
}
168+
#[cfg(not(stage0))]
169+
#[doc(hidden)]
170+
#[stable(feature = "rust1", since = "1.0.0")]
171+
pub fn from_usize(x: &usize) -> ArgumentV1 {
172+
ArgumentV1::new(x, ArgumentV1::show_usize)
163173
}
164174

165-
fn as_uint(&self) -> Option<uint> {
166-
if self.formatter as uint == ArgumentV1::show_uint as uint {
167-
Some(unsafe { *(self.value as *const _ as *const uint) })
175+
fn as_usize(&self) -> Option<usize> {
176+
if self.formatter as usize == ArgumentV1::show_usize as usize {
177+
Some(unsafe { *(self.value as *const _ as *const usize) })
168178
} else {
169179
None
170180
}
@@ -194,7 +204,7 @@ impl<'a> Arguments<'a> {
194204
/// The `pieces` array must be at least as long as `fmt` to construct
195205
/// a valid Arguments structure. Also, any `Count` within `fmt` that is
196206
/// `CountIsParam` or `CountIsNextParam` has to point to an argument
197-
/// created with `argumentuint`. However, failing to do so doesn't cause
207+
/// created with `argumentusize`. However, failing to do so doesn't cause
198208
/// unsafety, but will ignore invalid .
199209
#[doc(hidden)] #[inline]
200210
#[stable(feature = "rust1", since = "1.0.0")]
@@ -434,15 +444,15 @@ impl<'a> Formatter<'a> {
434444
(value.formatter)(value.value, self)
435445
}
436446

437-
fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<uint> {
447+
fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
438448
match *cnt {
439449
rt::v1::Count::Is(n) => Some(n),
440450
rt::v1::Count::Implied => None,
441451
rt::v1::Count::Param(i) => {
442-
self.args[i].as_uint()
452+
self.args[i].as_usize()
443453
}
444454
rt::v1::Count::NextParam => {
445-
self.curarg.next().and_then(|arg| arg.as_uint())
455+
self.curarg.next().and_then(|arg| arg.as_usize())
446456
}
447457
}
448458
}
@@ -476,12 +486,12 @@ impl<'a> Formatter<'a> {
476486
let mut sign = None;
477487
if !is_positive {
478488
sign = Some('-'); width += 1;
479-
} else if self.flags & (1 << (FlagV1::SignPlus as uint)) != 0 {
489+
} else if self.flags & (1 << (FlagV1::SignPlus as u32)) != 0 {
480490
sign = Some('+'); width += 1;
481491
}
482492

483493
let mut prefixed = false;
484-
if self.flags & (1 << (FlagV1::Alternate as uint)) != 0 {
494+
if self.flags & (1 << (FlagV1::Alternate as u32)) != 0 {
485495
prefixed = true; width += prefix.char_len();
486496
}
487497

@@ -511,7 +521,7 @@ impl<'a> Formatter<'a> {
511521
}
512522
// The sign and prefix goes before the padding if the fill character
513523
// is zero
514-
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as uint)) != 0 => {
524+
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 => {
515525
self.fill = '0';
516526
try!(write_prefix(self));
517527
self.with_padding(min - width, Alignment::Right, |f| {
@@ -581,7 +591,7 @@ impl<'a> Formatter<'a> {
581591

582592
/// Runs a callback, emitting the correct padding either before or
583593
/// afterwards depending on whether right or left alignment is requested.
584-
fn with_padding<F>(&mut self, padding: uint, default: Alignment,
594+
fn with_padding<F>(&mut self, padding: usize, default: Alignment,
585595
f: F) -> Result
586596
where F: FnOnce(&mut Formatter) -> Result,
587597
{
@@ -627,6 +637,11 @@ impl<'a> Formatter<'a> {
627637
write(self.buf, fmt)
628638
}
629639

640+
#[cfg(not(stage0))]
641+
/// Flags for formatting (packed version of rt::Flag)
642+
#[stable(feature = "rust1", since = "1.0.0")]
643+
pub fn flags(&self) -> u32 { self.flags }
644+
#[cfg(stage0)]
630645
/// Flags for formatting (packed version of rt::Flag)
631646
#[stable(feature = "rust1", since = "1.0.0")]
632647
pub fn flags(&self) -> usize { self.flags }
@@ -641,11 +656,11 @@ impl<'a> Formatter<'a> {
641656

642657
/// Optionally specified integer width that the output should be
643658
#[unstable(feature = "core", reason = "method was just created")]
644-
pub fn width(&self) -> Option<uint> { self.width }
659+
pub fn width(&self) -> Option<usize> { self.width }
645660

646661
/// Optionally specified precision for numeric types
647662
#[unstable(feature = "core", reason = "method was just created")]
648-
pub fn precision(&self) -> Option<uint> { self.precision }
663+
pub fn precision(&self) -> Option<usize> { self.precision }
649664
}
650665

651666
#[stable(feature = "rust1", since = "1.0.0")]
@@ -731,9 +746,9 @@ impl Display for char {
731746
#[stable(feature = "rust1", since = "1.0.0")]
732747
impl<T> Pointer for *const T {
733748
fn fmt(&self, f: &mut Formatter) -> Result {
734-
f.flags |= 1 << (FlagV1::Alternate as uint);
735-
let ret = LowerHex::fmt(&(*self as uint), f);
736-
f.flags &= !(1 << (FlagV1::Alternate as uint));
749+
f.flags |= 1 << (FlagV1::Alternate as u32);
750+
let ret = LowerHex::fmt(&(*self as u32), f);
751+
f.flags &= !(1 << (FlagV1::Alternate as u32));
737752
ret
738753
}
739754
}
@@ -889,7 +904,7 @@ impl<'a> Debug for &'a (any::Any+'a) {
889904
#[stable(feature = "rust1", since = "1.0.0")]
890905
impl<T: Debug> Debug for [T] {
891906
fn fmt(&self, f: &mut Formatter) -> Result {
892-
if f.flags & (1 << (FlagV1::Alternate as uint)) == 0 {
907+
if f.flags & (1 << (FlagV1::Alternate as u32)) == 0 {
893908
try!(write!(f, "["));
894909
}
895910
let mut is_first = true;
@@ -901,7 +916,7 @@ impl<T: Debug> Debug for [T] {
901916
}
902917
try!(write!(f, "{:?}", *x))
903918
}
904-
if f.flags & (1 << (FlagV1::Alternate as uint)) == 0 {
919+
if f.flags & (1 << (FlagV1::Alternate as u32)) == 0 {
905920
try!(write!(f, "]"));
906921
}
907922
Ok(())

src/libcore/fmt/num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ macro_rules! integer {
214214
show! { $Uint with $SU }
215215
}
216216
}
217-
integer! { int, uint, "i", "u" }
217+
integer! { isize, usize, "i", "u" }
218218
integer! { i8, u8 }
219219
integer! { i16, u16 }
220220
integer! { i32, u32 }

src/libcore/fmt/rt/v1.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ pub struct FormatSpec {
3232
pub fill: char,
3333
#[stable(feature = "rust1", since = "1.0.0")]
3434
pub align: Alignment,
35+
#[cfg(stage0)]
3536
#[stable(feature = "rust1", since = "1.0.0")]
36-
pub flags: uint,
37+
pub flags: usize,
38+
#[cfg(not(stage0))]
39+
#[stable(feature = "rust1", since = "1.0.0")]
40+
pub flags: u32,
3741
#[stable(feature = "rust1", since = "1.0.0")]
3842
pub precision: Count,
3943
#[stable(feature = "rust1", since = "1.0.0")]

src/libfmt_macros/lib.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
html_root_url = "http://doc.rust-lang.org/nightly/",
2525
html_playground_url = "http://play.rust-lang.org/")]
2626

27-
#![feature(int_uint)]
2827
#![feature(staged_api)]
2928
#![feature(unicode)]
3029

@@ -65,7 +64,7 @@ pub struct FormatSpec<'a> {
6564
/// Optionally specified alignment
6665
pub align: Alignment,
6766
/// Packed version of various flags provided
68-
pub flags: uint,
67+
pub flags: u32,
6968
/// The integer precision to use
7069
pub precision: Count<'a>,
7170
/// The string width requested for the resulting format
@@ -82,7 +81,7 @@ pub enum Position<'a> {
8281
/// The argument will be in the next position. This is the default.
8382
ArgumentNext,
8483
/// The argument is located at a specific index.
85-
ArgumentIs(uint),
84+
ArgumentIs(usize),
8685
/// The argument has a name.
8786
ArgumentNamed(&'a str),
8887
}
@@ -121,11 +120,11 @@ pub enum Flag {
121120
#[derive(Copy, PartialEq)]
122121
pub enum Count<'a> {
123122
/// The count is specified explicitly.
124-
CountIs(uint),
123+
CountIs(usize),
125124
/// The count is specified by the argument with the given name.
126125
CountIsName(&'a str),
127126
/// The count is specified by the argument at the given index.
128-
CountIsParam(uint),
127+
CountIsParam(usize),
129128
/// The count is specified by the next parameter.
130129
CountIsNextParam,
131130
/// The count is implied and cannot be explicitly specified.
@@ -237,7 +236,7 @@ impl<'a> Parser<'a> {
237236

238237
/// Parses all of a string which is to be considered a "raw literal" in a
239238
/// format string. This is everything outside of the braces.
240-
fn string(&mut self, start: uint) -> &'a str {
239+
fn string(&mut self, start: usize) -> &'a str {
241240
loop {
242241
// we may not consume the character, so clone the iterator
243242
match self.cur.clone().next() {
@@ -314,13 +313,13 @@ impl<'a> Parser<'a> {
314313
}
315314
// Sign flags
316315
if self.consume('+') {
317-
spec.flags |= 1 << (FlagSignPlus as uint);
316+
spec.flags |= 1 << (FlagSignPlus as u32);
318317
} else if self.consume('-') {
319-
spec.flags |= 1 << (FlagSignMinus as uint);
318+
spec.flags |= 1 << (FlagSignMinus as u32);
320319
}
321320
// Alternate marker
322321
if self.consume('#') {
323-
spec.flags |= 1 << (FlagAlternate as uint);
322+
spec.flags |= 1 << (FlagAlternate as u32);
324323
}
325324
// Width and precision
326325
let mut havewidth = false;
@@ -333,7 +332,7 @@ impl<'a> Parser<'a> {
333332
spec.width = CountIsParam(0);
334333
havewidth = true;
335334
} else {
336-
spec.flags |= 1 << (FlagSignAwareZeroPad as uint);
335+
spec.flags |= 1 << (FlagSignAwareZeroPad as u32);
337336
}
338337
}
339338
if !havewidth {
@@ -413,7 +412,7 @@ impl<'a> Parser<'a> {
413412

414413
/// Optionally parses an integer at the current position. This doesn't deal
415414
/// with overflow at all, it's just accumulating digits.
416-
fn integer(&mut self) -> Option<uint> {
415+
fn integer(&mut self) -> Option<usize> {
417416
let mut cur = 0;
418417
let mut found = false;
419418
loop {
@@ -617,7 +616,7 @@ mod tests {
617616
format: FormatSpec {
618617
fill: None,
619618
align: AlignUnknown,
620-
flags: (1 << FlagSignMinus as uint),
619+
flags: (1 << FlagSignMinus as u32),
621620
precision: CountImplied,
622621
width: CountImplied,
623622
ty: "",
@@ -628,7 +627,7 @@ mod tests {
628627
format: FormatSpec {
629628
fill: None,
630629
align: AlignUnknown,
631-
flags: (1 << FlagSignPlus as uint) | (1 << FlagAlternate as uint),
630+
flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
632631
precision: CountImplied,
633632
width: CountImplied,
634633
ty: "",

src/libsyntax/ext/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub trait AstBuilder {
148148
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
149149
fn expr_int(&self, sp: Span, i: isize) -> P<ast::Expr>;
150150
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
151+
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
151152
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
152153

153154
fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
@@ -701,6 +702,9 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
701702
self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs(false),
702703
ast::Sign::new(i))))
703704
}
705+
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
706+
self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))
707+
}
704708
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
705709
self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
706710
}

src/libsyntax/ext/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl<'a, 'b> Context<'a, 'b> {
417417
parse::AlignUnknown => align("Unknown"),
418418
};
419419
let align = self.ecx.expr_path(align);
420-
let flags = self.ecx.expr_usize(sp, arg.format.flags);
420+
let flags = self.ecx.expr_u32(sp, arg.format.flags);
421421
let prec = self.trans_count(arg.format.precision);
422422
let width = self.trans_count(arg.format.width);
423423
let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, "FormatSpec"));
@@ -610,7 +610,7 @@ impl<'a, 'b> Context<'a, 'b> {
610610
ecx.ident_of_std("core"),
611611
ecx.ident_of("fmt"),
612612
ecx.ident_of("ArgumentV1"),
613-
ecx.ident_of("from_uint")], vec![arg])
613+
ecx.ident_of("from_usize")], vec![arg])
614614
}
615615
};
616616

0 commit comments

Comments
 (0)