Skip to content

Commit 6f4eb65

Browse files
reitermarkusnicholasbishop
authored andcommitted
Consistently use C types.
1 parent d8fa33b commit 6f4eb65

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ pub mod argument {
400400
/// need to null terminate a string to print it, you can skip that step.
401401
String(&'a CStr),
402402
/// `c`
403-
Char(u8),
403+
Char(c_char),
404404
/// `x`
405405
Hex(UnsignedInt),
406406
/// `X`

src/output.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ pub fn fmt_write(w: &mut impl fmt::Write) -> impl FnMut(Argument) -> c_int + '_
274274
},
275275
Specifier::Char(data) => {
276276
if flags.contains(Flags::LEFT_ALIGN) {
277-
write!(w, "{:width$}", data as char, width = width as usize)
277+
write!(w, "{:width$}", data as u8 as char, width = width as usize)
278278
} else {
279-
write!(w, "{:>width$}", data as char, width = width as usize)
279+
write!(w, "{:>width$}", data as u8 as char, width = width as usize)
280280
}
281281
}
282282
Specifier::Pointer(data) => {

src/parser.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl Length {
7474
unsafe fn parse_signed(self, args: &mut VaList) -> SignedInt {
7575
match self {
7676
Length::Int => SignedInt::Int(args.arg()),
77-
Length::Char => SignedInt::Char(args.arg::<i32>() as i8),
78-
Length::Short => SignedInt::Short(args.arg::<i32>() as i16),
77+
Length::Char => SignedInt::Char(args.arg::<c_int>() as c_schar),
78+
Length::Short => SignedInt::Short(args.arg::<c_int>() as c_short),
7979
Length::Long => SignedInt::Long(args.arg()),
8080
Length::LongLong => SignedInt::LongLong(args.arg()),
8181
// for some reason, these exist as different options, yet produce the same output
@@ -85,8 +85,8 @@ impl Length {
8585
unsafe fn parse_unsigned(self, args: &mut VaList) -> UnsignedInt {
8686
match self {
8787
Length::Int => UnsignedInt::Int(args.arg()),
88-
Length::Char => UnsignedInt::Char(args.arg::<u32>() as u8),
89-
Length::Short => UnsignedInt::Short(args.arg::<u32>() as u16),
88+
Length::Char => UnsignedInt::Char(args.arg::<c_uint>() as c_uchar),
89+
Length::Short => UnsignedInt::Short(args.arg::<c_uint>() as c_ushort),
9090
Length::Long => UnsignedInt::Long(args.arg()),
9191
Length::LongLong => UnsignedInt::LongLong(args.arg()),
9292
// for some reason, these exist as different options, yet produce the same output
@@ -197,7 +197,21 @@ pub unsafe fn format(
197197
Specifier::String(CStr::from_ptr(arg))
198198
}
199199
}
200-
b'c' => Specifier::Char(args.arg::<u32>() as u8),
200+
b'c' => {
201+
trait CharToInt {
202+
type IntType;
203+
}
204+
205+
impl CharToInt for c_schar {
206+
type IntType = c_int;
207+
}
208+
209+
impl CharToInt for c_uchar {
210+
type IntType = c_uint;
211+
}
212+
213+
Specifier::Char(args.arg::<<c_char as CharToInt>::IntType>() as c_char)
214+
}
201215
b'p' => Specifier::Pointer(args.arg()),
202216
b'n' => Specifier::WriteBytesWritten(written, args.arg()),
203217
_ => return -1,

0 commit comments

Comments
 (0)