Skip to content

Commit

Permalink
Added std formatter for path view, but as no current standard library
Browse files Browse the repository at this point in the history
implements formatters for filesystem paths, no idea if it works or not.
  • Loading branch information
ned14 committed Aug 29, 2024
1 parent c9e6f6c commit 92dd7a5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/llfio/revision.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define LLFIO_PREVIOUS_COMMIT_REF 2ff292feda4aaece95ffcc5bc35095281ca45dfd
#define LLFIO_PREVIOUS_COMMIT_DATE "2024-08-29 10:58:20 +00:00"
#define LLFIO_PREVIOUS_COMMIT_UNIQUE 2ff292fe
#define LLFIO_PREVIOUS_COMMIT_REF c9e6f6c8accb5ce8a36607191955ad59e5cba099
#define LLFIO_PREVIOUS_COMMIT_DATE "2024-08-29 11:02:08 +00:00"
#define LLFIO_PREVIOUS_COMMIT_UNIQUE c9e6f6c8
54 changes: 54 additions & 0 deletions include/llfio/v2.0/path_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Distributed under the Boost Software License, Version 1.0.
#include <compare>
#endif

#if __cplusplus >= 202600L || _HAS_CXX26
#include <format>
#define LLFIO_PATH_VIEW_HAVE_FORMAT 1
#endif

#include "quickcpplib/algorithm/hash.hpp"
#include "quickcpplib/algorithm/string.hpp"

Expand Down Expand Up @@ -3199,6 +3204,55 @@ static_assert(std::is_trivially_copyable<path_view>::value, "path_view is not a

LLFIO_V2_NAMESPACE_END

#if LLFIO_PATH_VIEW_HAVE_FORMAT
template <class charT> struct std::formatter<LLFIO_V2_NAMESPACE::path_view_component, charT>
{
std::formatter<std::filesystem::path, charT> fmt;
constexpr void set_debug_format() { fmt.set_debug_format(); }
constexpr typename std::basic_format_parse_context<charT>::iterator parse(std::basic_format_parse_context<charT> &ctx) { return fmt.parse(ctx); }
template <class FormatContext> typename FormatContext::iterator format(const LLFIO_V2_NAMESPACE::path_view_component &pv, FormatContext &ctx) const
{
return LLFIO_V2_NAMESPACE::visit(
[&](auto sv)
{
using type = typename decltype(sv)::value_type;
if constexpr(std::is_same_v<type, std::byte>)
{
return fmt.format(std::filesystem::path(QUICKCPPLIB_NAMESPACE::algorithm::string::to_hex_string((const char *) v.data(), v.size())), ctx);
}
else
{
return fmt.format(std::filesystem::path(sv), ctx);
}
},
pv);
}
};
template <class charT> struct std::formatter<LLFIO_V2_NAMESPACE::path_view, charT>
{
std::formatter<std::filesystem::path, charT> fmt;
constexpr void set_debug_format() { fmt.set_debug_format(); }
constexpr typename std::basic_format_parse_context<charT>::iterator parse(std::basic_format_parse_context<charT> &ctx) { return fmt.parse(ctx); }
template <class FormatContext> typename FormatContext::iterator format(const LLFIO_V2_NAMESPACE::path_view &pv, FormatContext &ctx) const
{
return LLFIO_V2_NAMESPACE::visit(
[&](auto sv)
{
using type = typename decltype(sv)::value_type;
if constexpr(std::is_same_v<type, std::byte>)
{
return fmt.format(std::filesystem::path(QUICKCPPLIB_NAMESPACE::algorithm::string::to_hex_string((const char *) v.data(), v.size())), ctx);
}
else
{
return fmt.format(std::filesystem::path(sv), ctx);
}
},
pv);
}
};
#endif

#if LLFIO_HEADERS_ONLY == 1 && !defined(DOXYGEN_SHOULD_SKIP_THIS)
#define LLFIO_INCLUDED_BY_HEADER 1
#include "detail/impl/path_view.ipp"
Expand Down
11 changes: 11 additions & 0 deletions test/tests/path_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,17 @@ static inline void TestPathView()
BOOST_CHECK(zbuff.allocator().deleted == 1);
BOOST_CHECK(zbuff.allocator().sig == 0); // default initialised
}

#if LLFIO_PATH_VIEW_HAVE_FORMAT
{
std::string a, b;
std::format_to(std::back_inserter(a), std::filesystem::path("a/b/c"));
std::format_to(std::back_inserter(b), llfio::path_view("a/b/c"));
std::cout << "std::format(path) = " << a << std::endl;
std::cout << "std::format(path_view) = " << b << std::endl;
BOOST_CHECK(a == b);
}
#endif
}

KERNELTEST_TEST_KERNEL(integration, llfio, path_view, path_view, "Tests that llfio::path_view() works as expected", TestPathView())

0 comments on commit 92dd7a5

Please sign in to comment.