Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make String#chars slicing behaviour consistent with String. Closes #6387

 [Manfred Stienstra]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5286 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit cb927c8774f98c979b4e0466798d74bdd23a79f2 1 parent 7870494
Michael Koziarski NZKoz authored
2  activesupport/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Make String#chars slicing behaviour consistent with String. Closes #6387 [Manfred Stienstra]
+
* Pull in latest multibye patch. Closes #6346 [Manfred Stienstra]
* Add ActiveSupport::Multibyte. Provides String#chars which lets you deal with strings as a sequence of chars, not of bytes. Closes #6242 [Julian Tarkhanov, Manfred Stienstra, Thijs van der Vossen & Jan Behrens]
6 activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
View
@@ -162,10 +162,10 @@ def slice(str, *args)
elsif args[0].kind_of? Range
cps = u_unpack(str).slice(*args)
cps.nil? ? nil : cps.pack('U*')
- elsif args[0].kind_of? Numeric
+ elsif args.size == 1 && args[0].kind_of?(Numeric)
u_unpack(str)[args[0]]
else
- str.slice(*args)
+ u_unpack(str).slice(*args).pack('U*')
end
end
alias_method :[], :slice
@@ -434,4 +434,4 @@ def compose_codepoints(codepoints)
UCD = UnicodeDatabase.new
end
end
-end
+end
4 activesupport/test/multibyte_handler_test.rb
View
@@ -157,6 +157,8 @@ def test_slice
assert_equal nil, @handler.slice('', -1..1), "Broken range should return nil"
assert_equal '', @handler.slice('', 0..10), "Empty string should not break things"
assert_equal "d Блå ffi", @handler.slice(@string, 3..9), "Unicode characters have to be returned"
+ assert_equal "d Блå ffi", @handler.slice(@string, 3, 7), "Unicode characters have to be returned"
+ assert_equal "A", @handler.slice(@string, 0, 1), "Slicing from an offset should return characters"
assert_equal " Блå ffi ", @handler.slice(@string, 4..10), "Unicode characters have to be returned"
assert_equal "", @handler.slice(@string, 7..6), "Range is empty, should return an empty string"
assert_raise(ActiveSupport::Multibyte::Handlers::EncodingError) { @handler.slice(@bytestring, 2..3) }
@@ -266,4 +268,4 @@ def setup
common_setup
@handler = ::ActiveSupport::Multibyte::Handlers::UTF8Handler
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.