Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ function(add_example name)
endfunction()

add_example(vfthook)
add_example(libfmt)

CPMAddPackage("gh:fmtlib/fmt#12.2.0")
target_link_libraries(libhat_example_libfmt PRIVATE fmt::fmt)
41 changes: 41 additions & 0 deletions examples/libfmt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <libhat.hpp>

template<typename T, typename CharT>
requires(hat::formattable<T, CharT, fmt::formatter>)
struct fmt::formatter<T, CharT> : hat::formatter<T, CharT, fmt::formatter> {};

template<typename T, typename CharT>
requires(hat::disable_range_formatter<T>)
struct fmt::is_range<T, CharT> : std::false_type {};

int main() {
using namespace std::literals;
using namespace hat::literals;

fmt::println("{}", hat::fixed_string{"abc"});

fmt::println("{}", "abc"_s);

fmt::println("{}", "abc"_csv);

fmt::println("{}", hat::cow_string{"abc"s});
fmt::println("{}", hat::cow_string{"abc"sv});

fmt::println("{}", hat::cow_cstring{"abc"s});
fmt::println("{}", hat::cow_cstring{"abc"_csv});

std::vector data{1, 2, 3};
fmt::println("{}", hat::cow_span<int>{data});
fmt::println("{}", hat::cow_span<int>{std::span{data}});
// fmt::println("{}", hat::cow_writable_span<int>{data});
// fmt::println("{}", hat::cow_writable_span<int>{std::span{data}});

fmt::println("{}", hat::signature_element{std::byte{0x10}, std::byte{0xF0}});

constexpr auto sig = "11 22 33"_sig;
fmt::println("{}", hat::signature(sig.begin(), sig.end()));
fmt::println("{}", hat::signature_view{sig});
fmt::println("{}", sig);
}
1 change: 1 addition & 0 deletions include/libhat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "libhat/cstring_view.hpp"
#include "libhat/defines.hpp"
#include "libhat/fixed_string.hpp"
#include "libhat/formatter.hpp"
#include "libhat/memory.hpp"
#include "libhat/memory_protector.hpp"
#include "libhat/process.hpp"
Expand Down
9 changes: 9 additions & 0 deletions include/libhat/cow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "detail/compressed_pair.hpp"
#include "cstring_view.hpp"
#include "export.hpp"
#include "formatter.hpp"

LIBHAT_EXPORT namespace hat {

Expand Down Expand Up @@ -450,4 +451,12 @@ LIBHAT_EXPORT namespace hat {
template<detail::non_const T>
using cow_writable_span = cow_writable_span<T, std::pmr::polymorphic_allocator<T>>;
}

template<typename T, typename Traits, typename Allocator, typename CharT, template<typename...> class Formatter>
struct formatter<cow<T, Traits, Allocator>, CharT, Formatter> : Formatter<T, CharT> {
template<typename FormatContext>
constexpr auto format(const cow<T, Traits, Allocator>& value, FormatContext& ctx) const {
return Formatter<T, CharT>::format(value.viewed(), ctx);
}
};
}
12 changes: 12 additions & 0 deletions include/libhat/cstring_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#endif

#include "export.hpp"
#include "formatter.hpp"

LIBHAT_EXPORT namespace hat {

Expand Down Expand Up @@ -308,6 +309,17 @@ LIBHAT_EXPORT namespace hat {
#endif
using u16cstring_view = basic_cstring_view<char16_t>;
using u32cstring_view = basic_cstring_view<char32_t>;

template<typename CharT, typename Traits, template<typename...> class Formatter>
struct formatter<basic_cstring_view<CharT, Traits>, CharT, Formatter> : Formatter<std::basic_string_view<CharT, Traits>, CharT> {
template<typename FormatContext>
constexpr auto format(const basic_cstring_view<CharT, Traits>& value, FormatContext& ctx) const {
return Formatter<std::basic_string_view<CharT, Traits>, CharT>::format(value, ctx);
}
};

template<typename CharT, typename Traits>
constexpr bool disable_range_formatter<basic_cstring_view<CharT, Traits>> = true;
}

namespace hat::detail {
Expand Down
18 changes: 14 additions & 4 deletions include/libhat/fixed_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "cstring_view.hpp"
#include "export.hpp"
#include "formatter.hpp"

LIBHAT_EXPORT namespace hat {

Expand Down Expand Up @@ -174,16 +175,25 @@ LIBHAT_EXPORT namespace hat {
};

#define LIBHAT_DEFINE_FIXED_STRING(name, type) \
template<std::size_t N> \
template<std::size_t N> \
struct name : public basic_fixed_string<type, N, name> { \
using basic_fixed_string<type, N, name>::basic_fixed_string; \
}; \
template<std::size_t N> \
template<std::size_t N> \
name(const type(&str)[N]) -> name<N - 1>; \
template<std::size_t N, std::size_t M> \
template<std::size_t N, std::size_t M> \
constexpr inline auto operator+(const type (&cstr)[N], const basic_fixed_string<type, M, name>& lstr) { \
return name{cstr} + lstr; \
}
} \
template<std::size_t N, template<typename...> class Formatter> \
struct formatter<name<N>, type, Formatter> : Formatter<std::basic_string_view<type>, type> { \
template<typename FormatContext> \
constexpr auto format(const name<N>& value, FormatContext& ctx) const { \
return Formatter<std::basic_string_view<type>, type>::format(value.to_view(), ctx); \
} \
}; \
template<std::size_t N> \
constexpr bool disable_range_formatter<name<N>> = true;

LIBHAT_DEFINE_FIXED_STRING(fixed_string, char)
LIBHAT_DEFINE_FIXED_STRING(wfixed_string, wchar_t)
Expand Down
35 changes: 35 additions & 0 deletions include/libhat/formatter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#ifndef LIBHAT_MODULE
#include <concepts>
#include <format>
#include <type_traits>
#endif

#include "export.hpp"

LIBHAT_EXPORT namespace hat {

template<typename T, typename CharT, template<typename...> class Formatter>
struct formatter {
formatter() = delete;
formatter(const formatter&) = delete;
formatter& operator=(const formatter&) = delete;
};

template<typename T, typename CharT, template<typename...> class Formatter>
concept formattable = std::semiregular<formatter<std::remove_reference_t<T>, CharT, Formatter>>;

template<typename T>
constexpr bool disable_range_formatter = false;
}

template<typename T, typename CharT>
requires(hat::formattable<T, CharT, std::formatter>)
struct std::formatter<T, CharT> : hat::formatter<T, CharT, std::formatter> {};

#if __cpp_lib_format_ranges >= 202207L
template<typename T>
requires(hat::disable_range_formatter<T>)
constexpr auto std::format_kind<T> = std::range_format::disabled;
#endif
107 changes: 80 additions & 27 deletions include/libhat/signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

#include "defines.hpp"
#include "export.hpp"
#include "strconv.hpp"
#include "fixed_string.hpp"
#include "formatter.hpp"
#include "strconv.hpp"

LIBHAT_EXPORT namespace hat {

Expand Down Expand Up @@ -254,38 +255,90 @@ LIBHAT_EXPORT namespace hat {
}
#endif

[[nodiscard]] constexpr std::string to_string(const signature_view signature) {
constexpr auto format_signature_to(std::output_iterator<char> auto out, const signature_element element) {
constexpr std::string_view hex{"0123456789ABCDEF"};
std::string ret;
ret.reserve(signature.size() * 3);
const bool a = (element.mask() & std::byte{0xF0}) == std::byte{0xF0};
const bool b = (element.mask() & std::byte{0x0F}) == std::byte{0x0F};
if (a || b) {
*out++ = a ? hex[static_cast<std::size_t>(element.value() >> 4) & 0xFu] : '?';
*out++ = b ? hex[static_cast<std::size_t>(element.value() >> 0) & 0xFu] : '?';
} else if (element.none()) {
*out++ = '?';
} else {
*out++ = element.has(7) ? (element.at(7) ? '1' : '0') : '?';
*out++ = element.has(6) ? (element.at(6) ? '1' : '0') : '?';
*out++ = element.has(5) ? (element.at(5) ? '1' : '0') : '?';
*out++ = element.has(4) ? (element.at(4) ? '1' : '0') : '?';
*out++ = element.has(3) ? (element.at(3) ? '1' : '0') : '?';
*out++ = element.has(2) ? (element.at(2) ? '1' : '0') : '?';
*out++ = element.has(1) ? (element.at(1) ? '1' : '0') : '?';
*out++ = element.has(0) ? (element.at(0) ? '1' : '0') : '?';
}
return out;
}

constexpr auto format_signature_to(std::output_iterator<char> auto out, const signature_view signature) {
for (auto& element : signature) {
const bool a = (element.mask() & std::byte{0xF0}) == std::byte{0xF0};
const bool b = (element.mask() & std::byte{0x0F}) == std::byte{0x0F};
if (a || b) {
ret += {
a ? hex[static_cast<std::size_t>(element.value() >> 4) & 0xFu] : '?',
b ? hex[static_cast<std::size_t>(element.value() >> 0) & 0xFu] : '?',
' '
};
} else if (element.none()) {
ret += "? ";
} else {
ret += {
element.has(7) ? (element.at(7) ? '1' : '0') : '?',
element.has(6) ? (element.at(6) ? '1' : '0') : '?',
element.has(5) ? (element.at(5) ? '1' : '0') : '?',
element.has(4) ? (element.at(4) ? '1' : '0') : '?',
element.has(3) ? (element.at(3) ? '1' : '0') : '?',
element.has(2) ? (element.at(2) ? '1' : '0') : '?',
element.has(1) ? (element.at(1) ? '1' : '0') : '?',
element.has(0) ? (element.at(0) ? '1' : '0') : '?',
' '
};
out = format_signature_to(out, element);
if (&element != &signature.back()) {
*out++ = ' ';
}
}
ret.pop_back();
return out;
}

[[nodiscard]] constexpr std::string to_string(const signature_element element) {
std::string ret;
format_signature_to(std::back_inserter(ret), element);
return ret;
}

[[nodiscard]] constexpr std::string to_string(const signature_view signature) {
std::string ret;
ret.reserve(signature.size() * 3);
format_signature_to(std::back_inserter(ret), signature);
return ret;
}
}

namespace hat::detail {

template<typename T>
struct signature_formatter {
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
return ctx.begin();
}

template<typename FormatContext>
constexpr auto format(const T& value, FormatContext& ctx) const {
return format_signature_to(ctx.out(), value);
}
};
}

LIBHAT_EXPORT namespace hat {

template<template<typename...> class Formatter>
struct formatter<signature_element, char, Formatter> : detail::signature_formatter<signature_element> {};

template<template<typename...> class Formatter>
struct formatter<signature, char, Formatter> : detail::signature_formatter<signature> {};

template<>
constexpr bool disable_range_formatter<signature> = true;

template<template<typename...> class Formatter>
struct formatter<signature_view, char, Formatter> : detail::signature_formatter<signature_view> {};

template<>
constexpr bool disable_range_formatter<signature_view> = true;

template<std::size_t N, template<typename...> class Formatter>
struct formatter<fixed_signature<N>, char, Formatter> : detail::signature_formatter<fixed_signature<N>> {};

template<std::size_t N>
constexpr bool disable_range_formatter<fixed_signature<N>> = true;
}

LIBHAT_EXPORT namespace hat::inline literals::inline signature_literals {
Expand Down
9 changes: 9 additions & 0 deletions include/libhat/string_literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "export.hpp"
#include "fixed_string.hpp"
#include "formatter.hpp"

LIBHAT_EXPORT namespace hat {

Expand All @@ -26,6 +27,14 @@ LIBHAT_EXPORT namespace hat {

template<hat::u32fixed_string str>
using u32string_literal = basic_string_literal<str>;

template<auto str, template<typename...> class Formatter>
struct formatter<basic_string_literal<str>, typename decltype(str)::value_type, Formatter> : Formatter<decltype(str), typename decltype(str)::value_type> {
template<typename FormatContext>
constexpr auto format(const string_literal<str>&, FormatContext& ctx) const {
return Formatter<decltype(str), typename decltype(str)::value_type>::format(str, ctx);
}
};
}

LIBHAT_EXPORT namespace hat::inline literals::inline string_literals {
Expand Down
1 change: 1 addition & 0 deletions module/libhat.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module;
#include <cstdlib>
#include <cstring>
#include <execution>
#include <format>
#include <functional>
#include <iterator>
#include <memory>
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ register_test(libhat_benchmark_compare benchmark/Compare.cpp)
register_test(libhat_benchmark_compare_impl benchmark/CompareImpl.cpp)
register_test(libhat_test_scanner tests/Scanner.cpp)
register_test(libhat_test_process tests/Process.cpp)
register_test(libhat_test_formatter tests/Formatter.cpp)

if(LIBHAT_TESTING_SAMPLE_BIN)
CPMAddPackage(
Expand Down
Loading