Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    devhandle
    @devhandle
    Thanks @stryku, that works great.
    Mateusz Janek
    @stryku
    Np, glad to hear
    Dirk Van Haerenborgh
    @vhdirk
    Hi guys
    is it possible to specify the 'precision/width' of a string field but overflow on the left part?
    Using {:.30s} will always trim the tail. adding the alignment operator just aligns the field after it's been trimmed {:>.30s}
    Victor Zverovich
    @vitaut
    No, but it's possible to do by removing a prefix from a string view, something like:
    std::string_view s = ...
    if (s.size() > 30) s.remove_prefix(s.size() - 30);
    and formatting the resulting string view if needed.
    Dirk Van Haerenborgh
    @vhdirk
    @vitaut thanks, but that would mean that I'd have to do that in code, rather than in the formatter string
    Victor Zverovich
    @vitaut
    Yes. alternatively you could write a custom formatter, but that's probably an overkill.
    Patrick Bos
    @egpbos
    hey, i'm working on a multi-process code from which i want to print diagnostic stuff to stdout, and it's quite a lot, so output from different processes is getting mixed up
    i was naively hoping that i could control std::cout's buffer size, but apparently this is an implementation detail and in any case not part of the standard
    so i'm wondering: can fmt help me here?
    do you have / know of examples of using fmt in a multi-process environment?
    note: not multi-threaded, so no shared memory, though I am able to communicate between processes, but would rather not, as I'm doing performance critical stuff
    Patrick Bos
    @egpbos
    the simplest thing i could think of is to just have an adjustable size buffer which i could then tune to only output stuff from each process after some specified criterium to make sure that no output gets scattered across buffer flushes as is happening now
    i guess the easiest would be to just flush after every line, but i'm afraid that would impact my performance too much
    and also, i'm just looking for a good excuse to try out fmt :D
    Victor Zverovich
    @vitaut
    Unfortunately, I don't have any examples how to prevent mixing of output from multiple processes. fmt::print writes the whole message atomically (using one call to the underlying system function), which prevents mixing parts of messages within the process, but I'm not sure if it helps with multiple processes.
    Patrick Bos
    @egpbos
    ok, thx for your response!
    genuine_
    @blaquee
    you might need to implement some internal lock mechanism
    but it would probably throttle output
    Patrick Bos
    @egpbos
    yeah, that's not good
    i think ideally i'd have a separate process that handles output, to which other processes send their output, via a block of shared memory to prevent copies
    but this is a bit outside of the scope of my project ;)
    Vladyslav
    @0xd800

    Hello, people. How is your day?
    Can I get an aid with my problem? I want compile-time checks of parameters correctness (number of param mismatch etc.) with syntax like fmt::format("The answer is {:d}", "forty-two");. Is that possible? From website I only see alternative format(fmt kind of syntax that switches to compile time checks instead of throwing exceptions.
    I tried DCMAKE_FMT_PEDANTIC=ONdidn't seem to help on both gcc and clang, probably it's doing not what I thought it would.

    Reason to do this is to have compile-time checks for another library that uses fmt (named spdlog) while not changing anything on spdlog side...
    I promise I did research myself prior to asking here, sorry for any trouble. I hope someone else had solution for this, spdlog seems to be quite popular and is mentioned in readme as a fellow library.

    Victor Zverovich
    @vitaut
    You need to wrap the format string in the fmt (or FMT_STRING) macro as described in http://fmtlib.net/latest/api.html#compile-time-format-string-checks
    Vladyslav
    @0xd800
    Thank you. I assumed that's for calming down MSVC, that's what I read in a particular paragraph on your old blog post.
    std branch being only branch with this feature enabled is no more actual?
    I didn't know fmt and FMT_STRING effect is in fact equal when it comes to aiming for compile time checks.
    Now it is all clear to me. Thank you again, Victor.
    Victor Zverovich
    @vitaut
    Compile-time checks are in {fmt} since version 5.0, not only in the std branch. Macros are unfortunately the only portable way to have compile-time strings.
    jbeach
    @jasonbeach
    I'm trying to implement a formatter for a couple of user defined types (namely a Point type (struct with 3 doubles)and a Polygon type which is a std::vector<Point>). I have them both working, but am not sure the one for polygon is implemented as efficiently as it could be:
    template <>
    struct formatter<Polygon> {
      template <typename ParseContext>
      constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
    
      template <typename FormatContext>
      auto format(const Polygon &p,
        FormatContext &ctx)
      {
        fmt::memory_buffer out;
        bool is_first = true;
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            format_to(out, "{}", vertice);
            is_first = false;
          }
          else
          {
            format_to(out, ", {}", vertice);
          }
        }
        return format_to(ctx.begin(), "{}", to_string(out));
      }
    };
    }
    Is there a better way?
    Victor Zverovich
    @vitaut
    You could make it more efficient by writing directly to the output iterator ctx.out() instead of intermediate buffer.
    jbeach
    @jasonbeach

    So something like:

      template <typename FormatContext>
      auto format(const Polygon &p,
        FormatContext &ctx)
      {
        //fmt::memory_buffer out;
        bool is_first = true;
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            format_to(ctx.begin(), "{}", vertice);
            is_first = false;
          }
          else
          {
            format_to(ctx.begin(), ", {}", vertice);
          }
        }
        return ctx.begin();
      }

    Seems much better. Thanks!

    jbeach
    @jasonbeach

    So something like

      template <typename FormatContext>
      auto format(const :Polygon &p,
        FormatContext &ctx)
      {
        bool is_first = true;
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            format_to(ctx.begin(), "{}", vertice);
            is_first = false;
          }
          else
          {
            format_to(ctx.begin(), ", {}", vertice);
          }
        }
        return ctx.begin();
      }

    seems much better. Thanks!

    Victor Zverovich
    @vitaut
    Almost. You need to update the iterator between the calls to format_to and use out() instead of begin() (the latter is deprecated):
      template <typename FormatContext>
      auto format(const :Polygon &p,
        FormatContext &ctx)
      {
        bool is_first = true;
        auto out = ctx.out();
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            out = format_to(out, "{}", vertice);
            is_first = false;
          }
          else
          {
            out = format_to(out, ", {}", vertice);
          }
        }
        return out;
      }
    gerald5867
    @gerald5867

    hello i tested fmt::snprintf on vs 2017 like this:

    The first one does only remove %s and %d resulting in
    hallo mein name ist : und ich bin jahre altÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ

    The other two replace the format specifier correctly with their value.
    Also neither of them does a null termination?

    Am i missing something ? shouldnt fmt::snprintf work exactly like std::snprintf?

    image.png
    image.png
    Victor Zverovich
    @vitaut
    Please open an issue with a reproducible example. Code is not copyable from images.
    gerald5867
    @gerald5867
    i did made an issue and closed it again because i wanted to ask first if im doing something wrong.
    i did reopen it just now
    Giulio De Pasquale
    @peperunas
    @vitaut hello Victor, I am following your suggestion to create a custom formatter for LLVM's MachineInstr. I am receiving this error (since I'm using C++11): function uses ‘auto’ type specifier without trailing return type. Which return type should I put instead of auto?
    Here's my sample code:
    template <> struct fmt::formatter<llvm::MachineInstr> {
      template <typename ParseContext> constexpr auto parse(ParseContext &ctx) {
        return ctx.begin();
      }
    
      template <typename FormatContext>
      auto format(const llvm::MachineInstr &MI, FormatContext &ctx) {
        return fmt::format_to(ctx.begin(), "{}", to_string(MI));
      }
    };
    Matthias Moulin
    @matt77hias
    Hey, if one just wants to print a string (which can potentially contain formatting related character sequences), should one use fmt::print("{}", str); or fmt::print(str);. For example: Print("({:.1f}, {:.1f})"); works, but it could be a hack as this is not explicitly documented. Furthermore, if both are valid, which one is the most performant and thus recommended?
    Victor Zverovich
    @vitaut
    @matt77hias use fmt::print("{}", str) as with printf and Python's str.format.
    fmt::print("({:.1f}, {:.1f})") won't work: https://godbolt.org/z/YqpUyh
    @peperunas sorry missed your message. You probably figured it out already, but you should put typename FormatContext::iterator there.
    Matthias Moulin
    @matt77hias
    @vitaut Thanks! (Just realized that Print("({:.1f}, {:.1f})"); is my own wrapper handling the no arguments case in a separate overload, my bad.)
    Widualk
    @Widualk
    can someone help me to implement fmt library in visual studio 2019?