A question about FLAGS in lstrlib.c

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

A question about FLAGS in lstrlib.c

Marc Balmer
Hi

In lstrlib.c, the preprocessor define FLAGS defines the flag characters that be used in string formatting (string.format()). It includes flags like 0, -, space, etc.  The flag to add thousands separators to a number ('), however, is missing.  Is there a reason for that?

I can use

print(string.format("%.2f", 1000000))

to print a number,  but with "stock" Lua I can not use

print(string.format("%'.2f", 1000000))

to print the number with thousands separator.

Adding an apostrophe to FLAGS in lstrlib.c makes it perfectly word.

I understand that the thousands separator flag is not part of C89, but a SuSv2 extension, which seems to be present on almost all (modern) operating systems.

Could FLAGS be something that could be overriden in luaconf.h?  It would be as simple as enclosing the definition of FLAGS in a #ifndef  FLAGS / #endif bracket.

Thanks,
Marc


Reply | Threaded
Open this post in threaded view
|

Re: A question about FLAGS in lstrlib.c

Lorenzo Donati-3
On 15/10/2019 14:14, Marc Balmer wrote:

> Hi
>
> In lstrlib.c, the preprocessor define FLAGS defines the flag
> characters that be used in string formatting (string.format()). It
> includes flags like 0, -, space, etc.  The flag to add thousands
> separators to a number ('), however, is missing.  Is there a reason
> for that?
>
> I can use
>
> print(string.format("%.2f", 1000000))
>
> to print a number,  but with "stock" Lua I can not use
>
> print(string.format("%'.2f", 1000000))
>
> to print the number with thousands separator.
>
> Adding an apostrophe to FLAGS in lstrlib.c makes it perfectly word.
>
> I understand that the thousands separator flag is not part of C89,
> but a SuSv2 extension, which seems to be present on almost all
> (modern) operating systems.
>

I don't think it is part of ANY C standard, if I read correctly the info
at cppreference.com:

https://en.cppreference.com/w/c/io/fprintf

Although I consider that a very useful feature, messing with the default
flags could be dangerous for code compatibility IMO, as long as
Lua delegates the underlying formatting to C libraries.

Should Lua ever implemented a more complete string formatting facility,
I'd welcome that feature.

> Could FLAGS be something that could be overriden in luaconf.h?  It
> would be as simple as enclosing the definition of FLAGS in a #ifndef
> FLAGS / #endif bracket.
>
> Thanks, Marc
>
>
>

cheers!

-- Lorenzo

Reply | Threaded
Open this post in threaded view
|

Re: A question about FLAGS in lstrlib.c

Marc Balmer


> Am 15.10.2019 um 16:43 schrieb Lorenzo Donati <[hidden email]>:
>
> On 15/10/2019 14:14, Marc Balmer wrote:
>> Hi
>>
>> In lstrlib.c, the preprocessor define FLAGS defines the flag
>> characters that be used in string formatting (string.format()). It
>> includes flags like 0, -, space, etc.  The flag to add thousands
>> separators to a number ('), however, is missing.  Is there a reason
>> for that?
>>
>> I can use
>>
>> print(string.format("%.2f", 1000000))
>>
>> to print a number,  but with "stock" Lua I can not use
>>
>> print(string.format("%'.2f", 1000000))
>>
>> to print the number with thousands separator.
>>
>> Adding an apostrophe to FLAGS in lstrlib.c makes it perfectly word.
>>
>> I understand that the thousands separator flag is not part of C89,
>> but a SuSv2 extension, which seems to be present on almost all
>> (modern) operating systems.
>>
>
> I don't think it is part of ANY C standard, if I read correctly the info
> at cppreference.com:
>
> https://en.cppreference.com/w/c/io/fprintf

As I wrote, it is a SUSv2 extension...

>
> Although I consider that a very useful feature, messing with the default flags could be dangerous for code compatibility IMO, as long as
> Lua delegates the underlying formatting to C libraries.
>
> Should Lua ever implemented a more complete string formatting facility, I'd welcome that feature.
>
>> Could FLAGS be something that could be overriden in luaconf.h?  It
>> would be as simple as enclosing the definition of FLAGS in a #ifndef
>> FLAGS / #endif bracket.
>>
>> Thanks, Marc
>>
>>
>>
>
> cheers!
>
> -- Lorenzo
>


Reply | Threaded
Open this post in threaded view
|

Re: A question about FLAGS in lstrlib.c

Roberto Ierusalimschy
In reply to this post by Marc Balmer
> Could FLAGS be something that could be overriden in luaconf.h?  It
> would be as simple as enclosing the definition of FLAGS in a #ifndef
> FLAGS / #endif bracket.

https://github.com/lua/lua/commit/6055a039b57b405adc268672caeb682ef2a551ee

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: A question about FLAGS in lstrlib.c

bil til
This post was updated on .
you might consider to include the "small printf" of mpaland (MIT license) in
the lua code.

This is quite recent (from 2018 I think - you find it with Google. This
gives a good impression, looks nicely programmed. (I did not test it myself
yet...). With this then it would be no problem to adapt this.

For controller application you anyway need something like this, as it is MUCH
more compact compared to the standard C implementation of printf. At least
if you look at the GCC source code of printf, you really get grey hairs... it
looks REALLY terrible ... a bit a shame for GCC.

For me it would be also a very nice thing to include something like "%.3u"
as float specifier to allow "scientific unit notation" with milli, kilo, ...
(a, f, n, u, m, ' ', k, M, G, T, P). But this really is a bit too much
probably, ... just side remark... . You can do such formatting always also
"by some pre-function", then generating a new float and some additional
"unit char". But if it works directly this always has the VERY large
advantage, that you can possilby use the same formatting string for printf
and scanf (though scanf not available for lua... no problem.. at least a
"strtonumber" function supporting such format string sometimes would be
nice, but not really ímportant of course).



--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

Reply | Threaded
Open this post in threaded view
|

Re: A question about FLAGS in lstrlib.c

Marc Balmer
In reply to this post by Roberto Ierusalimschy
Cool! Thank you.

> Am 21.10.2019 um 23:24 schrieb Roberto Ierusalimschy <[hidden email]>:
>
> 
>>
>> Could FLAGS be something that could be overriden in luaconf.h?  It
>> would be as simple as enclosing the definition of FLAGS in a #ifndef
>> FLAGS / #endif bracket.
>
> https://github.com/lua/lua/commit/6055a039b57b405adc268672caeb682ef2a551ee
>
> -- Roberto
>