diff --git a/src/lib.rs b/src/lib.rs index e99335d..41ecf91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -400,7 +400,7 @@ pub mod argument { /// need to null terminate a string to print it, you can skip that step. String(&'a CStr), /// `c` - Char(u8), + Char(c_char), /// `x` Hex(UnsignedInt), /// `X` diff --git a/src/output.rs b/src/output.rs index 3853935..70f0e69 100644 --- a/src/output.rs +++ b/src/output.rs @@ -274,9 +274,9 @@ pub fn fmt_write(w: &mut impl fmt::Write) -> impl FnMut(Argument) -> c_int + '_ }, Specifier::Char(data) => { if flags.contains(Flags::LEFT_ALIGN) { - write!(w, "{:width$}", data as char, width = width as usize) + write!(w, "{:width$}", data as u8 as char, width = width as usize) } else { - write!(w, "{:>width$}", data as char, width = width as usize) + write!(w, "{:>width$}", data as u8 as char, width = width as usize) } } Specifier::Pointer(data) => { diff --git a/src/parser.rs b/src/parser.rs index 1a79348..ef14622 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -74,8 +74,8 @@ impl Length { unsafe fn parse_signed(self, args: &mut VaList) -> SignedInt { match self { Length::Int => SignedInt::Int(args.arg()), - Length::Char => SignedInt::Char(args.arg::() as i8), - Length::Short => SignedInt::Short(args.arg::() as i16), + Length::Char => SignedInt::Char(args.arg::() as c_schar), + Length::Short => SignedInt::Short(args.arg::() as c_short), Length::Long => SignedInt::Long(args.arg()), Length::LongLong => SignedInt::LongLong(args.arg()), // for some reason, these exist as different options, yet produce the same output @@ -85,8 +85,8 @@ impl Length { unsafe fn parse_unsigned(self, args: &mut VaList) -> UnsignedInt { match self { Length::Int => UnsignedInt::Int(args.arg()), - Length::Char => UnsignedInt::Char(args.arg::() as u8), - Length::Short => UnsignedInt::Short(args.arg::() as u16), + Length::Char => UnsignedInt::Char(args.arg::() as c_uchar), + Length::Short => UnsignedInt::Short(args.arg::() as c_ushort), Length::Long => UnsignedInt::Long(args.arg()), Length::LongLong => UnsignedInt::LongLong(args.arg()), // for some reason, these exist as different options, yet produce the same output @@ -197,7 +197,21 @@ pub unsafe fn format( Specifier::String(CStr::from_ptr(arg)) } } - b'c' => Specifier::Char(args.arg::() as u8), + b'c' => { + trait CharToInt { + type IntType; + } + + impl CharToInt for c_schar { + type IntType = c_int; + } + + impl CharToInt for c_uchar { + type IntType = c_uint; + } + + Specifier::Char(args.arg::<::IntType>() as c_char) + } b'p' => Specifier::Pointer(args.arg()), b'n' => Specifier::WriteBytesWritten(written, args.arg()), _ => return -1,