| 1 |
#ifndef MICROFMT_HPP |
| 2 |
#define MICROFMT_HPP |
| 3 |
|
| 4 |
#include <algorithm> |
| 5 |
#include <cstdint> |
| 6 |
#include <cstring> |
| 7 |
#include <initializer_list> |
| 8 |
#include <iostream> |
| 9 |
#include <iterator> |
| 10 |
#include <string> |
| 11 |
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L) |
| 12 |
#include <string_view> |
| 13 |
#endif |
| 14 |
#ifdef _MSC_VER |
| 15 |
#include <intrin.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include "utils/string_view.hpp" |
| 19 |
|
| 20 |
// https://github.com/jeremy-rifkin/microfmt |
| 21 |
// Format: {[align][width][:[fill][base]]} # width: number or {} |
| 22 |
|
| 23 |
CPPTRACE_BEGIN_NAMESPACE |
| 24 |
namespace microfmt { |
| 25 |
namespace detail { |
| 26 |
inline std::uint64_t clz(std::uint64_t value) { |
| 27 |
#ifdef _MSC_VER |
| 28 |
unsigned long out = 0; |
| 29 |
#ifdef _WIN64 |
| 30 |
_BitScanReverse64(&out, value); |
| 31 |
#else |
| 32 |
if(_BitScanReverse(&out, std::uint32_t(value >> 32))) { |
| 33 |
return 63 - int(out + 32); |
| 34 |
} |
| 35 |
_BitScanReverse(&out, std::uint32_t(value)); |
| 36 |
#endif |
| 37 |
return 63 - out; |
| 38 |
#else |
| 39 |
return __builtin_clzll(value); |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
template<typename U, typename V> U to(V v) { |
| 44 |
return static_cast<U>(v); // A way to cast to U without "warning: useless cast to type" |
| 45 |
} |
| 46 |
|
| 47 |
enum class alignment { left, right }; |
| 48 |
|
| 49 |
struct format_options { |
| 50 |
alignment align = alignment::left; |
| 51 |
char fill = ' '; |
| 52 |
size_t width = 0; |
| 53 |
char base = 'd'; |
| 54 |
}; |
| 55 |
|
| 56 |
template<typename OutputIt, typename InputIt> |
| 57 |
void do_write(OutputIt out, InputIt begin, InputIt end, const format_options& options) { |
| 58 |
auto size = end - begin; |
| 59 |
if(static_cast<std::size_t>(size) >= options.width) { |
| 60 |
std::copy(begin, end, out); |
| 61 |
} else { |
| 62 |
if(options.align == alignment::left) { |
| 63 |
std::copy(begin, end, out); |
| 64 |
std::fill_n(out, options.width - size, options.fill); |
| 65 |
} else { |
| 66 |
std::fill_n(out, options.width - size, options.fill); |
| 67 |
std::copy(begin, end, out); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
template<int shift, int mask> |
| 73 |
std::string to_string(std::uint64_t value, const char* digits = "0123456789abcdef") { |
| 74 |
if(value == 0) { |
| 75 |
return "0"; |
| 76 |
} else { |
| 77 |
// digits = floor(1 + log_base(x)) |
| 78 |
// log_base(x) = log_2(x) / log_2(base) |
| 79 |
// log_2(x) == 63 - clz(x) |
| 80 |
// 1 + (63 - clz(value)) / (63 - clz(1 << shift)) |
| 81 |
// 63 - clz(1 << shift) is the same as shift |
| 82 |
auto n_digits = to<std::size_t>(1 + (63 - clz(value)) / shift); |
| 83 |
std::string number; |
| 84 |
number.resize(n_digits); |
| 85 |
std::size_t i = n_digits - 1; |
| 86 |
while(value > 0) { |
| 87 |
number[i--] = digits[value & mask]; |
| 88 |
value >>= shift; |
| 89 |
} |
| 90 |
return number; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
inline std::string to_string(std::uint64_t value, const format_options& options) { |
| 95 |
switch(options.base) { |
| 96 |
case 'H': return to_string<4, 0xf>(value, "0123456789ABCDEF"); |
| 97 |
case 'h': return to_string<4, 0xf>(value); |
| 98 |
case 'o': return to_string<3, 0x7>(value); |
| 99 |
case 'b': return to_string<1, 0x1>(value); |
| 100 |
default: return std::to_string(value); // failure: decimal |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
struct string_view { |
| 105 |
const char* data; |
| 106 |
std::size_t size; |
| 107 |
}; |
| 108 |
|
| 109 |
class format_value { |
| 110 |
enum class value_type { |
| 111 |
char_value, |
| 112 |
int64_value, |
| 113 |
uint64_value, |
| 114 |
string_value, |
| 115 |
string_view_value, |
| 116 |
c_string_value, |
| 117 |
}; |
| 118 |
union { |
| 119 |
char char_value; |
| 120 |
std::int64_t int64_value; |
| 121 |
std::uint64_t uint64_value; |
| 122 |
const std::string* string_value; |
| 123 |
string_view string_view_value; |
| 124 |
const char* c_string_value; |
| 125 |
}; |
| 126 |
value_type value; |
| 127 |
|
| 128 |
public: |
| 129 |
format_value(char c) : char_value(c), value(value_type::char_value) {} |
| 130 |
format_value(short int_val) : int64_value(int_val), value(value_type::int64_value) {} |
| 131 |
format_value(int int_val) : int64_value(int_val), value(value_type::int64_value) {} |
| 132 |
format_value(long int_val) : int64_value(int_val), value(value_type::int64_value) {} |
| 133 |
format_value(long long int_val) : int64_value(int_val), value(value_type::int64_value) {} |
| 134 |
format_value(unsigned char int_val) : uint64_value(int_val), value(value_type::uint64_value) {} |
| 135 |
format_value(unsigned short int_val) : uint64_value(int_val), value(value_type::uint64_value) {} |
| 136 |
format_value(unsigned int int_val) : uint64_value(int_val), value(value_type::uint64_value) {} |
| 137 |
format_value(unsigned long int_val) : uint64_value(int_val), value(value_type::uint64_value) {} |
| 138 |
format_value(unsigned long long int_val) : uint64_value(int_val), value(value_type::uint64_value) {} |
| 139 |
format_value(const std::string& string) : string_value(&string), value(value_type::string_value) {} |
| 140 |
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L) |
| 141 |
format_value(std::string_view sv) |
| 142 |
: string_view_value{sv.data(), sv.size()}, value(value_type::string_view_value) {} |
| 143 |
#endif |
| 144 |
format_value(cpptrace::detail::string_view sv) |
| 145 |
: string_view_value{sv.data(), sv.size()}, value(value_type::string_view_value) {} |
| 146 |
format_value(const char* c_string) : c_string_value(c_string), value(value_type::c_string_value) {} |
| 147 |
|
| 148 |
int unwrap_int() const { |
| 149 |
switch(value) { |
| 150 |
case value_type::int64_value: return static_cast<int>(int64_value); |
| 151 |
case value_type::uint64_value: return static_cast<int>(uint64_value); |
| 152 |
default: return 0; // failure: just 0 |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public: |
| 157 |
template<typename OutputIt> |
| 158 |
void write(OutputIt out, const format_options& options) const { |
| 159 |
switch(value) { |
| 160 |
case value_type::char_value: |
| 161 |
do_write(out, &char_value, &char_value + 1, options); |
| 162 |
break; |
| 163 |
case value_type::int64_value: |
| 164 |
{ |
| 165 |
std::string str; |
| 166 |
std::int64_t val = int64_value; |
| 167 |
if(val < 0) { |
| 168 |
str += '-'; |
| 169 |
val *= -1; |
| 170 |
} |
| 171 |
str += to_string(static_cast<std::uint64_t>(val), options); |
| 172 |
do_write(out, str.begin(), str.end(), options); |
| 173 |
} |
| 174 |
break; |
| 175 |
case value_type::uint64_value: |
| 176 |
{ |
| 177 |
std::string str = to_string(uint64_value, options); |
| 178 |
do_write(out, str.begin(), str.end(), options); |
| 179 |
} |
| 180 |
break; |
| 181 |
case value_type::string_value: |
| 182 |
do_write(out, string_value->begin(), string_value->end(), options); |
| 183 |
break; |
| 184 |
case value_type::string_view_value: |
| 185 |
do_write(out, string_view_value.data, string_view_value.data + string_view_value.size, options); |
| 186 |
break; |
| 187 |
case value_type::c_string_value: |
| 188 |
do_write(out, c_string_value, c_string_value + std::strlen(c_string_value), options); |
| 189 |
break; |
| 190 |
} // failure: nop |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
// note: previously used std::array and there was a bug with std::array<T, 0> affecting old msvc |
| 195 |
// https://godbolt.org/z/88T8hrzzq mre: https://godbolt.org/z/drd8echbP |
| 196 |
template<typename OutputIt, typename InputIt> |
| 197 |
void format(OutputIt out, InputIt fmt_begin, InputIt fmt_end, const std::initializer_list<format_value>& args) { |
| 198 |
std::size_t arg_i = 0; |
| 199 |
auto it = fmt_begin; |
| 200 |
auto peek = [&] (std::size_t dist) -> char { // 0 on failure |
| 201 |
return fmt_end - it > signed(dist) ? *(it + dist) : 0; |
| 202 |
}; |
| 203 |
auto read_number = [&] () -> int { // -1 on failure |
| 204 |
auto scan = it; |
| 205 |
int num = 0; |
| 206 |
while(scan != fmt_end && isdigit(*scan)) { |
| 207 |
num *= 10; |
| 208 |
num += *scan - '0'; |
| 209 |
scan++; |
| 210 |
} |
| 211 |
if(scan != it) { |
| 212 |
it = scan; |
| 213 |
return num; |
| 214 |
} else { |
| 215 |
return -1; |
| 216 |
} |
| 217 |
}; |
| 218 |
for(; it != fmt_end; it++) { |
| 219 |
if((*it == '{' || *it == '}') && peek(1) == *it) { // parse {{ and }} escapes |
| 220 |
it++; |
| 221 |
} else if(*it == '{' && it + 1 != fmt_end) { |
| 222 |
auto saved_it = it; |
| 223 |
auto handle_formatter = [&] () { |
| 224 |
it++; |
| 225 |
format_options options; |
| 226 |
// try to parse alignment |
| 227 |
if(*it == '<' || *it == '>') { |
| 228 |
options.align = *it++ == '<' ? alignment::left : alignment::right; |
| 229 |
} |
| 230 |
// try to parse width |
| 231 |
auto width = read_number(); // handles fmt_end check |
| 232 |
if(width != -1) { |
| 233 |
options.width = width; |
| 234 |
} else if(it != fmt_end && *it == '{') { // try to parse variable width |
| 235 |
if(peek(1) != '}') { |
| 236 |
return false; |
| 237 |
} |
| 238 |
it += 2; |
| 239 |
options.width = arg_i < args.size() ? args.begin()[arg_i++].unwrap_int() : 0; |
| 240 |
} |
| 241 |
// try to parse fill/base |
| 242 |
if(it != fmt_end && *it == ':') { |
| 243 |
it++; |
| 244 |
if(fmt_end - it > 1 && *it != '}' && peek(1) != '}') { // two chars before the }, fill+base |
| 245 |
options.fill = *it++; |
| 246 |
options.base = *it++; |
| 247 |
} else if(it != fmt_end && *it != '}') { // one char before the }, just base |
| 248 |
if(*it == 'd' || *it == 'h' || *it == 'H' || *it == 'o' || *it == 'b') { |
| 249 |
options.base = *it++; |
| 250 |
} else { |
| 251 |
options.fill = *it++; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
if(it == fmt_end || *it != '}') { |
| 256 |
return false; |
| 257 |
} |
| 258 |
if(arg_i < args.size()) { |
| 259 |
args.begin()[arg_i++].write(out, options); |
| 260 |
} |
| 261 |
return true; |
| 262 |
}; |
| 263 |
if(handle_formatter()) { |
| 264 |
continue; // If reached here, successfully parsed and wrote a formatter. Don't write *it. |
| 265 |
} |
| 266 |
it = saved_it; // go back |
| 267 |
} |
| 268 |
*out++ = *it; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L) |
| 273 |
template<typename OutputIt> |
| 274 |
void format(OutputIt out, std::string_view fmt, const std::initializer_list<format_value>& args) { |
| 275 |
return format(out, fmt.begin(), fmt.end(), args); |
| 276 |
} |
| 277 |
#endif |
| 278 |
|
| 279 |
template<typename OutputIt> |
| 280 |
void format(OutputIt out, const char* fmt, const std::initializer_list<format_value>& args) { |
| 281 |
return format(out, fmt, fmt + std::strlen(fmt), args); |
| 282 |
} |
| 283 |
|
| 284 |
std::ostream& get_cout(); |
| 285 |
} |
| 286 |
|
| 287 |
template<typename S, typename... Args> |
| 288 |
std::string format(const S& fmt, Args&&... args) { |
| 289 |
std::string str; |
| 290 |
detail::format(std::back_inserter(str), fmt, {detail::format_value(args)...}); |
| 291 |
return str; |
| 292 |
} |
| 293 |
|
| 294 |
template<typename S, typename... Args> |
| 295 |
void print(const S& fmt, Args&&... args) { |
| 296 |
detail::format(std::ostream_iterator<char>(detail::get_cout()), fmt, {args...}); |
| 297 |
} |
| 298 |
|
| 299 |
template<typename S, typename... Args> |
| 300 |
void print(std::ostream& ostream, const S& fmt, Args&&... args) { |
| 301 |
detail::format(std::ostream_iterator<char>(ostream), fmt, {args...}); |
| 302 |
} |
| 303 |
|
| 304 |
template<typename S, typename... Args> |
| 305 |
void print(std::FILE* stream, const S& fmt, Args&&... args) { |
| 306 |
auto str = format(fmt, args...); |
| 307 |
fwrite(str.data(), 1, str.size(), stream); |
| 308 |
} |
| 309 |
} |
| 310 |
CPPTRACE_END_NAMESPACE |
| 311 |
|
| 312 |
#endif |