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

Avoid string mutation for Ruby 3.4 #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ def self.strip_string(value, options = {})
replace_newlines = options[:replace_newlines]
regex = options[:regex]

value.gsub!(regex, "") if regex
value = value.gsub(regex, "") if regex

if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
value = if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
value.gsub(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
else
value.strip!
value.strip
end

value.gsub!(/[\r\n]+/, " ") if replace_newlines
value = value.gsub(/[\r\n]+/, " ") if replace_newlines

if collapse_spaces
if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
value.gsub!(/#{MULTIBYTE_BLANK}+/, " ")
value = if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
value.gsub(/#{MULTIBYTE_BLANK}+/, " ")
else
value.squeeze!(" ")
value.squeeze(" ")
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/strip_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_should_strip_unicode
skip "multi-byte characters not supported by this version of Ruby" unless StripAttributes::MULTIBYTE_SUPPORTED

assert_equal "foo", StripAttributes.strip("\u200A\u200B foo\u200A\u200B ")
assert_equal "foo\u20AC".force_encoding("ASCII-8BIT"), StripAttributes.strip("foo\u20AC ".force_encoding("ASCII-8BIT"))
assert_equal "foo\u20AC".b, StripAttributes.strip("foo\u20AC ".b)
end
end
end