Coder Social home page Coder Social logo

pprintpp's Issues

Mixing format syles causes wrong output

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)

reorder printf paramters

As you wrote in the FAQ, printfwill 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.

AVOID_STL requires STL includes

When compiling with -DPPRINTPP_AVOID_STL the file stl_symbols.hpp still requires cstddef. I'd suggest to include stddef.h in that case.

Explicit hexadecimal format with signed argument

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.

C++11 Lambda expression instead of local class/struct in AUTOFORMAT

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

pprintpp needs extra cstdio include

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.

Explicit length identifier broken

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

Add support for string view?

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());

Thank you!

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 :)

tie_types ... no implementation ?

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

https://github.com/dbj-systems/pprintpp_msvc

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 ...

Header soup

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);
}

How to integrate with user defined types

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.