-
Notifications
You must be signed in to change notification settings - Fork 102
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
Conversation
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.
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).
spec/custom_tags_spec.rb
Outdated
html << %Q{ </a>\n} | ||
html << %Q{ <p>Figure #{label}</p>\n} | ||
html << %Q{<div>\n} | ||
html += %Q{ <a class="fig" href="/images/#{img}">\n} |
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.
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.
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.
That makes sense. It makes the diff smaller too.
spec/parser_spec.rb
Outdated
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" |
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.
Rather than having version-specific logic here, I'd recommend just making the initial string mutable: s = "\xa3".dup
lib/redcloth/formatters/base.rb
Outdated
@@ -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| |
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'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 🤷♂️
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.
@joshuasiler I added a commit that fixes the build failure on ruby-head. |
@joshuasiler can you take another look please? |
Thanks, @joshuasiler! |
Awesome! :) A new gem release would be very welcome now... |
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.