Skip to content

Commit 07343ac

Browse files
gergoerdishepmaster
authored andcommitted
Remove the guts of fmt, but keep its interface.
Works around avr-rust/rust-legacy-fork#37
1 parent e39a823 commit 07343ac

File tree

1 file changed

+13
-225
lines changed

1 file changed

+13
-225
lines changed

src/fmt/mod.rs

+13-225
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ pub struct Formatter<'a> {
250250
width: Option<usize>,
251251
precision: Option<usize>,
252252

253-
buf: &'a mut (Write+'a),
254-
curarg: slice::Iter<'a, ArgumentV1<'a>>,
255-
args: &'a [ArgumentV1<'a>],
253+
phantom: PhantomData<&'a ()>,
256254
}
257255

258256
// NB. Argument is essentially an optimized partially applied formatting function,
@@ -440,7 +438,7 @@ impl<'a> Debug for Arguments<'a> {
440438
#[stable(feature = "rust1", since = "1.0.0")]
441439
impl<'a> Display for Arguments<'a> {
442440
fn fmt(&self, fmt: &mut Formatter) -> Result {
443-
write(fmt.buf, *self)
441+
Ok(())
444442
}
445443
}
446444

@@ -1013,42 +1011,6 @@ pub trait UpperExp {
10131011
/// [`write!`]: ../../std/macro.write.html
10141012
#[stable(feature = "rust1", since = "1.0.0")]
10151013
pub fn write(output: &mut Write, args: Arguments) -> Result {
1016-
let mut formatter = Formatter {
1017-
flags: 0,
1018-
width: None,
1019-
precision: None,
1020-
buf: output,
1021-
align: rt::v1::Alignment::Unknown,
1022-
fill: ' ',
1023-
args: args.args,
1024-
curarg: args.args.iter(),
1025-
};
1026-
1027-
let mut pieces = args.pieces.iter();
1028-
1029-
match args.fmt {
1030-
None => {
1031-
// We can use default formatting parameters for all arguments.
1032-
for (arg, piece) in args.args.iter().zip(pieces.by_ref()) {
1033-
formatter.buf.write_str(*piece)?;
1034-
(arg.formatter)(arg.value, &mut formatter)?;
1035-
}
1036-
}
1037-
Some(fmt) => {
1038-
// Every spec has a corresponding argument that is preceded by
1039-
// a string piece.
1040-
for (arg, piece) in fmt.iter().zip(pieces.by_ref()) {
1041-
formatter.buf.write_str(*piece)?;
1042-
formatter.run(arg)?;
1043-
}
1044-
}
1045-
}
1046-
1047-
// There can be only one trailing string piece left.
1048-
if let Some(piece) = pieces.next() {
1049-
formatter.buf.write_str(*piece)?;
1050-
}
1051-
10521014
Ok(())
10531015
}
10541016

@@ -1057,55 +1019,22 @@ impl<'a> Formatter<'a> {
10571019
where 'b: 'c, F: FnOnce(&'b mut (Write+'b)) -> &'c mut (Write+'c)
10581020
{
10591021
Formatter {
1060-
// We want to change this
1061-
buf: wrap(self.buf),
1062-
10631022
// And preserve these
10641023
flags: self.flags,
10651024
fill: self.fill,
10661025
align: self.align,
10671026
width: self.width,
10681027
precision: self.precision,
10691028

1070-
// These only exist in the struct for the `run` method,
1071-
// which won’t be used together with this method.
1072-
curarg: self.curarg.clone(),
1073-
args: self.args,
1029+
phantom: self.phantom,
10741030
}
10751031
}
10761032

10771033
// First up is the collection of functions used to execute a format string
10781034
// at runtime. This consumes all of the compile-time statics generated by
10791035
// the format! syntax extension.
10801036
fn run(&mut self, arg: &rt::v1::Argument) -> Result {
1081-
// Fill in the format parameters into the formatter
1082-
self.fill = arg.format.fill;
1083-
self.align = arg.format.align;
1084-
self.flags = arg.format.flags;
1085-
self.width = self.getcount(&arg.format.width);
1086-
self.precision = self.getcount(&arg.format.precision);
1087-
1088-
// Extract the correct argument
1089-
let value = match arg.position {
1090-
rt::v1::Position::Next => { *self.curarg.next().unwrap() }
1091-
rt::v1::Position::At(i) => self.args[i],
1092-
};
1093-
1094-
// Then actually do some printing
1095-
(value.formatter)(value.value, self)
1096-
}
1097-
1098-
fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
1099-
match *cnt {
1100-
rt::v1::Count::Is(n) => Some(n),
1101-
rt::v1::Count::Implied => None,
1102-
rt::v1::Count::Param(i) => {
1103-
self.args[i].as_usize()
1104-
}
1105-
rt::v1::Count::NextParam => {
1106-
self.curarg.next().and_then(|arg| arg.as_usize())
1107-
}
1108-
}
1037+
Ok(())
11091038
}
11101039

11111040
// Helper methods used for padding and processing formatting arguments that
@@ -1130,58 +1059,7 @@ impl<'a> Formatter<'a> {
11301059
prefix: &str,
11311060
buf: &str)
11321061
-> Result {
1133-
let mut width = buf.len();
1134-
1135-
let mut sign = None;
1136-
if !is_nonnegative {
1137-
sign = Some('-'); width += 1;
1138-
} else if self.sign_plus() {
1139-
sign = Some('+'); width += 1;
1140-
}
1141-
1142-
let mut prefixed = false;
1143-
if self.alternate() {
1144-
prefixed = true; width += prefix.chars().count();
1145-
}
1146-
1147-
// Writes the sign if it exists, and then the prefix if it was requested
1148-
let write_prefix = |f: &mut Formatter| {
1149-
if let Some(c) = sign {
1150-
f.buf.write_str(c.encode_utf8(&mut [0; 4]))?;
1151-
}
1152-
if prefixed { f.buf.write_str(prefix) }
1153-
else { Ok(()) }
1154-
};
1155-
1156-
// The `width` field is more of a `min-width` parameter at this point.
1157-
match self.width {
1158-
// If there's no minimum length requirements then we can just
1159-
// write the bytes.
1160-
None => {
1161-
write_prefix(self)?; self.buf.write_str(buf)
1162-
}
1163-
// Check if we're over the minimum width, if so then we can also
1164-
// just write the bytes.
1165-
Some(min) if width >= min => {
1166-
write_prefix(self)?; self.buf.write_str(buf)
1167-
}
1168-
// The sign and prefix goes before the padding if the fill character
1169-
// is zero
1170-
Some(min) if self.sign_aware_zero_pad() => {
1171-
self.fill = '0';
1172-
self.align = rt::v1::Alignment::Right;
1173-
write_prefix(self)?;
1174-
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
1175-
f.buf.write_str(buf)
1176-
})
1177-
}
1178-
// Otherwise, the sign and prefix goes after the padding
1179-
Some(min) => {
1180-
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
1181-
write_prefix(f)?; f.buf.write_str(buf)
1182-
})
1183-
}
1184-
}
1062+
Ok(())
11851063
}
11861064

11871065
/// This function takes a string slice and emits it to the internal buffer
@@ -1197,47 +1075,7 @@ impl<'a> Formatter<'a> {
11971075
/// Notably this function ignores the `flag` parameters.
11981076
#[stable(feature = "rust1", since = "1.0.0")]
11991077
pub fn pad(&mut self, s: &str) -> Result {
1200-
// Make sure there's a fast path up front
1201-
if self.width.is_none() && self.precision.is_none() {
1202-
return self.buf.write_str(s);
1203-
}
1204-
// The `precision` field can be interpreted as a `max-width` for the
1205-
// string being formatted.
1206-
let s = if let Some(max) = self.precision {
1207-
// If our string is longer that the precision, then we must have
1208-
// truncation. However other flags like `fill`, `width` and `align`
1209-
// must act as always.
1210-
if let Some((i, _)) = s.char_indices().skip(max).next() {
1211-
// LLVM here can't prove that `..i` won't panic `&s[..i]`, but
1212-
// we know that it can't panic. Use `get` + `unwrap_or` to avoid
1213-
// `unsafe` and otherwise don't emit any panic-related code
1214-
// here.
1215-
s.get(..i).unwrap_or(&s)
1216-
} else {
1217-
&s
1218-
}
1219-
} else {
1220-
&s
1221-
};
1222-
// The `width` field is more of a `min-width` parameter at this point.
1223-
match self.width {
1224-
// If we're under the maximum length, and there's no minimum length
1225-
// requirements, then we can just emit the string
1226-
None => self.buf.write_str(s),
1227-
// If we're under the maximum width, check if we're over the minimum
1228-
// width, if so it's as easy as just emitting the string.
1229-
Some(width) if s.chars().count() >= width => {
1230-
self.buf.write_str(s)
1231-
}
1232-
// If we're under both the maximum and the minimum width, then fill
1233-
// up the minimum width with the specified string + some alignment.
1234-
Some(width) => {
1235-
let align = rt::v1::Alignment::Left;
1236-
self.with_padding(width - s.chars().count(), align, |me| {
1237-
me.buf.write_str(s)
1238-
})
1239-
}
1240-
}
1078+
Ok(())
12411079
}
12421080

12431081
/// Runs a callback, emitting the correct padding either before or
@@ -1246,45 +1084,20 @@ impl<'a> Formatter<'a> {
12461084
f: F) -> Result
12471085
where F: FnOnce(&mut Formatter) -> Result,
12481086
{
1249-
let align = match self.align {
1250-
rt::v1::Alignment::Unknown => default,
1251-
_ => self.align
1252-
};
1253-
1254-
let (pre_pad, post_pad) = match align {
1255-
rt::v1::Alignment::Left => (0, padding),
1256-
rt::v1::Alignment::Right |
1257-
rt::v1::Alignment::Unknown => (padding, 0),
1258-
rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2),
1259-
};
1260-
1261-
let mut fill = [0; 4];
1262-
let fill = self.fill.encode_utf8(&mut fill);
1263-
1264-
for _ in 0..pre_pad {
1265-
self.buf.write_str(fill)?;
1266-
}
1267-
1268-
f(self)?;
1269-
1270-
for _ in 0..post_pad {
1271-
self.buf.write_str(fill)?;
1272-
}
1273-
12741087
Ok(())
12751088
}
12761089

12771090
/// Writes some data to the underlying buffer contained within this
12781091
/// formatter.
12791092
#[stable(feature = "rust1", since = "1.0.0")]
12801093
pub fn write_str(&mut self, data: &str) -> Result {
1281-
self.buf.write_str(data)
1094+
Ok(())
12821095
}
12831096

12841097
/// Writes some formatted information into this instance
12851098
#[stable(feature = "rust1", since = "1.0.0")]
12861099
pub fn write_fmt(&mut self, fmt: Arguments) -> Result {
1287-
write(self.buf, fmt)
1100+
Ok(())
12881101
}
12891102

12901103
/// Flags for formatting
@@ -1658,15 +1471,15 @@ impl<'a> Formatter<'a> {
16581471
#[stable(since = "1.2.0", feature = "formatter_write")]
16591472
impl<'a> Write for Formatter<'a> {
16601473
fn write_str(&mut self, s: &str) -> Result {
1661-
self.buf.write_str(s)
1474+
Ok(())
16621475
}
16631476

16641477
fn write_char(&mut self, c: char) -> Result {
1665-
self.buf.write_char(c)
1478+
Ok(())
16661479
}
16671480

16681481
fn write_fmt(&mut self, args: Arguments) -> Result {
1669-
write(self.buf, args)
1482+
Ok(())
16701483
}
16711484
}
16721485

@@ -1767,39 +1580,14 @@ impl Debug for char {
17671580
#[stable(feature = "rust1", since = "1.0.0")]
17681581
impl Display for char {
17691582
fn fmt(&self, f: &mut Formatter) -> Result {
1770-
if f.width.is_none() && f.precision.is_none() {
1771-
f.write_char(*self)
1772-
} else {
1773-
f.pad(self.encode_utf8(&mut [0; 4]))
1774-
}
1583+
Ok(())
17751584
}
17761585
}
17771586

17781587
#[stable(feature = "rust1", since = "1.0.0")]
17791588
impl<T: ?Sized> Pointer for *const T {
17801589
fn fmt(&self, f: &mut Formatter) -> Result {
1781-
let old_width = f.width;
1782-
let old_flags = f.flags;
1783-
1784-
// The alternate flag is already treated by LowerHex as being special-
1785-
// it denotes whether to prefix with 0x. We use it to work out whether
1786-
// or not to zero extend, and then unconditionally set it to get the
1787-
// prefix.
1788-
if f.alternate() {
1789-
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
1790-
1791-
if let None = f.width {
1792-
f.width = Some(((mem::size_of::<usize>() * 8) / 4) + 2);
1793-
}
1794-
}
1795-
f.flags |= 1 << (FlagV1::Alternate as u32);
1796-
1797-
let ret = LowerHex::fmt(&(*self as *const () as usize), f);
1798-
1799-
f.width = old_width;
1800-
f.flags = old_flags;
1801-
1802-
ret
1590+
Ok(())
18031591
}
18041592
}
18051593

0 commit comments

Comments
 (0)