Skip to content

Commit 619e2d9

Browse files
committed
Use pure Rust for format_decimal_float to fix Windows build
libc::snprintf is not available on Windows in the libc crate. Replace with pure Rust formatting that mimics %g behavior.
1 parent e52418b commit 619e2d9

1 file changed

Lines changed: 12 additions & 32 deletions

File tree

  • rust/spirv-tools-core/src/disassembly

rust/spirv-tools-core/src/disassembly/mod.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use rspirv::dr::{self, Block, Function, Instruction, Module, ModuleHeader, Opera
33
use rspirv::grammar::{GlslStd450InstructionTable, OpenCLStd100InstructionTable};
44
use rspirv::spirv;
55
use std::collections::{HashMap, HashSet};
6-
use std::ffi::CString;
76
use std::mem::size_of_val;
87
use std::num::FpCategory;
98
use std::slice;
@@ -904,37 +903,18 @@ fn format_f64_literal(bits: u64) -> String {
904903
}
905904

906905
fn format_decimal_float(value: f64, digits: usize) -> String {
907-
let format = match CString::new(format!("%.{digits}g")) {
908-
Ok(fmt) => fmt,
909-
Err(_) => return value.to_string(),
910-
};
911-
let mut stack = [0u8; 128];
912-
unsafe {
913-
let len = libc::snprintf(
914-
stack.as_mut_ptr() as *mut libc::c_char,
915-
stack.len(),
916-
format.as_ptr(),
917-
value as libc::c_double,
918-
);
919-
if len < 0 {
920-
return value.to_string();
921-
}
922-
let len = len as usize;
923-
if len < stack.len() {
924-
return String::from_utf8_lossy(&stack[..len]).into_owned();
925-
}
926-
let mut heap = vec![0u8; len + 1];
927-
let retry = libc::snprintf(
928-
heap.as_mut_ptr() as *mut libc::c_char,
929-
heap.len(),
930-
format.as_ptr(),
931-
value as libc::c_double,
932-
);
933-
if retry < 0 {
934-
value.to_string()
935-
} else {
936-
String::from_utf8_lossy(&heap[..retry as usize]).into_owned()
937-
}
906+
// Use pure Rust formatting - format with specified precision then trim trailing zeros
907+
// This mimics C's %g format specifier behavior
908+
let formatted = format!("{:.prec$}", value, prec = digits);
909+
910+
// If it contains a decimal point, trim trailing zeros (like %g does)
911+
if formatted.contains('.') {
912+
let trimmed = formatted.trim_end_matches('0');
913+
// Don't leave a trailing decimal point
914+
let trimmed = trimmed.trim_end_matches('.');
915+
trimmed.to_string()
916+
} else {
917+
formatted
938918
}
939919
}
940920

0 commit comments

Comments
 (0)