Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/spdlog/details/fmt_helper.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 #pragma once
0004 
0005 #include <chrono>
0006 #include <iterator>
0007 #include <spdlog/common.h>
0008 #include <spdlog/fmt/fmt.h>
0009 #include <type_traits>
0010 
0011 #ifdef SPDLOG_USE_STD_FORMAT
0012     #include <charconv>
0013     #include <limits>
0014 #endif
0015 
0016 // Some fmt helpers to efficiently format and pad ints and strings
0017 namespace spdlog {
0018 namespace details {
0019 namespace fmt_helper {
0020 
0021 inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) {
0022     auto *buf_ptr = view.data();
0023     dest.append(buf_ptr, buf_ptr + view.size());
0024 }
0025 
0026 #ifdef SPDLOG_USE_STD_FORMAT
0027 template <typename T>
0028 inline void append_int(T n, memory_buf_t &dest) {
0029     // Buffer should be large enough to hold all digits (digits10 + 1) and a sign
0030     SPDLOG_CONSTEXPR const auto BUF_SIZE = std::numeric_limits<T>::digits10 + 2;
0031     char buf[BUF_SIZE];
0032 
0033     auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10);
0034     if (ec == std::errc()) {
0035         dest.append(buf, ptr);
0036     } else {
0037         throw_spdlog_ex("Failed to format int", static_cast<int>(ec));
0038     }
0039 }
0040 #else
0041 template <typename T>
0042 inline void append_int(T n, memory_buf_t &dest) {
0043     fmt::format_int i(n);
0044     dest.append(i.data(), i.data() + i.size());
0045 }
0046 #endif
0047 
0048 template <typename T>
0049 SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n) {
0050     // taken from fmt: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/format.h#L899-L912
0051     unsigned int count = 1;
0052     for (;;) {
0053         // Integer division is slow so do it for a group of four digits instead
0054         // of for every digit. The idea comes from the talk by Alexandrescu
0055         // "Three Optimization Tips for C++". See speed-test for a comparison.
0056         if (n < 10) return count;
0057         if (n < 100) return count + 1;
0058         if (n < 1000) return count + 2;
0059         if (n < 10000) return count + 3;
0060         n /= 10000u;
0061         count += 4;
0062     }
0063 }
0064 
0065 template <typename T>
0066 inline unsigned int count_digits(T n) {
0067     using count_type =
0068         typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
0069 #ifdef SPDLOG_USE_STD_FORMAT
0070     return count_digits_fallback(static_cast<count_type>(n));
0071 #else
0072     return static_cast<unsigned int>(fmt::
0073     // fmt 7.0.0 renamed the internal namespace to detail.
0074     // See: https://github.com/fmtlib/fmt/issues/1538
0075     #if FMT_VERSION < 70000
0076                                          internal
0077     #else
0078                                          detail
0079     #endif
0080                                      ::count_digits(static_cast<count_type>(n)));
0081 #endif
0082 }
0083 
0084 inline void pad2(int n, memory_buf_t &dest) {
0085     if (n >= 0 && n < 100)  // 0-99
0086     {
0087         dest.push_back(static_cast<char>('0' + n / 10));
0088         dest.push_back(static_cast<char>('0' + n % 10));
0089     } else  // unlikely, but just in case, let fmt deal with it
0090     {
0091         fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n);
0092     }
0093 }
0094 
0095 template <typename T>
0096 inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) {
0097     static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
0098     for (auto digits = count_digits(n); digits < width; digits++) {
0099         dest.push_back('0');
0100     }
0101     append_int(n, dest);
0102 }
0103 
0104 template <typename T>
0105 inline void pad3(T n, memory_buf_t &dest) {
0106     static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
0107     if (n < 1000) {
0108         dest.push_back(static_cast<char>(n / 100 + '0'));
0109         n = n % 100;
0110         dest.push_back(static_cast<char>((n / 10) + '0'));
0111         dest.push_back(static_cast<char>((n % 10) + '0'));
0112     } else {
0113         append_int(n, dest);
0114     }
0115 }
0116 
0117 template <typename T>
0118 inline void pad6(T n, memory_buf_t &dest) {
0119     pad_uint(n, 6, dest);
0120 }
0121 
0122 template <typename T>
0123 inline void pad9(T n, memory_buf_t &dest) {
0124     pad_uint(n, 9, dest);
0125 }
0126 
0127 // return fraction of a second of the given time_point.
0128 // e.g.
0129 // fraction<std::milliseconds>(tp) -> will return the millis part of the second
0130 template <typename ToDuration>
0131 inline ToDuration time_fraction(log_clock::time_point tp) {
0132     using std::chrono::duration_cast;
0133     using std::chrono::seconds;
0134     auto duration = tp.time_since_epoch();
0135     auto secs = duration_cast<seconds>(duration);
0136     return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
0137 }
0138 
0139 }  // namespace fmt_helper
0140 }  // namespace details
0141 }  // namespace spdlog