Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted indentation when printing null-terminated strings with StdStream.print #4171

Closed
ergl opened this issue Aug 5, 2022 · 0 comments · Fixed by #4180
Closed

Unwanted indentation when printing null-terminated strings with StdStream.print #4171

ergl opened this issue Aug 5, 2022 · 0 comments · Fixed by #4180
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ergl
Copy link
Member

ergl commented Aug 5, 2022

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:

PONY_API void pony_os_std_print(FILE* fp, char* buffer, size_t len)
{
if(len == 0)
fprintf(fp, "\n");
else
fprintf(fp, "%*.*s\n", (int)len, (int)len, buffer);
}

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.

@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Aug 5, 2022
@ergl ergl added bug Something isn't working discuss during sync Should be discussed during an upcoming sync and removed discuss during sync Should be discussed during an upcoming sync labels Aug 5, 2022
@SeanTAllen SeanTAllen added help wanted Extra attention is needed good first issue Good for newcomers labels Aug 5, 2022
@ponylang-main ponylang-main removed the discuss during sync Should be discussed during an upcoming sync label Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants