ponyc
ponyc copied to clipboard
Unwanted indentation when printing null-terminated strings with `StdStream.print`
The example below prints "Hello, world" when using printf, and " Hello, world" (note the extra leading space) when using @pony_os_std_print (which is what StdStream.print is using).
Note that @pony_os_std_print will put as many leading spaces as \0 are present in the original string.
use @printf[I32](fmt: Pointer[U8] tag, ...)
use @pony_os_stdout[Pointer[U8]]()
use @pony_os_std_print[None](fp: Pointer[U8], buffer: Pointer[U8] tag, len: USize)
actor Main
new create(env: Env) =>
let str = "Hello, world\n\0"
@printf(str.cpointer())
@pony_os_std_print(@pony_os_stdout(), str.cpointer(), str.size())
This was introduced in #1768, which ensured that we respect the size of a string when printing to standard output:
https://github.com/ponylang/ponyc/blob/5978d146d5fd6529e262690734d71081357b5f0c/src/libponyrt/lang/stdfd.c#L516-L522
This is done by using the %* format, which specifies the minimum field width. The manual mentions that if the supplied string is shorter than this, then the result is padded with space characters.
Since fprintf will stop printing as soon as it encounters the first null terminator, there's a mismatch between what is reporter by String.size() and what fprintf considers to be the true size of the string.