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
44 changes: 43 additions & 1 deletion 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 @@ -286,6 +287,47 @@ LIBHAT_EXPORT namespace hat {
ret.pop_back();
return ret;
}

template<template<typename...> class Formatter>
struct formatter<signature_element, char, Formatter> : Formatter<std::string, char> {
template<typename FormatContext>
constexpr auto format(const signature_element& value, FormatContext& ctx) const {
return Formatter<std::string, char>::format(to_string(signature_view{&value, 1}), ctx);
}
};

template<template<typename...> class Formatter>
struct formatter<signature, char, Formatter> : Formatter<std::string, char> {
template<typename FormatContext>
constexpr auto format(const signature& value, FormatContext& ctx) const {
return Formatter<std::string, char>::format(to_string(value), ctx);
}
};

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

template<template<typename...> class Formatter>
struct formatter<signature_view, char, Formatter> : Formatter<std::string, char> {
template<typename FormatContext>
constexpr auto format(const signature_view& value, FormatContext& ctx) const {
return Formatter<std::string, char>::format(to_string(value), ctx);
}
};

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> : Formatter<std::string, char> {
template<typename FormatContext>
constexpr auto format(const fixed_signature<N>& value, FormatContext& ctx) const {
return Formatter<std::string, char>::format(to_string(value), ctx);
}
};

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
55 changes: 55 additions & 0 deletions test/tests/Formatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <gtest/gtest.h>
#include <libhat/cow.hpp>
#include <libhat/cstring_view.hpp>
#include <libhat/fixed_string.hpp>
#include <libhat/signature.hpp>
#include <libhat/string_literal.hpp>
#include <format>

using namespace std::literals;
using namespace hat::literals;

TEST(FormatterTest, FixedString) {
ASSERT_EQ(std::format("{}", hat::fixed_string{"abc"}), "abc"sv);
}

TEST(FormatterTest, StringLiteral) {
ASSERT_EQ(std::format("{}", "abc"_s), "abc"sv);
}

TEST(FormatterTest, CStringView) {
ASSERT_EQ(std::format("{}", "abc"_csv), "abc"sv);
}

TEST(FormatterTest, CowString) {
ASSERT_EQ(std::format("{}", hat::cow_string{"abc"s}), "abc"sv);
ASSERT_EQ(std::format("{}", hat::cow_string{"abc"sv}), "abc"sv);
}

TEST(FormatterTest, CowCString) {
ASSERT_EQ(std::format("{}", hat::cow_cstring{"abc"s}), "abc"sv);
ASSERT_EQ(std::format("{}", hat::cow_cstring{"abc"_csv}), "abc"sv);
}

TEST(FormatterTest, CowSpan) {
#if __cpp_lib_format_ranges >= 202207L
std::vector data{1, 2, 3};
ASSERT_EQ(std::format("{}", hat::cow_span<int>{data}), "[1, 2, 3]"sv);
ASSERT_EQ(std::format("{}", hat::cow_span<int>{std::span{data}}), "[1, 2, 3]"sv);
// ASSERT_EQ(std::format("{}", hat::cow_writable_span<int>{data}), "[1, 2, 3]"sv);
// ASSERT_EQ(std::format("{}", hat::cow_writable_span<int>{std::span{data}}), "[1, 2, 3]"sv);
#else
GTEST_SKIP_("Formatting ranges requires C++23");
#endif
}

TEST(FormatterTest, SignatureElement) {
ASSERT_EQ(std::format("{}", hat::signature_element{std::byte{0x10}, std::byte{0xF0}}), "1?"sv);
}

TEST(FormatterTest, Signature) {
constexpr auto sig = "11 22 33"_sig;
ASSERT_EQ(std::format("{}", hat::signature(sig.begin(), sig.end())), "11 22 33"sv);
ASSERT_EQ(std::format("{}", hat::signature_view{sig}), "11 22 33"sv);
ASSERT_EQ(std::format("{}", sig), "11 22 33"sv);
}