Skip to content
This repository
Browse code

refactor String#truncate not to use mb_chars

  • Loading branch information...
commit 5a7513593f64e0ff7e4de1ee37bac5eeddfae270 1 parent 33386b0
Sergey Nartimov authored January 04, 2012
11  activesupport/lib/active_support/core_ext/string/filters.rb
@@ -36,14 +36,13 @@ def squish!
36 36
   #   "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
37 37
   #   # => "And they f... (continued)"
38 38
   def truncate(length, options = {})
39  
-    text = self.dup
40  
-    options[:omission] ||= "..."
  39
+    return self.dup unless self.length > length
41 40
 
42  
-    length_with_room_for_omission = length - options[:omission].mb_chars.length
43  
-    chars = text.mb_chars
  41
+    options[:omission] ||= "..."
  42
+    length_with_room_for_omission = length - options[:omission].length
44 43
     stop = options[:separator] ?
45  
-      (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
  44
+      (rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
46 45
 
47  
-    (chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
  46
+    self[0...stop] + options[:omission]
48 47
   end
49 48
 end

0 notes on commit 5a75135

Please sign in to comment.
Something went wrong with that request. Please try again.