From 441de5dcae646397f71745b455e5c16f77d27e4c Mon Sep 17 00:00:00 2001 From: tompng Date: Tue, 20 Jan 2026 04:13:13 +0900 Subject: [PATCH 1/2] Improve scan of regexp handling --- lib/rdoc/markup/formatter.rb | 42 +++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/rdoc/markup/formatter.rb b/lib/rdoc/markup/formatter.rb index 0532fae5fc..98de6e450f 100644 --- a/lib/rdoc/markup/formatter.rb +++ b/lib/rdoc/markup/formatter.rb @@ -85,31 +85,29 @@ def convert(content) # Applies regexp handling to +text+ and returns an array of [text, converted?] pairs. def apply_regexp_handling(text) - output = [] - start = 0 - loop do - pos = text.size - matched_name = matched_text = nil - @markup.regexp_handlings.each do |pattern, name| - m = text.match(pattern, start) - next unless m + matched = [] + @markup.regexp_handlings.each_with_index do |(pattern, name), priority| + text.scan(pattern) do + m = Regexp.last_match idx = m[1] ? 1 : 0 - if m.begin(idx) < pos - pos = m.begin(idx) - matched_text = m[idx] - matched_name = name - end - end - output << [text[start...pos], false] if pos > start - if matched_name - handled = public_send(:"handle_regexp_#{matched_name}", matched_text) - output << [handled, true] - start = pos + matched_text.size - else - start = pos + matched << [m.begin(idx), m.end(idx), m[idx], name, priority] end - break if pos == text.size end + # If the start positions are the same, prefer the one with higher priority (registered earlier one) + matched.sort_by! {|beg_pos, _, _, _, priority| [beg_pos, priority] } + + pos = 0 + output = [] + matched.each do |beg_pos, end_pos, s, name| + next if beg_pos < pos + + output << [text[pos...beg_pos], false] if beg_pos != pos + handled = public_send(:"handle_regexp_#{name}", s) + output << [handled, true] + pos = end_pos + end + + output << [text[pos..], false] if pos < text.size output end From 5561522de253ac85ccf925493fe63a3e2db70a8c Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Wed, 13 May 2026 14:50:31 +0900 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- lib/rdoc/markup/formatter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rdoc/markup/formatter.rb b/lib/rdoc/markup/formatter.rb index 98de6e450f..8e191d6c19 100644 --- a/lib/rdoc/markup/formatter.rb +++ b/lib/rdoc/markup/formatter.rb @@ -93,7 +93,8 @@ def apply_regexp_handling(text) matched << [m.begin(idx), m.end(idx), m[idx], name, priority] end end - # If the start positions are the same, prefer the one with higher priority (registered earlier one) + # If the start positions are the same, prefer the earlier-registered one + # (lower numeric priority from each_with_index). matched.sort_by! {|beg_pos, _, _, _, priority| [beg_pos, priority] } pos = 0