diff options
author | Erik Ostrom <erik@echographia.com> | 2009-08-09 18:57:25 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-08-09 19:05:19 -0700 |
commit | 4dda9b644df5e4386f693a4b7bd00fe787f41a28 (patch) | |
tree | 49604711f2136314baccaa00a60027556f8f32e1 /activesupport/lib/active_support/multibyte | |
parent | e06a0b03c8ba29f4b05a35560645814ac88aefbe (diff) | |
download | rails-4dda9b644df5e4386f693a4b7bd00fe787f41a28.tar.gz rails-4dda9b644df5e4386f693a4b7bd00fe787f41a28.tar.bz2 rails-4dda9b644df5e4386f693a4b7bd00fe787f41a28.zip |
Add rindex to ActiveSupport::Multibyte::Chars.
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib/active_support/multibyte')
-rw-r--r-- | activesupport/lib/active_support/multibyte/chars.rb | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 96ed35f0e0..3c16999e43 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 <tt>String#[]=</tt>, except instead of byte offsets you specify character offsets. # # Example: |