Skip to content

Avoid redundant work when processing UTF-8 strings #4475

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 1 commit into from
Jun 20, 2025

Conversation

localspook
Copy link
Contributor

convert_precision_to_size calculates how many bytes of a UTF-8 string we can print before we exceed a display width limit, but does it using a weird two-step:

  1. Walk the string, calculating the number of code points we can print, and throw away the position we stopped at.
  2. Walk the string again to convert the number of code points into a number of bytes (i.e. recalculate the position we stopped at).

Put like that, the fix is clear. Also, this is the only place code_point_index, the function that does step 2, is used, so we delete it.

@vitaut vitaut merged commit 067bc47 into fmtlib:master Jun 20, 2025
45 checks passed
@vitaut
Copy link
Contributor

vitaut commented Jun 20, 2025

Nice improvement, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants