aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb8
-rw-r--r--activesupport/test/multibyte_chars_test.rb7
2 files changed, 11 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index 3c16999e43..64a35dca40 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -206,7 +206,8 @@ module ActiveSupport #:nodoc:
# 'Café périferôl'.mb_chars.index('ô') #=> 12
# 'Café périferôl'.mb_chars.index(/\w/u) #=> 0
def index(needle, offset=0)
- index = @wrapped_string.index(needle, offset)
+ wrapped_offset = self.first(offset).wrapped_string.length
+ index = @wrapped_string.index(needle, wrapped_offset)
index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil
end
@@ -215,11 +216,12 @@ module ActiveSupport #:nodoc:
# string. Returns +nil+ if _needle_ isn't found.
#
# Example:
- # 'Café périferôl'.mb_chars.rindex('é') #=> 5
+ # 'Café périferôl'.mb_chars.rindex('é') #=> 6
# 'Café périferôl'.mb_chars.rindex(/\w/u) #=> 13
def rindex(needle, offset=nil)
offset ||= length
- index = @wrapped_string.rindex(needle, offset)
+ wrapped_offset = self.first(offset).wrapped_string.length
+ index = @wrapped_string.rindex(needle, wrapped_offset)
index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil
end
diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb
index f3c7f50458..ed37a1a0da 100644
--- a/activesupport/test/multibyte_chars_test.rb
+++ b/activesupport/test/multibyte_chars_test.rb
@@ -230,14 +230,19 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
assert_nil @chars.index('u')
assert_equal 0, @chars.index('こに')
assert_equal 2, @chars.index('ち')
+ assert_equal 2, @chars.index('ち', -2)
+ assert_equal nil, @chars.index('ち', -1)
assert_equal 3, @chars.index('わ')
+ assert_equal 5, 'ééxééx'.mb_chars.index('x', 4)
end
def test_rindex_should_return_character_offset
assert_nil @chars.rindex('u')
assert_equal 1, @chars.rindex('に')
+ assert_equal 2, @chars.rindex('ち', -2)
+ assert_nil @chars.rindex('ち', -3)
assert_equal 6, 'Café périferôl'.mb_chars.rindex('é')
- assert_equal 12, 'Café périferôl'.mb_chars.rindex(/\w/u)
+ assert_equal 13, 'Café périferôl'.mb_chars.rindex(/\w/u)
end
def test_indexed_insert_should_take_character_offsets