Skip to content
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

Add truncate_str_short() which always returns a prefix of the input #1761

Closed
wants to merge 1 commit into from

Conversation

th1000s
Copy link
Collaborator

@th1000s th1000s commented Jul 16, 2024

Document that the input of truncate_str() may be cut on a double width char ("fullwidth") - then the output is no longer a prefix of the input.

truncate_str_short() could be faster by not constructing the result but only returning an index, but it will be used in rare edge cases so reusing existing logic is good enough.

Common code moved to truncate_str_impl().

Assume (with fallback) that graphemes are at most 2 wide.
Fewer allocations.
Tests.

Document that the input of `truncate_str()` may be cut on a double width
char ("fullwidth") - then the output is no longer a prefix of the input.

`truncate_str_short()` could be faster by not constructing the result but
only returning an index, but it will be used in rare edge cases so reusing
existing logic is good enough.

Common code moved to `truncate_str_impl()`.

Assume (with fallback) that graphemes are at most 2 wide.
Fewer allocations.
Tests.
/// the resulting string is *shorter* than `display_width`. But this way the result is always a
/// prefix of the input `s`.
pub fn truncate_str_short(s: &str, display_width: usize) -> Cow<str> {
truncate_str_impl(s, display_width, "", None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy wants #[allow(dead_code)] here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just merge it with the --max-syntax-highlighting-length PR :)

@th1000s
Copy link
Collaborator Author

th1000s commented Jul 21, 2024

Now contained in PR #1746

@th1000s th1000s closed this Jul 21, 2024
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