Skip to content

Commit 8064958

Browse files
ClearlyClairehiyuki2578
authored andcommitted
Minor performance improvements and cleanup in formatter (mastodon#10765)
1 parent 980f72f commit 8064958

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

app/lib/formatter.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def nyaize(html)
193193
end
194194

195195
def rewrite(text, entities)
196-
chars = text.to_s.to_char_a
196+
text = text.to_s
197197

198198
# Sort by start index
199199
entities = entities.sort_by do |entity|
@@ -205,12 +205,12 @@ def rewrite(text, entities)
205205

206206
last_index = entities.reduce(0) do |index, entity|
207207
indices = entity.respond_to?(:indices) ? entity.indices : entity[:indices]
208-
result << encode(chars[index...indices.first].join)
208+
result << encode(text[index...indices.first])
209209
result << yield(entity)
210210
indices.last
211211
end
212212

213-
result << encode(chars[last_index..-1].join)
213+
result << encode(text[last_index..-1])
214214

215215
result.flatten.join
216216
end
@@ -237,23 +237,14 @@ def utf8_friendly_extractor(text, options = {})
237237
# Note: I couldn't obtain list_slug with @user/list-name format
238238
# for mention so this requires additional check
239239
special = Extractor.extract_urls_with_indices(escaped, options).map do |extract|
240-
# exactly one of :url, :hashtag, :screen_name, :cashtag keys is present
241-
key = (extract.keys & [:url, :hashtag, :screen_name, :cashtag]).first
242-
243240
new_indices = [
244241
old_to_new_index.find_index(extract[:indices].first),
245242
old_to_new_index.find_index(extract[:indices].last),
246243
]
247244

248-
has_prefix_char = [:hashtag, :screen_name, :cashtag].include?(key)
249-
value_indices = [
250-
new_indices.first + (has_prefix_char ? 1 : 0), # account for #, @ or $
251-
new_indices.last - 1,
252-
]
253-
254245
next extract.merge(
255-
:indices => new_indices,
256-
key => text[value_indices.first..value_indices.last]
246+
indices: new_indices,
247+
url: text[new_indices.first..new_indices.last - 1]
257248
)
258249
end
259250

0 commit comments

Comments
 (0)