verifyEq(Format.printf("%s, world!", ["Hello"]), "Hello, world!") //simple test
verifyEq(Format.printf("%010s", ["a"]), "000000000a") //test pad zeroes
verifyEq(Format.printf("%.1s", ["ab"]), "a") //test precision
verifyEq(Format.printf("%5s -> %-5s", ["a", "b"]), " a -> b ") //test 2 args
verifyEq(Format.printf("%+05d", [10]), "+0010") //test padding spaces
Or like this:
f := Format.compile("%d")
str1 := f.format([5])
str2 := f.format([3])
I've not yet added support for floating types, but I'll add it in a few days.
I did it because several times in the past I really missed feature like this. Str interpolation rocks in most of cases, but sometimes printf is better:
you can save format strings in resources, construct them in runtime and so on
more complex formatting in a simpler way. Even such simple case like this:
However existing implementations (at least in Java, Tcl, bash and C) have a lot of differences in flag processing and supported types, so it's necessary to either thoroughly document the format of this library, or take existing spec from Java or C and implement as close as possible to refer the doc.
If anyone is interested in this lib, please provide your feedback, and if anyone wants to contribute, let me know and I can give a write access to repo.
ivan Wed 8 Sep 2010
Today I've started working on pure Fantom impl of printf-style formatting, the project is shared on bitbucket: http://bitbucket.org/ivan_inozemtsev/printf
It works like this now:
Or like this:
I've not yet added support for floating types, but I'll add it in a few days.
I did it because several times in the past I really missed feature like this. Str interpolation rocks in most of cases, but sometimes printf is better:
for me looks much clearer than
However existing implementations (at least in Java, Tcl, bash and C) have a lot of differences in flag processing and supported types, so it's necessary to either thoroughly document the format of this library, or take existing spec from Java or C and implement as close as possible to refer the doc.
If anyone is interested in this lib, please provide your feedback, and if anyone wants to contribute, let me know and I can give a write access to repo.