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

Immutable strings #38

Merged
merged 4 commits into from
Sep 13, 2017
Merged

Immutable strings #38

merged 4 commits into from
Sep 13, 2017

Conversation

mvz
Copy link
Contributor

@mvz mvz commented Aug 26, 2017

This PR fixes the cases where RedCloth doesn't work when string immutability is set.

  • Removes the line from Rakefile that sets RUBYOPT to allow setting string immutablity like so:
    RUBYOPT="--enable-frozen-string-literal" be rake
    (I kind of understand where this line came from but perhaps there is another way to achieve the same effect).

  • Updates the code of RedCloth and specs to not mutate string literals.

Background: This PR came from pry/pry#1625, via the build failures of coderay with string immutability set (https://travis-ci.org/rubychan/coderay/jobs/261625648).

I have not updated the Travis configuration to actually run with immutable strings, since tests will fail at the end due to RSpec mutating string literals.

Copy link

@pat pat left a comment

Choose a reason for hiding this comment

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

Great to see this PR! I've added a few comments (purely based on the similar frozen-string-literal changes I've made elsewhere - I've not contributed to this repo before, so the maintainers may have clearly different preferences to mind).

html << %Q{ </a>\n}
html << %Q{ <p>Figure #{label}</p>\n}
html << %Q{<div>\n}
html += %Q{ <a class="fig" href="/images/#{img}">\n}
Copy link

Choose a reason for hiding this comment

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

Better to make the initial string mutable (using .dup at the end), and then keeping all the << calls. Using += means extra string allocations that aren't really necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. It makes the diff smaller too.

output = RedCloth.new(input).to_html

output.should == "<p>This is an <span class=\"caps\">ISO</span>-8859-1 string</p>"
output.encoding.to_s.should == "ISO-8859-1"
end

it "should not raise ArgumentError: invalid byte sequence" do
s = "\xa3"
s.force_encoding 'iso-8859-1'
if RUBY_VERSION > "2.3.0"
Copy link

Choose a reason for hiding this comment

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

Rather than having version-specific logic here, I'd recommend just making the initial string mutable: s = "\xa3".dup

@@ -37,10 +36,10 @@ def pba(opts)
[:"padding-right", :"padding-left"].each do |a|
opts[:style] = "#{a}:#{opts[a]}em;#{opts[:style]}" if opts[a]
end
[:style, :class, :lang, :id, :colspan, :rowspan, :title, :start, :align].each do |a|
atts << " #{a}=\"#{ html_esc(opts[a].to_s, :html_escape_attributes) }\"" if opts[a]
atts = [:style, :class, :lang, :id, :colspan, :rowspan, :title, :start, :align].map do |a|
Copy link

Choose a reason for hiding this comment

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

I've no strong preference here - perhaps the maintainers do - but I'd probably just make the definition of atts in the original version mutable with atts = ''.dup. Much of a muchness when it comes to the number of object allocations though 🤷‍♂️

@joshuasiler
Copy link
Contributor

Hi, thank you for your contribution. I'll be happy to merge this in once the CI build is fixed.

The .load_documents method is deprecated and seems to have been removed
entirely in the upcoming Ruby 2.5.
@mvz
Copy link
Contributor Author

mvz commented Sep 1, 2017

@joshuasiler I added a commit that fixes the build failure on ruby-head.

@mvz
Copy link
Contributor Author

mvz commented Sep 13, 2017

@joshuasiler can you take another look please?

@joshuasiler joshuasiler merged commit 4808b36 into jgarber:master Sep 13, 2017
@mvz mvz deleted the immutable-strings branch September 14, 2017 05:24
@mvz
Copy link
Contributor Author

mvz commented Sep 14, 2017

Thanks, @joshuasiler!

@korny
Copy link
Contributor

korny commented Sep 14, 2017

Awesome! :) A new gem release would be very welcome now...

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.

4 participants