diff options
author | Sergey Nartimov <just.lest@gmail.com> | 2012-01-04 22:30:07 +0300 |
---|---|---|
committer | Sergey Nartimov <just.lest@gmail.com> | 2012-01-04 22:30:07 +0300 |
commit | 5a7513593f64e0ff7e4de1ee37bac5eeddfae270 (patch) | |
tree | fd3851da4b8db26d76850f659c9f7737520a5295 /activesupport/lib/active_support | |
parent | 33386b065c8dc029f0c5f31188fdc4c12e1d7293 (diff) | |
download | rails-5a7513593f64e0ff7e4de1ee37bac5eeddfae270.tar.gz rails-5a7513593f64e0ff7e4de1ee37bac5eeddfae270.tar.bz2 rails-5a7513593f64e0ff7e4de1ee37bac5eeddfae270.zip |
refactor String#truncate not to use mb_chars
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/filters.rb | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index d478ee0ef6..1a34e88a87 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -36,14 +36,13 @@ class String # "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 |