Skip to content

Conversation

remi-dupre
Copy link
Contributor

@remi-dupre remi-dupre commented Mar 15, 2025

Hello!

While looking again at #206 I bumped into a few issues in current implementation for truncate_str.

  1. It may truncate if not required if measure_width(s) + measure_width(tail) > width : truncate_str("foo bar", 7, "!") == "foo ba!". I don't think this is the expected behavior.
  2. Using a tail longer than text width leads to overflowing substractions, which gives "undefined behavior" (ish) :
// In debug mode, panics:
truncate_str("foo bar", 2, "!!!")  # panics
// In release mode, weird result:
truncate_str("foo bar", 2, "!!!") == "foo bar"

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

Makes sense, thanks!

@djc djc merged commit 72aa50e into console-rs:main Mar 16, 2025
15 checks passed
@djc djc changed the title fix(utils): undefined behavior in truncate_str when tail is larger than width fix(utils): surprising behavior in truncate_str when tail is larger than width Mar 16, 2025
@remi-dupre remi-dupre deleted the truncate_ansi_ub branch March 16, 2025 11:00
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