Skip to content

Conversation

sheldonh
Copy link

Fixes #9

@sheldonh
Copy link
Author

~/git/json-stream (fix-9 %=)$ RUBYOPT="--enable=frozen-string-literal" bin/rake test
Run options: --seed 54361

# Running:

......................................................................................................................................................

Finished in 0.016708s, 8977.5977 runs/s, 15621.0199 assertions/s.

150 runs, 261 assertions, 0 failures, 0 errors, 0 skips

# Avoid state machine for complete UTF-8.
if @buffer.empty?
data.force_encoding(Encoding::UTF_8)
(+data).force_encoding(Encoding::UTF_8)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work?
See also: #8 (comment)

Copy link
Author

@sheldonh sheldonh Jul 29, 2025

Choose a reason for hiding this comment

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

@kou Yes, this works. (Edited. Sorry, I misunderstood the concern you were raising, initially).

The construct (+data) (or String.new(data)) already returns a dup if the String is frozen. Another dup is unnecessary.

3.4.5 :022 > x = String.new("foo", encoding: 'ASCII-8BIT').freeze;
             y = (+x).force_encoding('UTF-8');
             z = (+y);
3.4.5 :023 > [x, y, z].map(&:encoding)
 => [#<Encoding:BINARY (ASCII-8BIT)>, #<Encoding:UTF-8>, #<Encoding:UTF-8>]
3.4.5 :024 > [x, y, z].map(&:frozen?)
 => [true, false, false]
3.4.5 :025 > [x, y, z].map(&:object_id)
 => [326840, 326848, 326848]

By the way, this change (to #<<) seems like it's only necessary in test context. I can't imagine real world cases where the append data would be a frozen String literal. I don't love the fact that we mutate our inputs, but that was already happening.

Copy link
Contributor

Choose a reason for hiding this comment

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

If data is frozen, (+data).force_encoding(Encoding::UTF_8) does nothing because the (+data).force_encoding(Encoding::UTF_8) result is ignored. The below if data.valid_encoding? check may be done with non UTF-8 encoding.

If data's encoding is always UTF-8, we don't need this (+data).force_encoding(Encoding::UTF_8) entirely.

If data's encoding may not be UTF-8, we need data = (+data).force_encoding(Encoding::UTF_8) or data = +data; data.force_encoding(Encoding::UTF_8).

Copy link
Author

Choose a reason for hiding this comment

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

@kou Ah, that is correct, thank you! I missed the required reassignment. Fixed in 831698f.

@AxelTheGerman
Copy link

Is this something that could get merged? Just making sure this library stays ahead of the frozen string literal changes.

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.

Fix mutation of string literals
3 participants