Skip to content

Commit a40d3de

Browse files
committed
Improve perf of ActionView::Helpers::TextHelper#excerpt for large strings.
1 parent b39ea7c commit a40d3de

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

actionview/lib/action_view/helpers/text_helper.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "active_support/core_ext/string/filters"
4+
require "active_support/core_ext/string/access"
45
require "active_support/core_ext/array/extract_options"
56

67
module ActionView
@@ -467,18 +468,25 @@ def cut_excerpt_part(part_position, part, separator, options)
467468
radius = options.fetch(:radius, 100)
468469
omission = options.fetch(:omission, "...")
469470

470-
part = part.split(separator)
471-
part.delete("")
472-
affix = part.size > radius ? omission : ""
471+
if separator != ""
472+
part = part.split(separator)
473+
part.delete("")
474+
end
473475

474-
part = if part_position == :first
475-
drop_index = [part.length - radius, 0].max
476-
part.drop(drop_index)
477-
else
478-
part.first(radius)
476+
affix = part.length > radius ? omission : ""
477+
478+
part =
479+
if part_position == :first
480+
part.last(radius)
481+
else
482+
part.first(radius)
483+
end
484+
485+
if separator != ""
486+
part = part.join(separator)
479487
end
480488

481-
return affix, part.join(separator)
489+
return affix, part
482490
end
483491
end
484492
end

0 commit comments

Comments
 (0)