aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/multibyte
diff options
context:
space:
mode:
authorErik Ostrom <erik@echographia.com>2009-08-09 22:14:59 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-08-09 23:30:41 -0700
commit7d16e94d827197d030fdd611db4310396aeb0114 (patch)
tree6cdade25d435e896ea030b45831f56cdd595a6c8 /activesupport/lib/active_support/multibyte
parent3e35ba248154209d9c46e3d9fa78e98c1b5d0a96 (diff)
downloadrails-7d16e94d827197d030fdd611db4310396aeb0114.tar.gz
rails-7d16e94d827197d030fdd611db4310396aeb0114.tar.bz2
rails-7d16e94d827197d030fdd611db4310396aeb0114.zip
Correctly handle offsets in Multibyte::Chars#index and #rindex.
The offset in codepoints was being passed directly to the wrapped string's index/rindex method. Now we translate the offset into bytes first. [#3028 state:committed] 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.rb8
1 files changed, 5 insertions, 3 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