-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: Refactor string truncation logic into reusable utility function to avoid panic (#2818) #2819
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba74174
Refactor string truncation logic into reusable utility function
toyamagu-2021 49386ec
Merge remote-tracking branch 'upstream/main' into fix/2818
toyamagu-2021 06e9f9f
fix: lint
toyamagu-2021 2a5b4df
Merge remote-tracking branch 'upstream/main' into fix/2818
toyamagu-2021 2b835eb
fix: lint and pick https://github.com/block/goose/pull/3243
toyamagu-2021 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /// Utility functions for safe string handling and other common operations | ||
| /// Safely truncate a string at character boundaries, not byte boundaries | ||
| /// | ||
| /// This function ensures that multi-byte UTF-8 characters (like Japanese, emoji, etc.) | ||
| /// are not split in the middle, which would cause a panic. | ||
| /// | ||
| /// # Arguments | ||
| /// * `s` - The string to truncate | ||
| /// * `max_chars` - Maximum number of characters to keep | ||
| /// | ||
| /// # Returns | ||
| /// A truncated string with "..." appended if truncation occurred | ||
| pub fn safe_truncate(s: &str, max_chars: usize) -> String { | ||
| if s.chars().count() <= max_chars { | ||
| s.to_string() | ||
| } else { | ||
| let truncated: String = s.chars().take(max_chars.saturating_sub(3)).collect(); | ||
| format!("{}...", truncated) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_safe_truncate_ascii() { | ||
| assert_eq!(safe_truncate("hello world", 20), "hello world"); | ||
| assert_eq!(safe_truncate("hello world", 8), "hello..."); | ||
| assert_eq!(safe_truncate("hello", 5), "hello"); | ||
| assert_eq!(safe_truncate("hello", 3), "..."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_safe_truncate_japanese() { | ||
| // Japanese characters: "こんにちは世界" (Hello World) | ||
| let japanese = "こんにちは世界"; | ||
| assert_eq!(safe_truncate(japanese, 10), japanese); | ||
| assert_eq!(safe_truncate(japanese, 5), "こん..."); | ||
| assert_eq!(safe_truncate(japanese, 7), japanese); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_safe_truncate_mixed() { | ||
| // Mixed ASCII and Japanese | ||
| let mixed = "Hello こんにちは"; | ||
| assert_eq!(safe_truncate(mixed, 20), mixed); | ||
| assert_eq!(safe_truncate(mixed, 8), "Hello..."); | ||
| } | ||
| } | ||
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.
Totally fine as is, just a note this walks the string twice. Hopefully we aren't passing large file data here anyways.
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 also came across https://doc.rust-lang.org/std/primitive.str.html#method.floor_char_boundary which is exactly what we want here, but yeah still experimental.