diff options
author | Francesco Rodríguez <frodsan@me.com> | 2018-10-20 20:17:05 +0200 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2018-10-20 12:00:37 -0700 |
commit | d684778a000cdcfcac53f6390c565c6a91a3391e (patch) | |
tree | 31f34bbd343cbee008232c62316071ea64de4b2b | |
parent | 5943a5562eeea16afc5b809a1fcdd497ccec9465 (diff) | |
download | rails-d684778a000cdcfcac53f6390c565c6a91a3391e.tar.gz rails-d684778a000cdcfcac53f6390c565c6a91a3391e.tar.bz2 rails-d684778a000cdcfcac53f6390c565c6a91a3391e.zip |
Use String#truncate_bytes inside Multibyte::Chars#limit
-rw-r--r-- | activesupport/lib/active_support/multibyte/chars.rb | 14 | ||||
-rw-r--r-- | activesupport/test/multibyte_chars_test.rb | 18 |
2 files changed, 1 insertions, 31 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index bd8cab3460..a1e23aeaca 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -122,7 +122,7 @@ module ActiveSupport #:nodoc: # # 'こんにちは'.mb_chars.limit(7).to_s # => "こん" def limit(limit) - slice(0...translate_offset(limit)) + truncate_bytes(limit, omission: nil) end # Capitalizes the first letter of every word, when possible. @@ -208,18 +208,6 @@ module ActiveSupport #:nodoc: private - def translate_offset(byte_offset) - return nil if byte_offset.nil? - return 0 if @wrapped_string == "" - - begin - @wrapped_string.byteslice(0...byte_offset).unpack("U*").length - rescue ArgumentError - byte_offset -= 1 - retry - end - end - def chars(string) self.class.new(string) end diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 6b33f5f9b2..f87099566b 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -785,21 +785,3 @@ class MultibyteCharsExtrasTest < ActiveSupport::TestCase end.pack("U*") end end - -class MultibyteInternalsTest < ActiveSupport::TestCase - include MultibyteTestHelpers - - test "Chars translates a character offset to a byte offset" do - example = chars("Puisque c'était son erreur, il m'a aidé") - [ - [0, 0], - [3, 3], - [12, 11], - [14, 13], - [41, 39] - ].each do |byte_offset, character_offset| - assert_equal character_offset, example.send(:translate_offset, byte_offset), - "Expected byte offset #{byte_offset} to translate to #{character_offset}" - end - end -end |