-
Notifications
You must be signed in to change notification settings - Fork 14
🎨 Make limited formatting available in freestanding #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| #pragma once | ||
|
|
||
| #if not STDX_FMT_FREESTANDING | ||
|
|
||
| #include <fmt/compile.h> | ||
| #include <fmt/format.h> | ||
|
|
||
| #include <string_view> | ||
|
|
||
| #define STDX_FMT_COMPILE(Fmt) FMT_COMPILE(std::string_view{Fmt}) | ||
|
|
||
| #else | ||
|
|
||
| #include <stdx/compiler.hpp> | ||
| #include <stdx/concepts.hpp> | ||
| #include <stdx/ranges.hpp> | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| #define STDX_FMT_COMPILE(X) [] { return X; } | ||
|
|
||
| namespace stdx { | ||
| inline namespace v1 { | ||
| namespace fmt { | ||
|
|
||
| namespace detail { | ||
| struct fmt_spec { | ||
| int len{}; | ||
| int base{}; | ||
| bool well_formed{true}; | ||
| }; | ||
|
|
||
| CONSTEVAL auto parse_fmt_spec(auto fmtstr) -> fmt_spec { | ||
| constexpr auto fmt = fmtstr(); | ||
| auto i = fmt.begin(); | ||
| while (*i != '{') { | ||
| ++i; | ||
| } | ||
| ++i; | ||
| if (*i == '}') { | ||
| return {.len = 2, .base = 10}; | ||
| } else if (*i++ == ':' and *i++ == 'x' and *i == '}') { | ||
| return {.len = 4, .base = 16}; | ||
| } | ||
| return {.well_formed = false}; | ||
| } | ||
|
|
||
| template <stdx::range R> | ||
| CONSTEVAL auto formatted_size(fmt_spec, R const &r) -> std::size_t { | ||
| return r.size(); | ||
| } | ||
|
|
||
| CONSTEVAL auto formatted_size(fmt_spec, char const *str) -> std::size_t { | ||
| std::size_t sz{}; | ||
| while (*str) { | ||
| ++sz; | ||
| ++str; | ||
| } | ||
| return sz; | ||
| } | ||
|
|
||
| CONSTEVAL auto formatted_size(fmt_spec, char) -> std::size_t { return 1; } | ||
|
|
||
| template <stdx::integral I> | ||
| CONSTEVAL auto formatted_size(fmt_spec s, I i) -> std::size_t { | ||
| if (i == 0) { | ||
| return 1; | ||
| } else { | ||
| std::size_t sz{}; | ||
| if (i < 0) { | ||
| ++sz; | ||
| } | ||
|
|
||
| while (i != 0) { | ||
| ++sz; | ||
| i /= s.base; | ||
| } | ||
| return sz; | ||
| } | ||
| } | ||
| } // namespace detail | ||
|
|
||
| CONSTEVAL auto formatted_size(auto fmtstr, auto v) -> std::size_t { | ||
| constexpr auto spec = detail::parse_fmt_spec(fmtstr); | ||
| static_assert( | ||
| spec.well_formed, | ||
| "Freestanding fmt implementation does not support that format " | ||
| "specifier"); | ||
|
|
||
| return fmtstr().size() - spec.len + detail::formatted_size(spec, v); | ||
| } | ||
|
|
||
| namespace detail { | ||
| template <typename It, stdx::range R> | ||
| CONSTEVAL auto format_to(fmt_spec, It dest, R const &r) -> It { | ||
| for (auto const c : r) { | ||
| *dest++ = c; | ||
| } | ||
| return dest; | ||
| } | ||
|
|
||
| template <typename It> | ||
| CONSTEVAL auto format_to(fmt_spec, It dest, char const *str) -> It { | ||
| while (*str) { | ||
| *dest++ = *str++; | ||
| } | ||
| return dest; | ||
| } | ||
|
|
||
| template <typename It> | ||
| CONSTEVAL auto format_to(fmt_spec, It dest, char c) -> It { | ||
| *dest++ = c; | ||
| return dest; | ||
| } | ||
|
|
||
| template <typename It, stdx::integral I> | ||
| CONSTEVAL auto format_to(fmt_spec s, It dest, I i) -> It { | ||
| if (i == 0) { | ||
| *dest++ = '0'; | ||
| } else { | ||
| auto abs = []<typename T>(T v) -> T { return v < 0 ? -v : v; }; | ||
| auto to_digit = [](auto n) -> char { return "0123456789abcdef"[n]; }; | ||
|
|
||
| if (i < 0) { | ||
| *dest++ = '-'; | ||
| } | ||
|
|
||
| auto b = dest; | ||
| while (i != 0) { | ||
| auto digit = to_digit(abs(i % s.base)); | ||
| i /= s.base; | ||
| *dest++ = digit; | ||
| } | ||
|
|
||
| auto e = dest - 1; | ||
| while (b < e) { | ||
| auto tmp = *b; | ||
| *b++ = *e; | ||
| *e-- = tmp; | ||
elbeno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| return dest; | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| template <typename It> | ||
| CONSTEVAL auto format_to(It dest, auto fmtstr, auto v) -> void { | ||
| constexpr auto fmt = fmtstr(); | ||
| constexpr auto spec = detail::parse_fmt_spec(fmtstr); | ||
|
|
||
| // copy to opening { | ||
| auto src = fmt.begin(); | ||
| while (*src != '{') { | ||
| *dest++ = *src++; | ||
| } | ||
|
|
||
| // skip fmt spec | ||
| src += spec.len; | ||
|
|
||
| // format the value into the buffer | ||
| dest = detail::format_to(spec, dest, v); | ||
|
|
||
| // copy the rest of src | ||
| while (*src) { | ||
| *dest++ = *src++; | ||
| } | ||
| } | ||
|
|
||
| } // namespace fmt | ||
| } // namespace v1 | ||
| } // namespace stdx | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elbeno marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.