duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

format.cpp (1580B)


      1 #include "c4/format.hpp"
      2 
      3 #include <memory> // for std::align
      4 
      5 #ifdef __clang__
      6 #   pragma clang diagnostic push
      7 #   pragma clang diagnostic ignored "-Wformat-nonliteral"
      8 #   pragma clang diagnostic ignored "-Wold-style-cast"
      9 #elif defined(__GNUC__)
     10 #   pragma GCC diagnostic push
     11 #   pragma GCC diagnostic ignored "-Wformat-nonliteral"
     12 #   pragma GCC diagnostic ignored "-Wold-style-cast"
     13 #endif
     14 
     15 namespace c4 {
     16 
     17 
     18 size_t to_chars(substr buf, fmt::const_raw_wrapper r)
     19 {
     20     void * vptr = buf.str;
     21     size_t space = buf.len;
     22     auto ptr = (decltype(buf.str)) std::align(r.alignment, r.len, vptr, space);
     23     if(ptr == nullptr)
     24     {
     25         // if it was not possible to align, return a conservative estimate
     26         // of the required space
     27         return r.alignment + r.len;
     28     }
     29     C4_CHECK(ptr >= buf.begin() && ptr <= buf.end());
     30     size_t sz = static_cast<size_t>(ptr - buf.str) + r.len;
     31     if(sz <= buf.len)
     32     {
     33         memcpy(ptr, r.buf, r.len);
     34     }
     35     return sz;
     36 }
     37 
     38 
     39 bool from_chars(csubstr buf, fmt::raw_wrapper *r)
     40 {
     41     C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wcast-qual")
     42     void * vptr = (void*)buf.str;
     43     C4_SUPPRESS_WARNING_GCC_POP
     44     size_t space = buf.len;
     45     auto ptr = (decltype(buf.str)) std::align(r->alignment, r->len, vptr, space);
     46     C4_CHECK(ptr != nullptr);
     47     C4_CHECK(ptr >= buf.begin() && ptr <= buf.end());
     48     //size_t dim = (ptr - buf.str) + r->len;
     49     memcpy(r->buf, ptr, r->len);
     50     return true;
     51 }
     52 
     53 
     54 } // namespace c4
     55 
     56 #ifdef __clang__
     57 #   pragma clang diagnostic pop
     58 #elif defined(__GNUC__)
     59 #   pragma GCC diagnostic pop
     60 #endif