Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 09:27:47

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