Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #4304 from lest/refactor-truncate

refactor String#truncate not to use mb_chars
  • Loading branch information...
commit f4ef09c154fa1337c365541920d8f69604dc21b1 2 parents 351a600 + 5a75135
@tenderlove tenderlove authored
Showing with 5 additions and 6 deletions.
  1. +5 −6 activesupport/lib/active_support/core_ext/string/filters.rb
View
11 activesupport/lib/active_support/core_ext/string/filters.rb
@@ -36,14 +36,13 @@ def squish!
# "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
# # => "And they f... (continued)"
def truncate(length, options = {})
- text = self.dup
- options[:omission] ||= "..."
+ return self.dup unless self.length > length
- length_with_room_for_omission = length - options[:omission].mb_chars.length
- chars = text.mb_chars
+ options[:omission] ||= "..."
+ length_with_room_for_omission = length - options[:omission].length
stop = options[:separator] ?
- (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
+ (rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
- (chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
+ self[0...stop] + options[:omission]
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.