From f9f9a8ef19dcee4fb836172d762c168e14aef415 Mon Sep 17 00:00:00 2001 From: JKRT Date: Sat, 3 Oct 2026 15:39:06 +0200 Subject: [PATCH] String(): format with Printf, not C's snprintf (0.5.2) Windows exports no snprintf symbol (the Universal CRT defines it inline): modelica_String(r, significantDigits, minimumLength, leftJustified) and modelica_String(r, format) threw "could not load symbol snprintf" there (8 errors in modelicaBuiltinsTests on master's Windows CI). Printf takes the same C conversions; a `*.*` width and precision are written into the format. 376/376. Assisted-by: Claude Opus 5.5 --- Project.toml | 3 ++- src/CodeGeneration/modelicaBuiltins.jl | 16 +++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 9839f35..041b901 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "OMBackend" uuid = "9400bd5f-7561-42f6-8760-03756753da50" -version = "0.5.1" +version = "0.5.2" authors = ["John Tinnerholm ", "Karim Abdelhak ", "AnHeuermann "] [deps] @@ -33,6 +33,7 @@ OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" SCode = "350d45b0-c210-11e9-3b9e-fdad65bb3d46" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" diff --git a/src/CodeGeneration/modelicaBuiltins.jl b/src/CodeGeneration/modelicaBuiltins.jl index 4184e56..e2be0fb 100644 --- a/src/CodeGeneration/modelicaBuiltins.jl +++ b/src/CodeGeneration/modelicaBuiltins.jl @@ -12,6 +12,7 @@ =# import LinearAlgebra +import Printf import Symbolics #= ============================================================================ @@ -532,16 +533,13 @@ function modelica_String(x::Real, format::AbstractString) return _cFormat("%" * format, Float64(x)) end -#= C's snprintf of one double, after the width and precision of a `*.*` format. =# +#= C's printf of one double, after the width and precision of a `*.*` format, + through Julia's Printf (the same conversions, as C): Windows exports no + snprintf symbol (the Universal CRT defines it inline), and the ccall failed + there ("could not load symbol", 2026-10-03). =# function _cFormat(fmt::String, x::Float64; width::Union{Integer, Nothing} = nothing, precision::Integer = 0) - local n = width === nothing ? - @ccall(snprintf(C_NULL::Ptr{UInt8}, 0::Csize_t, fmt::Cstring; x::Cdouble)::Cint) : - @ccall(snprintf(C_NULL::Ptr{UInt8}, 0::Csize_t, fmt::Cstring; Cint(width)::Cint, Cint(precision)::Cint, x::Cdouble)::Cint) - local buf = Vector{UInt8}(undef, n + 1) - width === nothing ? - @ccall(snprintf(buf::Ptr{UInt8}, (n + 1)::Csize_t, fmt::Cstring; x::Cdouble)::Cint) : - @ccall(snprintf(buf::Ptr{UInt8}, (n + 1)::Csize_t, fmt::Cstring; Cint(width)::Cint, Cint(precision)::Cint, x::Cdouble)::Cint) - return String(resize!(buf, n)) + width === nothing || (fmt = replace(fmt, "*.*" => string(width, ".", precision))) + return Printf.format(Printf.Format(fmt), x) end #= A call of Modelica's String with the arguments the frontend fills in, each