From dd99658e453dc965ffd00316aa943982d1de229f Mon Sep 17 00:00:00 2001 From: Alex Muntean Date: Fri, 28 May 2010 13:22:29 +0900 Subject: [PATCH] Fix ActiveSupport::Multibyte::Chars#slice for empty strings when starting offset is negative --- .../lib/active_support/multibyte/chars.rb | 3 ++- activesupport/test/multibyte_chars_test.rb | 1 + 2 files changed, 3 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 32d97a6..3b7624a 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -344,7 +344,8 @@ module ActiveSupport #:nodoc: character = self.class.u_unpack(@wrapped_string)[args[0]] result = character.nil? ? nil : [character].pack('U') else - result = self.class.u_unpack(@wrapped_string).slice(*args).pack('U*') + cps = self.class.u_unpack(@wrapped_string).slice(*args) + result = cps.nil? ? nil : cps.pack('U*') end result.nil? ? nil : chars(result) end diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 98dcfbd..589ed29 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -401,6 +401,7 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase assert_equal 'こ', @chars.slice(0) assert_equal 'わ', @chars.slice(3) assert_equal nil, ''.mb_chars.slice(-1..1) + assert_equal nil, ''.mb_chars.slice(-1, 1) assert_equal '', ''.mb_chars.slice(0..10) assert_equal 'にちわ', @chars.slice(1..3) assert_equal 'にちわ', @chars.slice(1, 3) -- 1.7.1