Sha256: c731f8b4463e28262263a75312de2026ce9d711659290b655332182a912c9174

Contents?: true

Size: 876 Bytes

Versions: 1

Compression:

Stored size: 876 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  # @api private
  module OptimizedPatterns
    # A wrapper around patterns array to perform optimized search.
    # @api private
    class PatternsSet
      def initialize(patterns)
        @strings = Set.new
        @patterns = []
        partition_patterns(patterns)
      end

      def match?(path)
        @strings.include?(path) || @patterns.any? { |pattern| PathUtil.match_path?(pattern, path) }
      end

      private

      def partition_patterns(patterns)
        patterns.each do |pattern|
          if pattern.is_a?(String) && !pattern.match?(/[*{\[?]/)
            @strings << pattern
          else
            @patterns << pattern
          end
        end
      end
    end

    @cache = {}.compare_by_identity

    def self.from(patterns)
      @cache[patterns] ||= PatternsSet.new(patterns)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.40.0 lib/rubocop/optimized_patterns.rb