Skip to content

Commit 057b0bb

Browse files
committed
Improve scan of regexp handling
1 parent 9456e79 commit 057b0bb

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

lib/rdoc/markup/formatter.rb

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,28 @@ def convert(content)
9494
# Applies regexp handling to +text+ and returns an array of [text, converted?] pairs.
9595

9696
def apply_regexp_handling(text)
97-
output = []
98-
start = 0
99-
loop do
100-
pos = text.size
101-
matched_name = matched_text = nil
102-
@markup.regexp_handlings.each do |pattern, name|
103-
m = text.match(pattern, start)
104-
next unless m
97+
matched = []
98+
@markup.regexp_handlings.each do |pattern, name|
99+
text.scan(pattern) do
100+
m = Regexp.last_match
105101
idx = m[1] ? 1 : 0
106-
if m.begin(idx) < pos
107-
pos = m.begin(idx)
108-
matched_text = m[idx]
109-
matched_name = name
110-
end
111-
end
112-
output << [text[start...pos], false] if pos > start
113-
if matched_name
114-
handled = public_send(:"handle_regexp_#{matched_name}", matched_text)
115-
output << [handled, true]
116-
start = pos + matched_text.size
117-
else
118-
start = pos
102+
matched << [m.begin(idx), m.end(idx), m[idx], name]
119103
end
120-
break if pos == text.size
121104
end
105+
106+
chars = text.chars
107+
pos = 0
108+
output = []
109+
matched.sort_by(&:first).each do |beg_pos, end_pos, s, name|
110+
next if beg_pos < pos
111+
112+
output << [chars[pos...beg_pos].join, false] if beg_pos != pos
113+
handled = public_send(:"handle_regexp_#{name}", s)
114+
output << [handled, true]
115+
pos = end_pos
116+
end
117+
118+
output << [chars[pos..].join, false] if pos < chars.size
122119
output
123120
end
124121

0 commit comments

Comments
 (0)