Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion libcc2rs/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,54 @@ fn pad(out: &mut Vec<u8>, spec: &ConversionSpecifier, s: &[u8]) {
}
}

// Zero has no logarithmic exponent. Format it without sprintf's normalization.
fn float_zero(out: &mut Vec<u8>, spec: &ConversionSpecifier, negative: bool) {
let NumericParam::Literal(precision) = spec.precision else {
unreachable!()
};
let scientific = matches!(
spec.conversion_type,
ConversionType::SciFloatLower | ConversionType::SciFloatUpper
);
let precision = if scientific {
precision as usize
} else if spec.alt_form {
precision.max(1) as usize - 1
} else {
0
};
let mut number = Vec::new();
if negative {
number.push(b'-');
} else if spec.force_sign {
number.push(b'+');
} else if spec.space_sign {
number.push(b' ');
}
let point = precision > 0 || spec.alt_form;
if spec.zero_pad && !spec.left_adj {
let NumericParam::Literal(width) = spec.width else {
unreachable!()
};
let len =
number.len() + 1 + usize::from(point) + precision + if scientific { 4 } else { 0 };
number.resize(number.len() + (width as usize).saturating_sub(len), b'0');
}
number.push(b'0');
if point {
number.push(b'.');
}
number.resize(number.len() + precision, b'0');
if scientific {
number.extend_from_slice(if spec.conversion_type == ConversionType::SciFloatUpper {
b"E+00"
} else {
b"e+00"
});
}
pad(out, spec, &number);
}

fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg {
match conversion_type {
ConversionType::DecInt => match arg {
Expand Down Expand Up @@ -87,7 +135,7 @@ fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg {

// Formats like C's printf. The format, the strings and the result are bytes,
// which need not be UTF-8, so %s and %c are formatted here; sprintf formats
// the numbers, which are ASCII.
// the numbers, which are ASCII, except for scientific and compact zero.
pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec<u8> {
let mut out = Vec::with_capacity(fmt.len());
let mut va = va.iter();
Expand Down Expand Up @@ -121,6 +169,19 @@ pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec<u8> {
}
conversion_type => {
let arg = numeric_arg(conversion_type, arg);
if let Arg::Double(value) = arg
&& value == 0.0
&& matches!(
conversion_type,
ConversionType::SciFloatLower
| ConversionType::SciFloatUpper
| ConversionType::CompactFloatLower
| ConversionType::CompactFloatUpper
)
{
float_zero(&mut out, &spec, value.is_sign_negative());
continue;
}
match vsprintfp(&[FormatElement::Format(spec)], &[arg.as_printf()]) {
Ok(s) => out.extend_from_slice(s.as_bytes()),
Err(e) => panic!("format_c: cannot format {spec:?}: {e:?}"),
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/out/refcount/printf_zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
printf!(b"%e|%E\n", 0_f64, -0_f64);
printf!(b"%.0e|%#.0E|%02.2e\n", 0_f64, -0_f64, 0_f64);
printf!(b"%+ 012.2e|%-#012.0E|% 12.1e\n", 0_f64, -0_f64, 0_f64);
printf!(b"% .1e|% 012.1e\n", 0_f64, 0_f64);
printf!(b"%g|%G|%#g\n", 0_f64, -0_f64, 0_f64);
printf!(
b"%.0g|%.1g|%#.0g|%#.1G|%#.4g\n",
0_f64,
-0_f64,
0_f64,
-0_f64,
0_f64
);
printf!(
b"%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n",
0_f64,
0_f64,
0_f64,
-0_f64,
0_f64
);
printf!(b"%f|%F|%e|%g\n", 0_f64, -0_f64, 1500_f64, 0.5_f64);
return 0;
}
pub fn __cpp2rust_init_globals() {}
88 changes: 88 additions & 0 deletions tests/unit/out/unsafe/printf_zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe {
libc::printf(
(c"%e|%E\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
(0_f64),
(-0_f64),
)
});
(unsafe {
libc::printf(
(c"%.0e|%#.0E|%02.2e\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
(0_f64),
(-0_f64),
(0_f64),
)
});
(unsafe {
libc::printf(
(c"%+ 012.2e|%-#012.0E|% 12.1e\n".as_ptr().cast_mut()).cast_const()
as *const libc::c_char,
(0_f64),
(-0_f64),
(0_f64),
)
});
(unsafe {
libc::printf(
(c"% .1e|% 012.1e\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
(0_f64),
(0_f64),
)
});
(unsafe {
libc::printf(
(c"%g|%G|%#g\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
(0_f64),
(-0_f64),
(0_f64),
)
});
(unsafe {
libc::printf(
(c"%.0g|%.1g|%#.0g|%#.1G|%#.4g\n".as_ptr().cast_mut()).cast_const()
as *const libc::c_char,
(0_f64),
(-0_f64),
(0_f64),
(-0_f64),
(0_f64),
)
});
(unsafe {
libc::printf(
(c"%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n".as_ptr().cast_mut()).cast_const()
as *const libc::c_char,
(0_f64),
(0_f64),
(0_f64),
(-0_f64),
(0_f64),
)
});
(unsafe {
libc::printf(
(c"%f|%F|%e|%g\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
(0_f64),
(-0_f64),
(1500_f64),
(0.5_f64),
)
});
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
13 changes: 13 additions & 0 deletions tests/unit/printf_zero.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

int main() {
printf("%e|%E\n", 0.0, -0.0);
printf("%.0e|%#.0E|%02.2e\n", 0.0, -0.0, 0.0);
printf("%+ 012.2e|%-#012.0E|% 12.1e\n", 0.0, -0.0, 0.0);
printf("% .1e|% 012.1e\n", 0.0, 0.0);
printf("%g|%G|%#g\n", 0.0, -0.0, 0.0);
printf("%.0g|%.1g|%#.0g|%#.1G|%#.4g\n", 0.0, -0.0, 0.0, -0.0, 0.0);
printf("%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n", 0.0, 0.0, 0.0, -0.0, 0.0);
printf("%f|%F|%e|%g\n", 0.0, -0.0, 1500.0, 0.5);
return 0;
}
Loading