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

Alternative integer format #120

Closed
cobura2 opened this issue Feb 25, 2015 · 3 comments
Closed

Alternative integer format #120

cobura2 opened this issue Feb 25, 2015 · 3 comments

Comments

@cobura2
Copy link

cobura2 commented Feb 25, 2015

I noted in the documentation that the format syntax is based on python. In python two forms of syntax are allowed for integer both "d" and "i". see following link

https://docs.python.org/2/library/stdtypes.html#string-formatting

This code gives an exception as "i" integer format is not allowed:

fmt::print("int: {0:d}; alternative int: {0:i}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);

Should you allow the "i" format in a later release ?

@vitaut
Copy link
Contributor

vitaut commented Feb 25, 2015

Thanks for the suggestions, but the format syntax is mostly based on str.format where d is used for decimal integers. The one you refer to applies to the Python's interpolation operator % and is similar to printf. The C++ Format library already support "i" specifier in fmt::printf:

fmt::printf("%i", 42);

@vitaut vitaut closed this as completed Feb 25, 2015
@cobura2
Copy link
Author

cobura2 commented Feb 26, 2015

Hi Victor

Thanks for the reply. I see now that the format syntax is very closely aligned with Python’s str.format function. To be honest I was looking at ways where I could maybe contribute to your open source project. I spent 4 hours looking at it yesterday and I couldn’t see any holes in it that I could quickly fill.

Could you let me know what the main reasons why someone would use fmt::printf over normal c library printf. Is it to get the exception handling support ?

Please let me know if there is anything I can help you with. My main programming language is C++ but lately I have been also programming in python

Thanks for your prompt reply.

Best regards

Andrew

From: Victor Zverovich [mailto:notifications@github.com]
Sent: February-24-15 11:09 PM
To: cppformat/cppformat
Cc: Andrew Coburn
Subject: Re: [cppformat] Alternative integer format (#120)

Thanks for the suggestions, but the format syntax is mostly based on str.format https://docs.python.org/2/library/string.html#formatstrings where d is used for decimal integers. The one you refer to applies to the Python's interpolation operator % and is similar to printf. The C++ Format library already support "i" specifier in fmt::printf:

fmt::printf("%i", 42);


Reply to this email directly or view it on GitHub #120 (comment) . https://github.com/notifications/beacon/AKqa2JcVZ2pmJMxBPzKZCMrv7KUzlNJQks5nvUJIgaJpZM4DlXEA.gif

@vitaut
Copy link
Contributor

vitaut commented Feb 26, 2015

Hi Andrew,

It's great that you are interested in contributing to C++ Format.

Could you let me know what the main reasons why someone would use fmt::printf over normal c library printf. Is it to get the exception handling support ?

fmt::(s)printf has several advantages over libc's (s)printf:

  • Safety: libc's printf relies on varargs which are inherently unsafe while C++ Format uses variadic functions (emulated on pre-C++11). Safety issues in printf can be addressed with attribute ((format (printf, ...)), but it is not portable and only works with literal strings while in practice format strings can be dynamic (esp. when localized).
  • Extensibility: fmt::printf supports formatting of user defined types.
  • Performance: fmt::printf is much faster on integer formatting. (It is currently slower on floating-point formatting, but I plan to address this soon.)
  • Portability: According to 0 A.D. developers, fmt::printf is more portable than libc's printf. In particular, it provides consistent output across platforms and implements POSIX extensions for positional arguments that are not available in some printf implementations.

That said, fmt::printf is mostly there to simplify migration for the code that already uses system printf. fmt::format's string syntax inherited from Python's str.format is more expressive and can be easily extended for custom types (although this is not used in the library yet).

Please let me know if there is anything I can help you with. My main programming language is C++ but lately I have been also programming in python

I suggest choosing something you are interested in working on from Issues. For example, implementing custom streambuf in Issue #92 or replacing use of BasicStringRef::c_str() with data() and size() in #100. Feel free to ask if you have any questions.

Thanks,
Victor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants