-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Interpret precision as display width #4443
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
Open
nikhilreddydev
wants to merge
6
commits into
fmtlib:master
Choose a base branch
from
nikhilreddydev:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c11646
use display width precision
nikhilreddydev 9609f0d
use constexpr to compile only relevant branch
nikhilreddydev 07cf0ce
use enable_if
nikhilreddydev 4857e73
fix formatting
nikhilreddydev b80b43d
handle unused parameters
nikhilreddydev 0b6c4e0
remove unnecessay include
nikhilreddydev 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2110,13 +2110,51 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, | |
return write_int<Char>(out, make_write_int_arg(value, specs.sign()), specs); | ||
} | ||
|
||
FMT_INLINE auto count_code_points_with_display_width_precision( | ||
string_view s, size_t display_width_precision) -> size_t { | ||
size_t display_width = 0; | ||
size_t code_points = 0; | ||
|
||
// Iterate through the string to compute display width | ||
for_each_codepoint(s, [&](uint32_t, string_view sv) { | ||
// Compute the display width of the current code point | ||
size_t cp_width = compute_width(sv); | ||
if (display_width + cp_width > display_width_precision) { | ||
return false; // Stop iteration when display width exceeds precision | ||
} | ||
|
||
display_width += cp_width; | ||
code_points++; | ||
return true; | ||
}); | ||
|
||
return code_points; | ||
} | ||
|
||
template <typename Char> | ||
FMT_CONSTEXPR auto handle_precision( | ||
basic_string_view<Char> s, const Char* data, const format_specs& specs, | ||
FMT_ENABLE_IF(std::is_same<Char, char>::value)) -> size_t { | ||
auto code_points = count_code_points_with_display_width_precision( | ||
to_string_view(data), to_unsigned(specs.precision)); | ||
return code_point_index(s, to_unsigned(code_points)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
template <typename Char> | ||
FMT_CONSTEXPR auto handle_precision( | ||
basic_string_view<Char> s, const Char*, const format_specs&, | ||
FMT_ENABLE_IF(!std::is_same<Char, char>::value)) -> size_t { | ||
return code_point_index(s, to_unsigned(s.size())); | ||
} | ||
|
||
template <typename Char, typename OutputIt> | ||
FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s, | ||
const format_specs& specs) -> OutputIt { | ||
auto data = s.data(); | ||
auto size = s.size(); | ||
if (specs.precision >= 0 && to_unsigned(specs.precision) < size) | ||
size = code_point_index(s, to_unsigned(specs.precision)); | ||
if (specs.precision >= 0 && to_unsigned(specs.precision) < size) { | ||
size = handle_precision(s, data, specs); | ||
} | ||
|
||
bool is_debug = specs.type() == presentation_type::debug; | ||
if (is_debug) { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to separately pass
data
- you can uses
instead.