class AnsiFormatter

def scan_and_process_multiple_substrings(str, substrings, plain_sym, color_sym)

Returns:
  • (Array) - The processed segments.

Parameters:
  • color_sym (Symbol) -- The symbol for matching segments.
  • plain_sym (Symbol) -- The symbol for non-matching segments.
  • substrings (Array) -- The substrings to match in the string.
  • str (String) -- The string to scan.
def scan_and_process_multiple_substrings(str, substrings, plain_sym, color_sym)
  return string_send_color(str, plain_sym) if substrings.empty? || substrings.any?(&:empty?)
  substring_patterns = substrings.map do |value|
    [value, Regexp.new(value, Regexp::IGNORECASE)]
  end
  results = []
  remaining_str = str.dup
  while remaining_str.length.positive?
    match_indices = substring_patterns.map { |_, pattern| remaining_str.index(pattern) }.compact
    earliest_match = match_indices.min
    if earliest_match
      # Process non-matching segment before the earliest match, if any
      unless earliest_match.zero?
        non_matching_segment = remaining_str.slice!(0...earliest_match)
        results << string_send_color(non_matching_segment, plain_sym)
      end
      # Find which substring has this earliest match
      matching_substring = substring_patterns.find do |_, pattern|
        remaining_str.index(pattern) == earliest_match
      end
      if matching_substring
        matching_segment = remaining_str.slice!(0...matching_substring[0].length)
        results << string_send_color(matching_segment, color_sym)
      end
    else
      # Process the remaining non-matching segment
      results << string_send_color(remaining_str, plain_sym)
      break
    end
  end
  results
end