From aecf5ececf69d9d37a46e70b339fc6fff7526e3e Mon Sep 17 00:00:00 2001 From: Erik Ostrom Date: Sun, 9 Aug 2009 18:57:25 -0700 Subject: [PATCH] Add rindex to ActiveSupport::Multibyte::Chars. --- .../lib/active_support/multibyte/chars.rb | 13 +++++++++++++ activesupport/test/multibyte_chars_test.rb | 7 +++++++ 2 files changed, 20 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 96ed35f..3c16999 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -210,6 +210,19 @@ module ActiveSupport #:nodoc: index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil end + # Returns the position _needle_ in the string, counting in + # codepoints, searching backward from _offset_ or the end of the + # string. Returns +nil+ if _needle_ isn't found. + # + # Example: + # 'Café périferôl'.mb_chars.rindex('é') #=> 5 + # 'Café périferôl'.mb_chars.rindex(/\w/u) #=> 13 + def rindex(needle, offset=nil) + offset ||= length + index = @wrapped_string.rindex(needle, offset) + index ? (self.class.u_unpack(@wrapped_string.slice(0...index)).size) : nil + end + # Like String#[]=, except instead of byte offsets you specify character offsets. # # Example: diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 661b33c..2b30847 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -234,6 +234,13 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase assert_equal 3, @chars.index('わ') end + def test_rindex_should_return_character_offset + assert_nil @chars.rindex('u') + assert_equal 1, @chars.rindex('に') + assert_equal 6, 'Café périferôl'.mb_chars.rindex('é') + assert_equal 12, 'Café périferôl'.mb_chars.rindex(/\w/u) + end + def test_indexed_insert_should_take_character_offsets @chars[2] = 'a' assert_equal 'こにaわ', @chars -- 1.6.1.2