method
translate_offset
v2.2.1 -
Show latest stable
- Class:
ActiveSupport::Multibyte::Chars
translate_offset(byte_offset)protected
No documentation available.
# File activesupport/lib/active_support/multibyte/chars.rb, line 626
def translate_offset(byte_offset) #:nodoc:
return nil if byte_offset.nil?
return 0 if @wrapped_string == ''
chunk = @wrapped_string[0..byte_offset]
begin
begin
chunk.unpack('U*').length - 1
rescue ArgumentError => e
chunk = @wrapped_string[0..(byte_offset+=1)]
# Stop retrying at the end of the string
raise e unless byte_offset < chunk.length
# We damaged a character, retry
retry
end
# Catch the ArgumentError so we can throw our own
rescue ArgumentError
raise EncodingError, 'malformed UTF-8 character'
end
end