Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor String#truncate not to use mb_chars #4304

Merged
merged 1 commit into from Jan 5, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions activesupport/lib/active_support/core_ext/string/filters.rb
Expand Up @@ -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