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

Fix: Honour encoding in IO::Memory#to_s #11875

Conversation

straight-shoota
Copy link
Member

Resolves #11015

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:text labels Mar 5, 2022
@@ -393,7 +393,11 @@ class IO::Memory < IO
# ```
def to_s : String
if encoding = @encoding
String.new to_slice, encoding: encoding.name, invalid: encoding.invalid
{% if flag?(:without_iconv) %}
raise NotImplementedError.new("String.encode")
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't be better to move this check into the String.encode method instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I wondered about that. But String.encode is explicit about being for encoding purposes. Calling that method with -Dwithout_iconv should lead to a compile time error. IO::Memory#to_s on the other hand is not explicitly about encoding. It's just something that it needs to do in some state. In this case there should definitely not be a compile time error. So raising the runtime error here makes more sense IMO.

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

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

Thank you @straight-shoota 🙏

@straight-shoota straight-shoota added this to the 1.4.0 milestone Mar 14, 2022
@straight-shoota straight-shoota merged commit 03ed280 into crystal-lang:master Mar 17, 2022
@straight-shoota straight-shoota deleted the fix/io_memory-to_s-encoding branch March 17, 2022 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:text
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IO::Memory#to_s should honor encoding options
4 participants