locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sprintf formatting regression

ray007 opened this issue · comments

  • Have you checked the guidelines in our Contributing document?

Description

After upgrading to the current version of sprintf.js, a formatter I'm using in my code does not work anymore.
The format string %.*f now results in "f" instead of a number with the precision taken from the argument list.

In last patch I improved compatibility with PHP. The format you've used is not valid (per http://php.net/sprintf).
Previous sprintf implementation differed a lot from PHP's sprintf implementation.

Now a question to @kvz : what strategy should I follow? Keep backward compatibility with older locutus releases in mind or mimic PHP as close as possible?

Sounds like a bug in the php implementation to me. At least when looking at something like https://en.wikipedia.org/wiki/Printf_format_string#Precision_field

It's not just a breaking change, it's removing a feature.

It's now a tough nut to crack, because restoring the feature means breaking PHP compability.
If we can call it a PHP bug? Not sure, it's maybe on purpose, because PHP's sprintf is not just a wrapper around the C-function. Here is the precision parsing, and here applying the precision. I don't see any logic which is derived from .* format

My plan is to port the C version of sprintf in the C-lang namespace of locutus. Hope this solution suits you.

Sounds good to me. Thank you.
When you do so, could you please trigger this thread here with another comment?

@ray007 I've prepared a pull request (#368) implementing sprintf.
I tried to be as close as possible to the rules you've linked previously.
The implementation is not yet 100% complete, but I'll extend the function with new patches.
Please check if it works as expected for you.

I will close the issue. In case there are problems with the new implementation, please raise new tickets.

Thanks.
I haven't yet updated since my target environment is ES5 only, will hopefully find time soon...