Merge branch 'dev'

This commit is contained in:
Jeremy 2024-06-11 22:30:27 -05:00
commit bb83fa922c
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
3 changed files with 47 additions and 82 deletions

View File

@ -93,9 +93,9 @@ if(NOT WIN32 OR MINGW)
endif() endif()
if(UNIX AND NOT APPLE) if(UNIX AND NOT APPLE)
check_support(HAS_DL_FIND_OBJECT has_dl_find_object.cpp "" "" "") check_support(HAS_DL_FIND_OBJECT has_dl_find_object.cpp "" "dl" "")
if(NOT HAS_DL_FIND_OBJECT) if(NOT HAS_DL_FIND_OBJECT)
check_support(HAS_DLADDR1 has_dladdr1.cpp "" "" "") check_support(HAS_DLADDR1 has_dladdr1.cpp "" "dl" "")
endif() endif()
endif() endif()

View File

@ -1,4 +1,5 @@
#include <dlfcn.h> #include <dlfcn.h>
#include <link.h>
int main() { int main() {
Dl_info info; Dl_info info;

View File

@ -1,33 +1,24 @@
#ifndef MICROFMT_HPP #ifndef MICROFMT_HPP
#define MICROFMT_HPP #define MICROFMT_HPP
// Copyright (c) 2024 Jeremy Rifkin; MIT License
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <cstdio>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <string> #include <string>
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L) #if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
#include <string_view> #include <string_view>
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#include <intrin.h> #include <intrin.h>
#endif #endif
// {[[align][width]:[fill][base]]} // https://github.com/jeremy-rifkin/microfmt
// width: number or {} // Format: {[align][width][:[fill][base]]} # width: number or {}
namespace microfmt { namespace microfmt {
namespace detail { namespace detail {
#define STR2(x) #x
#define STR(x) STR2(x)
#define MICROFMT_ASSERT(expr) if(!(expr)) { \
throw std::runtime_error("Microfmt check failed" __FILE__ ":" STR(__LINE__) ": " #expr); \
}
inline std::uint64_t clz(std::uint64_t value) { inline std::uint64_t clz(std::uint64_t value) {
#ifdef _MSC_VER #ifdef _MSC_VER
unsigned long out = 0; unsigned long out = 0;
@ -60,7 +51,6 @@ namespace microfmt {
template<typename It> void do_write(std::string& out, It begin, It end, const format_options& options) { template<typename It> void do_write(std::string& out, It begin, It end, const format_options& options) {
auto size = end - begin; auto size = end - begin;
MICROFMT_ASSERT(size >= 0);
if(static_cast<std::size_t>(size) >= options.width) { if(static_cast<std::size_t>(size) >= options.width) {
out.append(begin, end); out.append(begin, end);
} else { } else {
@ -100,13 +90,11 @@ namespace microfmt {
inline std::string to_string(std::uint64_t value, const format_options& options) { inline std::string to_string(std::uint64_t value, const format_options& options) {
switch(options.base) { switch(options.base) {
case 'd': return std::to_string(value);
case 'H': return to_string<4, 0xf>(value, "0123456789ABCDEF"); case 'H': return to_string<4, 0xf>(value, "0123456789ABCDEF");
case 'h': return to_string<4, 0xf>(value); case 'h': return to_string<4, 0xf>(value);
case 'o': return to_string<3, 0x7>(value); case 'o': return to_string<3, 0x7>(value);
case 'b': return to_string<1, 0x1>(value); case 'b': return to_string<1, 0x1>(value);
default: default: return std::to_string(value); // failure: decimal
MICROFMT_ASSERT(false);
} }
} }
@ -154,7 +142,7 @@ namespace microfmt {
switch(value) { switch(value) {
case value_type::int64_value: return static_cast<int>(int64_value); case value_type::int64_value: return static_cast<int>(int64_value);
case value_type::uint64_value: return static_cast<int>(uint64_value); case value_type::uint64_value: return static_cast<int>(uint64_value);
default: MICROFMT_ASSERT(false); default: return 0; // failure: just 0
} }
} }
@ -193,108 +181,84 @@ namespace microfmt {
case value_type::c_string_value: case value_type::c_string_value:
do_write(out, c_string_value, c_string_value + std::strlen(c_string_value), options); do_write(out, c_string_value, c_string_value + std::strlen(c_string_value), options);
break; break;
default: } // failure: nop
MICROFMT_ASSERT(false);
}
} }
}; };
inline int parse_int(const char* begin, const char* end) { template<std::size_t N, typename It>
int x = 0; std::string format(It fmt_begin, It fmt_end, std::array<format_value, N> args) {
for(auto it = begin; it != end; it++) {
MICROFMT_ASSERT(isdigit(*it));
x *= 10;
x += *it - '0';
}
return x;
}
template<std::size_t N>
std::string format(const char* fmt_begin, const char* fmt_end, std::array<format_value, N> args) {
std::string str; std::string str;
std::size_t arg_i = 0; std::size_t arg_i = 0;
auto it = fmt_begin; auto it = fmt_begin;
auto peek = [&] (std::size_t dist) -> char { // 0 on failure auto peek = [&] (std::size_t dist) -> char { // 0 on failure
if(it != fmt_end) { return fmt_end - it > signed(dist) ? *(it + dist) : 0;
return *(it + dist);
} else {
return 0;
}
}; };
auto read_number = [&] () -> int { // -1 on failure auto read_number = [&] () -> int { // -1 on failure
auto scan = it; auto scan = it;
int num = 0;
while(scan != fmt_end && isdigit(*scan)) { while(scan != fmt_end && isdigit(*scan)) {
num *= 10;
num += *scan - '0';
scan++; scan++;
} }
if(scan != it) { if(scan != it) {
int val = parse_int(it, scan);
it = scan; it = scan;
return val; return num;
} else { } else {
return -1; return -1;
} }
}; };
while(it != fmt_end) { for(; it != fmt_end; it++) {
if(*it == '{') { if((*it == '{' || *it == '}') && peek(1) == *it) { // parse {{ and }} escapes
if(peek(1) == '{') { it++;
// try to handle escape } else if(*it == '{' && it + 1 != fmt_end) {
str += '{'; auto saved_it = it;
auto handle_formatter = [&] () {
it++; it++;
} else {
// parse format string
it++;
MICROFMT_ASSERT(it != fmt_end);
format_options options; format_options options;
// try to parse alignment // try to parse alignment
if(*it == '<' || *it == '>') { if(*it == '<' || *it == '>') {
options.align = *it == '<' ? alignment::left : alignment::right; options.align = *it++ == '<' ? alignment::left : alignment::right;
it++;
} }
// try to parse width // try to parse width
auto width = read_number(); auto width = read_number(); // handles fmt_end check
if(width != -1) { if(width != -1) {
options.width = width; options.width = width;
} else if(*it == '{') { // try to parse variable width } else if(it != fmt_end && *it == '{') { // try to parse variable width
MICROFMT_ASSERT(peek(1) == '}'); if(peek(1) != '}') {
return false;
}
it += 2; it += 2;
MICROFMT_ASSERT(arg_i < args.size()); options.width = arg_i < args.size() ? args[arg_i++].unwrap_int() : 0;
options.width = args[arg_i++].unwrap_int();
} }
// try to parse fill/base // try to parse fill/base
if(*it == ':') { if(it != fmt_end && *it == ':') {
it++; it++;
// try to parse fill if(fmt_end - it > 1 && *it != '}' && peek(1) != '}') { // two chars before the }, fill+base
if(*it != '}' && peek(1) != '}') {
// two chars before the }, treat as fill+base
options.fill = *it++; options.fill = *it++;
options.base = *it++; options.base = *it++;
} else if(*it != '}') { } else if(it != fmt_end && *it != '}') { // one char before the }, just base
// one char before the }, treat as base if possible
if(*it == 'd' || *it == 'h' || *it == 'H' || *it == 'o' || *it == 'b') { if(*it == 'd' || *it == 'h' || *it == 'H' || *it == 'o' || *it == 'b') {
options.base = *it++; options.base = *it++;
} else { } else {
options.fill = *it++; options.fill = *it++;
} }
} else {
MICROFMT_ASSERT(false);
} }
} }
MICROFMT_ASSERT(*it == '}'); if(it == fmt_end || *it != '}') {
MICROFMT_ASSERT(arg_i < args.size()); return false;
args[arg_i++].write(str, options); }
if(arg_i < args.size()) {
args[arg_i++].write(str, options);
}
return true;
};
if(handle_formatter()) {
continue; // If reached here, successfully parsed and wrote a formatter. Don't write *it.
} }
} else if(*it == '}') { it = saved_it; // go back
// parse }} escape
if(peek(1) == '}') {
str += '}';
it++;
} else {
MICROFMT_ASSERT(false);
}
} else {
str += *it;
} }
it++; str += *it;
} }
return str; return str;
} }
@ -306,8 +270,9 @@ namespace microfmt {
return detail::format<sizeof...(args)>(fmt.begin(), fmt.end(), {detail::format_value(args)...}); return detail::format<sizeof...(args)>(fmt.begin(), fmt.end(), {detail::format_value(args)...});
} }
// working around an old msvc bug https://godbolt.org/z/88T8hrzzq mre: https://godbolt.org/z/drd8echbP
inline std::string format(std::string_view fmt) { inline std::string format(std::string_view fmt) {
return std::string(fmt); return detail::format<1>(fmt.begin(), fmt.end(), {detail::format_value(1)});
} }
#endif #endif
@ -316,7 +281,6 @@ namespace microfmt {
return detail::format<sizeof...(args)>(fmt, fmt + std::strlen(fmt), {detail::format_value(args)...}); return detail::format<sizeof...(args)>(fmt, fmt + std::strlen(fmt), {detail::format_value(args)...});
} }
// working around an old msvc bug https://godbolt.org/z/88T8hrzzq mre: https://godbolt.org/z/drd8echbP
inline std::string format(const char* fmt) { inline std::string format(const char* fmt) {
return detail::format<1>(fmt, fmt + std::strlen(fmt), {detail::format_value(1)}); return detail::format<1>(fmt, fmt + std::strlen(fmt), {detail::format_value(1)});
} }