dprint-swc-ext icon indicating copy to clipboard operation
dprint-swc-ext copied to clipboard

fix: ensure safe text slicing boundaries with multi-byte characters

Open MujahedSafaa opened this issue 1 year ago • 3 comments

This commit resolves a panic that occurs when the text_info function attempts to slice a string containing non-ASCII characters.

The fix addresses the issue reported in the Deno repository: https://github.com/denoland/deno/issues/23875

Use Case: The bug arises because the range_text method tries to slice a string using byte indices (start and end) that may not align with UTF-8 character boundaries. In Rust, attempting to slice a string at invalid UTF-8 boundaries results in a panic.

Solution: The solution involves modifying the range_text method to correctly handle non-ASCII characters. The method now uses char_indices() to collect character boundaries and maps the start and end byte indices to these boundaries. The resulting string slice is based on valid UTF-8 character boundaries, ensuring the operation is safe and the encoding remains intact.

MujahedSafaa avatar Sep 02 '24 14:09 MujahedSafaa

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Sep 02 '24 14:09 CLAassistant

@dsherret Could you please review this PR?

MujahedSafaa avatar Sep 05 '24 06:09 MujahedSafaa

I think it would be really great if we could have a unit test for this :)

magurotuna avatar Sep 08 '24 04:09 magurotuna