<commit_before>#
# This file was ported to ruby from Composer php source code.
# Original Source: Composer\Repository\ArrayRepository.php
#
# (c) <NAME> <<EMAIL>>
#     <NAME> <<EMAIL>>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#

module Composer
  module Repository
    class HashRepository < Composer::Repository::BaseRepository
      def initialize(packages = [])
        packages.each do |package|
          add_package(package)
        end
      end

      def find_package(name, version = nil)
        # normalize name
        name = name.downcase

        # normalize version
        if version != nil
          version_parser = Composer::Package::Version::VersionParser.new
          version = version_parser.normalize(version)
        end

        packages.each do |package|
          if package.name === name && (nil === version || version === package.version)
            return package
          end
        end

      end

      def find_packages(name, version = nil)
          # normalize name
          name = name.downcase

          # normalize version
          if version != nil
            version_parser = Composer::Package::Version::VersionParser.new
            version = version_parser.normalize(version)
          end

          matches = []
          packages.each do |package|
            if package.name === name && (nil === version || version === package.version)
              matches << package
            end
          end
          matches
      end

      def search(query, full_search = false)
        regex = /(?:#{query.split(/\s+/).join('|')})/i
        matches = {}
        packages.each do |package|
          name = package.name

          # already matched
          next if matches['name']

          # search
          if full_search
            next unless (
              package.instance_of?(Composer::Package::CompletePackage) &&
              regex.match("#{package.keywords.join(' ')} #{package.description}")
            )
          else
            next unless (
              full_search == false &&
              regex.match(name)
            )
          end

          matches[name] = {
            'name' => package.pretty_name,
            'description' => package.description,
          }
        end
        matches
      end

      def package?(package)
        unless package
          raise ArgumentError,
                'package must be specified'
        end
        unless package.is_a?(Composer::Package::BasePackage)
          raise TypeError,
                'package must be a class or superclass of \
                Composer::Package::Package'
        end

        package_id = package.unique_name
        packages.each do |repo_package|
          return true if repo_package.unique_name === package_id
        end

        false
      end

      # Adds a new package to the repository
      #
      # Params:
      # +package+ Package The package to add
      def add_package(package)
        unless package<commit_msg>fixes in find_package fixes in find_packages fixes in search<commit_after>#
# This file was ported to ruby from Composer php source code.
# Original Source: Composer\Repository\ArrayRepository.php
#
# (c) <NAME> <<EMAIL>>
#     <NAME> <<EMAIL>>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#

module Composer
  module Repository
    class HashRepository < Composer::Repository::BaseRepository
      def initialize(packages = [])
        packages.each do |package|
          add_package(package)
        end
      end

      def find_package(name, version = nil)
        # normalize name
        name = name.downcase

        # normalize version
        if !version.nil?
          version_parser = Composer::Package::Version::VersionParser.new
          version = version_parser.normalize(version)
        end

        match = nil
        packages.each do |package|
          if package.name === name
            if version.nil? || package.version === version
              match = package
              break
            end
          end
        end
        match
      end

      def find_packages(name, version = nil)
          # normalize name
          name = name.downcase

          # normalize version
          if version != nil
            version_parser = Composer::Package::Version::VersionParser.new
            version = version_parser.normalize(version)
          end

          matches = []
          packages.each do |package|
            if package.name === name && (nil === version || version === package.version)
              matches.push(package)
            end
          end
          matches
      end

      def search(query, mode = 0)
        regex = /(?:#{query.split(/\s+/).join('|')})/i
        matches = {}
        packages.each do |package|
          name = package.name

          # already matched
          next if matches[name]

          # search
          unless regex.match(name)
            unless mode === Composer::Repository::BaseRepository::SEARCH_FULLTEXT &&
                package.instance_of?(Composer::Package::CompletePackage) &&
                regex.match("#{package.keywords ? package.keywords.join(' ') : ''} #{package.description ? package.description : ''}")
              next
            end
          end

          matches[name] = {
            'name' => package.pretty_name,
            'description' => package.description,
          }

        end
        matches.values
      end

      def package?(package)
        unless package
          raise ArgumentError,
                'package must be specified'
        end
        unless package.is_a?(Composer::Package::BasePackage)
          raise TypeError,
                'package must be a class or superclass of \
                Composer::Package::Package'
        end

        package_id = package.unique_name
        packages.each do |repo_package|
          return true if repo_package.unique_name === package_id
        end

        false
      end

      # Adds a new package to the repository
      #
      # Params:
      # +package+ Package The package to add
      def add_package(package)
        unless package
{'fraction_non_alphanumeric': 0.0890617558342594, 'fraction_numerical': 0.00015875535799333228, 'mean_word_length': 1.7450980392156863, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2878197', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1537, 'n_words': 445}