tfc / pprintpp Goto Github PK
View Code? Open in Web Editor NEWTypesafe Python Style Printf Formatting for C++
License: MIT License
Typesafe Python Style Printf Formatting for C++
License: MIT License
Would it be possible to add support for printing non-null terminated string views using the precision spec, e.g.
std::string_view s = "world";
pprintf("hello {}!\n", s);
would generate
printf("%.*s\n", s.size(), s.data());
Would it be possible to compile-time assert on trying to print an empty string_view
?
Current implementation, silently replaces hexadecimal specifier with decimal one in the case of signed argument. Eg, pprintfpp("{x}", 1)
translates to printf("%d", 1)
which is somewhat unexpected. In my opinion, it is better to have the "%x", as it doesn't break anything and does what the user asked for.
I wonder, if it would be possible to add support for user defined types and if so, how. Or if not, where would one add support for user defined types. Ideas?
struct foo {
} my_foo;
std::size_t print( char* buffer, std::size_t buffer_size, const foo& );
pprintpp( "This is my foo: {}", my_foo );
Could we somehow extend pprintpp to have print()
invoked with some statically allocated buffer for formatting purpose?
I know, pprintpp() is "just" creating printf style format strings, but in real world applications, user defined types have to be added sooner or later.
When compiling with -DPPRINTPP_AVOID_STL
the file stl_symbols.hpp
still requires cstddef
. I'd suggest to include stddef.h
in that case.
Fixed it on the level of the AUTOFORMAT
macro, by using lambda inside a macro. Compiles, runs, tests ok. But.
MSVC breaks under (what it thinks are) too complex recursive templates of the 'TYPE LIST' used. See the one example in the main()
.
CLANG + VS2019 -- no issues whatsoever ...
I just wanted to say, thank you for this awesome library ❤️ . This is such an awesome addition for embedded projects where the cost of a printf bug is high and we've been caught out by it many times.
I was thinking of implementing something like this myself but then I came across this after some searching. Once we start using it a lot, you'll probably see PRs coming through from us :)
fmtlib offers grouping:
setlocale(LC_NUMERIC, "en_US.UTF-8");
fmt::print("{:n}", 45000); // prints "45,000"
pprintf()
expands to a call to printf()
yet it doesn't include cstdio
. This leads to the somewhat confusing error seen below:
src/main.cpp: In function ‘int main()’:
src/main.cpp:65:3: error: ‘printf’ was not declared in this scope
65 | pprintf("Hello World!\n");
| ^~~~~~~
user-virtio-net/src/main.cpp:8:1: note: ‘printf’ is defined in header ‘<cstdio>’; did you forget to ‘#include <cstdio>’?
Minimal example of the problem:
#include <pprintpp/pprintpp.hpp>
int main()
{
pprintf("Hello World!\n");
return 0;
}
For me the expected behavior would be for this to work without manually including other headers.
Other projects include system headers as needed, as well as setting locale as
needed, leading to clean code:
#include <fmt/format.h>
int main() {
fmt::print("{:n}\n", 1000);
}
pprintpp does not include system headers as needed, and does not set locale as
needed, leading to verbose code:
#include "pprintpp.hpp"
#include <clocale>
#include <cstdio>
int main() {
setlocale(LC_ALL, "");
pprintf("{'}\n", 1000);
}
Hi,
I just found this issue today.
Consider the following code:
uint64_t val = 0x8ff00000000;
pprintf("{#08x}\n", val);
Expected output:
0x00000000
Actual output:
0x8ff00000000
As you wrote in the FAQ, printf
will not reorder arguments as in the example of fmt: fmt::format("{0}{1}{0}", "abra", "cad");
but wouldn't it be possible to create a wrapper function that looks up a translation from the format string and rearranges the parameters before calling printf
? This would allow use to create binaries that contains only the literals for a specific language and still supports a usage like gettext
.
Example:
printtext( "{1} beer to {0} guests.", 4, 2 );
could lookup the translation of "{1} beer to {0} guests."
to the target language, check the arguments and then generate a function that rearranges the arguments when calling printf.
using MSVC (the latest, all up to date), does not compile.
pprintpp.hpp , arround line 166:
template <typename ... Ts>
make_t<Ts...> tie_types(Ts...);
it seems MSVC can not compile that ...
ps: using clang all works
If you mix classic %[] formats with {} the output if broken. In some cases you get double 0x, other following parameters are cased to the wrong size.
pprintf("test %s {#x} {} {x}", c_string, uint64_t, void_ptr, int)
Hey,
Will trying to use pprintpp with MSVC, CLANG and Compilers for Microcontrollers (Aurix TriCore) I had some issues with the AUTOFORMAT Macro - not every Compiler seems to support the "local class" scheme used with the ({struct....}) statement.
Hence, I modified the AUTOFORMAT Macro to use a lambda expression which gets called - I am not aware if this will cause performance-issues, so maybe you can give me additional advice?
This approach is compatible with at least all the compilers I have tried (especially MSVC (Visual Studio Build Tools 2019) caused problems).
Here my modification:
#define AUTOFORMAT(s, ...) \
[]() { \
struct strprov \
{ \
static constexpr const char *str() { return static_cast<const char *>(s); } \
}; \
using paramtypes = decltype(pprintpp::tie_types(__VA_ARGS__)); \
using af = pprintpp::autoformat_t<strprov, paramtypes>; \
return af::str(); \
}()
#define pprintf(s, ...) printf(AUTOFORMAT(s, ## __VA_ARGS__), ## __VA_ARGS__);
}
If you are interested, I could do a fork and make a pull request.
Kind regards
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.