Skip to content

Commit 92dd7a5

Browse files
committed
Added std formatter for path view, but as no current standard library
implements formatters for filesystem paths, no idea if it works or not.
1 parent c9e6f6c commit 92dd7a5

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

include/llfio/revision.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
2-
#define LLFIO_PREVIOUS_COMMIT_REF 2ff292feda4aaece95ffcc5bc35095281ca45dfd
3-
#define LLFIO_PREVIOUS_COMMIT_DATE "2024-08-29 10:58:20 +00:00"
4-
#define LLFIO_PREVIOUS_COMMIT_UNIQUE 2ff292fe
2+
#define LLFIO_PREVIOUS_COMMIT_REF c9e6f6c8accb5ce8a36607191955ad59e5cba099
3+
#define LLFIO_PREVIOUS_COMMIT_DATE "2024-08-29 11:02:08 +00:00"
4+
#define LLFIO_PREVIOUS_COMMIT_UNIQUE c9e6f6c8

include/llfio/v2.0/path_view.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Distributed under the Boost Software License, Version 1.0.
3434
#include <compare>
3535
#endif
3636

37+
#if __cplusplus >= 202600L || _HAS_CXX26
38+
#include <format>
39+
#define LLFIO_PATH_VIEW_HAVE_FORMAT 1
40+
#endif
41+
3742
#include "quickcpplib/algorithm/hash.hpp"
3843
#include "quickcpplib/algorithm/string.hpp"
3944

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

32003205
LLFIO_V2_NAMESPACE_END
32013206

3207+
#if LLFIO_PATH_VIEW_HAVE_FORMAT
3208+
template <class charT> struct std::formatter<LLFIO_V2_NAMESPACE::path_view_component, charT>
3209+
{
3210+
std::formatter<std::filesystem::path, charT> fmt;
3211+
constexpr void set_debug_format() { fmt.set_debug_format(); }
3212+
constexpr typename std::basic_format_parse_context<charT>::iterator parse(std::basic_format_parse_context<charT> &ctx) { return fmt.parse(ctx); }
3213+
template <class FormatContext> typename FormatContext::iterator format(const LLFIO_V2_NAMESPACE::path_view_component &pv, FormatContext &ctx) const
3214+
{
3215+
return LLFIO_V2_NAMESPACE::visit(
3216+
[&](auto sv)
3217+
{
3218+
using type = typename decltype(sv)::value_type;
3219+
if constexpr(std::is_same_v<type, std::byte>)
3220+
{
3221+
return fmt.format(std::filesystem::path(QUICKCPPLIB_NAMESPACE::algorithm::string::to_hex_string((const char *) v.data(), v.size())), ctx);
3222+
}
3223+
else
3224+
{
3225+
return fmt.format(std::filesystem::path(sv), ctx);
3226+
}
3227+
},
3228+
pv);
3229+
}
3230+
};
3231+
template <class charT> struct std::formatter<LLFIO_V2_NAMESPACE::path_view, charT>
3232+
{
3233+
std::formatter<std::filesystem::path, charT> fmt;
3234+
constexpr void set_debug_format() { fmt.set_debug_format(); }
3235+
constexpr typename std::basic_format_parse_context<charT>::iterator parse(std::basic_format_parse_context<charT> &ctx) { return fmt.parse(ctx); }
3236+
template <class FormatContext> typename FormatContext::iterator format(const LLFIO_V2_NAMESPACE::path_view &pv, FormatContext &ctx) const
3237+
{
3238+
return LLFIO_V2_NAMESPACE::visit(
3239+
[&](auto sv)
3240+
{
3241+
using type = typename decltype(sv)::value_type;
3242+
if constexpr(std::is_same_v<type, std::byte>)
3243+
{
3244+
return fmt.format(std::filesystem::path(QUICKCPPLIB_NAMESPACE::algorithm::string::to_hex_string((const char *) v.data(), v.size())), ctx);
3245+
}
3246+
else
3247+
{
3248+
return fmt.format(std::filesystem::path(sv), ctx);
3249+
}
3250+
},
3251+
pv);
3252+
}
3253+
};
3254+
#endif
3255+
32023256
#if LLFIO_HEADERS_ONLY == 1 && !defined(DOXYGEN_SHOULD_SKIP_THIS)
32033257
#define LLFIO_INCLUDED_BY_HEADER 1
32043258
#include "detail/impl/path_view.ipp"

test/tests/path_view.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,17 @@ static inline void TestPathView()
357357
BOOST_CHECK(zbuff.allocator().deleted == 1);
358358
BOOST_CHECK(zbuff.allocator().sig == 0); // default initialised
359359
}
360+
361+
#if LLFIO_PATH_VIEW_HAVE_FORMAT
362+
{
363+
std::string a, b;
364+
std::format_to(std::back_inserter(a), std::filesystem::path("a/b/c"));
365+
std::format_to(std::back_inserter(b), llfio::path_view("a/b/c"));
366+
std::cout << "std::format(path) = " << a << std::endl;
367+
std::cout << "std::format(path_view) = " << b << std::endl;
368+
BOOST_CHECK(a == b);
369+
}
370+
#endif
360371
}
361372

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

0 commit comments

Comments
 (0)