Minor performance improvements and cleanup in formatter (#10765)
This commit is contained in:
parent
9e95af3391
commit
ee17d81b8a
@ -187,7 +187,7 @@ class Formatter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def rewrite(text, entities)
|
def rewrite(text, entities)
|
||||||
chars = text.to_s.to_char_a
|
text = text.to_s
|
||||||
|
|
||||||
# Sort by start index
|
# Sort by start index
|
||||||
entities = entities.sort_by do |entity|
|
entities = entities.sort_by do |entity|
|
||||||
@ -199,12 +199,12 @@ class Formatter
|
|||||||
|
|
||||||
last_index = entities.reduce(0) do |index, entity|
|
last_index = entities.reduce(0) do |index, entity|
|
||||||
indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
|
indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
|
||||||
result << encode(chars[index...indices.first].join)
|
result << encode(text[index...indices.first])
|
||||||
result << yield(entity)
|
result << yield(entity)
|
||||||
indices.last
|
indices.last
|
||||||
end
|
end
|
||||||
|
|
||||||
result << encode(chars[last_index..-1].join)
|
result << encode(text[last_index..-1])
|
||||||
|
|
||||||
result.flatten.join
|
result.flatten.join
|
||||||
end
|
end
|
||||||
@ -231,23 +231,14 @@ class Formatter
|
|||||||
# Note: I couldn't obtain list_slug with @user/list-name format
|
# Note: I couldn't obtain list_slug with @user/list-name format
|
||||||
# for mention so this requires additional check
|
# for mention so this requires additional check
|
||||||
special = Extractor.extract_urls_with_indices(escaped, options).map do |extract|
|
special = Extractor.extract_urls_with_indices(escaped, options).map do |extract|
|
||||||
# exactly one of :url, :hashtag, :screen_name, :cashtag keys is present
|
|
||||||
key = (extract.keys & [:url, :hashtag, :screen_name, :cashtag]).first
|
|
||||||
|
|
||||||
new_indices = [
|
new_indices = [
|
||||||
old_to_new_index.find_index(extract[:indices].first),
|
old_to_new_index.find_index(extract[:indices].first),
|
||||||
old_to_new_index.find_index(extract[:indices].last),
|
old_to_new_index.find_index(extract[:indices].last),
|
||||||
]
|
]
|
||||||
|
|
||||||
has_prefix_char = [:hashtag, :screen_name, :cashtag].include?(key)
|
|
||||||
value_indices = [
|
|
||||||
new_indices.first + (has_prefix_char ? 1 : 0), # account for #, @ or $
|
|
||||||
new_indices.last - 1,
|
|
||||||
]
|
|
||||||
|
|
||||||
next extract.merge(
|
next extract.merge(
|
||||||
:indices => new_indices,
|
indices: new_indices,
|
||||||
key => text[value_indices.first..value_indices.last]
|
url: text[new_indices.first..new_indices.last - 1]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user