Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Playlists: Fix 'invalid byte sequence' error when subscribing #4887

Merged

Conversation

DmitrySandalov
Copy link
Contributor

@DmitrySandalov DmitrySandalov commented Aug 28, 2024

Fixes #4886

In Crystal, handling multi-byte sequences in UTF-8 requires understanding that slicing by bytes can lead to invalid sequences if the slicing isn't aligned with character boundaries. In this case, attempting to slice a string by bytes can cut through multi-byte UTF-8 sequences, leading to invalid sequences.

To avoid this, strings should be sliced based on characters rather than bytes.

Explanation:

  1. str.chars: Converts the string to an array of characters, which allows you to handle slicing based on characters rather than bytes.
  2. chars[start_index, length]: Slices the array of characters, which is safe for UTF-8 as it ensures no multi-byte sequences are broken.
  3. sliced_chars.join: Joins the sliced characters back into a string.

This approach ensures that the slicing respects the boundaries of UTF-8 characters and avoids invalid byte sequences.

@DmitrySandalov DmitrySandalov requested a review from a team as a code owner August 28, 2024 22:04
@DmitrySandalov DmitrySandalov requested review from syeopite and removed request for a team August 28, 2024 22:04
@SamantazFox SamantazFox changed the title Fix #4886 'invalid byte sequence' error when subscribing to playlists Playlists: Fix 'invalid byte sequence' error when subscribing Oct 6, 2024
@SamantazFox SamantazFox added in-testing This feature has been deployed and is being tested ready and removed in-testing This feature has been deployed and is being tested labels Oct 7, 2024
@SamantazFox SamantazFox merged commit 97895a4 into iv-org:master Oct 8, 2024
8 checks passed
@SamantazFox
Copy link
Member

Thank you for fixing that bug!

@DmitrySandalov DmitrySandalov deleted the fix/playlist-subscribe-error branch October 18, 2024 11:06
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Subscription to Playlist Fails with 'invalid byte sequence' error
2 participants