Issue with fmt width flag and zero padding?

Rather new to Odin, coming back after a while. I just got something strange (at least to me!) with the width flag of fmt format(s).

The project is a CLI filter reading a text file with a bunch of data and converting it into Odin sources to insert into another project.

With the statement:

text	= fmt.bprintf(buffer[:], "{{\"%s\", %2d, %3i}}", a_string, an_int, another_int);
_		= os.write_string(output, text)	or_return;

with two format fields with width (%2d and %3i, without 0 padding flag) I expect to find in the text variable the text:
{"string", 1, 0}
WITHOUT 0 padding, instead I get
{"string", 01, 000}
WITH 0 padding.

I also tried the statement:

if (fmt.fprintf(output, "{{\"%s\", %2d, %3i}}",  a_string, an_int, another_int) == 0)
{ return io.Error.Short_Write }

and I get the same exact strings.

It seems the with flag for integers automatically turns 0 padding on.

Is this expected? Is it possible to have a width WITHOUT 0 padding? Thanks!

(Note: using d for one format and i for the other is just a test to see if it makes any difference: it does not).

If what you want is spaces instead of 0s ,
simply add one space after the % , like this:

"{{\"%s\", % 2d, % 3i}}" 
1 Like

Thank you for the quick reply! I tried and it works indeed!

Perhaps it would be useful to add this detail to the fmt package documentaqtion:

in all the languages I know using the %... formatting style (at least C, C++, Java, …) the default is padding with spaces and require a specific flag (‘0’) to change to padding with 0’s (which is also documented for Odin, while the ’ ’ flag is documented for a different purpose).

In practice Odin chose an opposite default, and developers coming from other languages (which is far from rare) might be surprised and waste time on a minimal detail: doesn’t sound good for the “joy of programming”! :wink:

Thanks again, though!

1 Like