aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-01-05 09:23:16 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-01-05 09:23:16 -0800
commitf4ef09c154fa1337c365541920d8f69604dc21b1 (patch)
treee54aff5c5218cf72d66f7ea87ea6dee7ca96c7d5 /activesupport
parent351a6009c5a31a86e6a2afb18a84c4f80fbf11f4 (diff)
parent5a7513593f64e0ff7e4de1ee37bac5eeddfae270 (diff)
downloadrails-f4ef09c154fa1337c365541920d8f69604dc21b1.tar.gz
rails-f4ef09c154fa1337c365541920d8f69604dc21b1.tar.bz2
rails-f4ef09c154fa1337c365541920d8f69604dc21b1.zip
Merge pull request #4304 from lest/refactor-truncate
refactor String#truncate not to use mb_chars
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/string/filters.rb11
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