<commit_before># frozen_string_literal: true
module Sprockets
  # Internal: File and path related utilities. Mixed into Environment.
  #
  # Probably would be called FileUtils, but that causes namespace annoyances
  # when code actually wants to reference ::FileUtils.
  module PathUtils
    extend self
    require 'pathname'

    # Public: Like `File.stat`.
    #
    # path - String file or directory path
    #
    # Returns nil if the file does not exist.
    def stat(path)
      if File.exist?(path)
        File.stat(path.to_s)
      else
        nil
      end
    end

    # Public: Like `File.file?`.
    #
    # path - String file path.
    #
    # Returns true path exists and is a file.
    def file?(path)
      if stat = self.stat(path)
        stat.file?
      else
        false
      end
    end

    # Public: Like `File.directory?`.
    #
    # path - String file path.
    #
    # Returns true path exists and is a directory.
    def directory?(path)
      if stat = self.stat(path)
        stat.directory?
      else
        false
      end
    end

    # Public: A version of `Dir.entries` that filters out `.` files and `~`
    # swap files.
    #
    # path - String directory path
    #
    # Returns an empty `Array` if the directory does not exist.
    def entries(path)
      if File.directory?(path)
        entries = Dir.entries(path, encoding: Encoding.default_internal)
        entries.reject! { |entry|
          entry.start_with?(".".freeze) ||
            (entry.start_with?("#".freeze) && entry.end_with?("#".freeze)) ||
            entry.end_with?("~".freeze)
        }
        entries.sort!
        entries
      else
        []
      end
    end

    # Public: Check if path is absolute or relative.
    #
    # path - String path.
    #
    # Returns true if path is absolute, otherwise false.
    if File::ALT_SEPARATOR
      # On Windows, ALT_SEPARATOR is \
      # Delegate to Pathname since the logic gets complex.
      def absolute_path?(path)
        Pathname.new(path).absolute?
      end
    else
      def absolute_path?(path)
        path.start_with?(File::SEPARATOR)
      end
    end

    if File::ALT_SEPARATOR
      SEPARATOR_PATTERN = "#{Regexp.quote(File::SEPARATOR)}|#{Regexp.quote(File::ALT_SEPARATOR)}"
    else
      SEPARATOR_PATTERN = "#{Regexp.quote(File::SEPARATOR)}"
    end

    # Public: Check if path is explicitly relative.
    # Starts with "./" or "../".
    #
    # path - String path.
    #
    # Returns true if path is relative, otherwise false.
    def relative_path?(path)
      path.match?(/^\.\.?($|#{SEPARATOR_PATTERN})/) ? true : false
    end

    # Public: Get relative path from `start` to `dest`.
    #
    # start - String start path (file or dir)
    # dest  - String destination path
    #
    # Returns relative String path from `start` to `dest`
    def relative_path_from(start, dest)
      start, dest = Pathname.new(start), Pathname.new(dest)
      start = start.dirname unless start.directory?
      dest.relative_path_from(start).to_s
    end

    # Public: Joins path to base path.
    #
    # base - Root path
    # path - Extending path
    #
    # Example
    #
    #     join('base/path/', '../file.js')
    #     # => 'base/file.js'
    #
    # Returns string path starting from base and ending at path
    def join(base, path)
      (Pathname.new(base) + path).to_s
    end

    # Public: Sets pipeline for path
    #
    # path       - String path
    # extensions - List of file extensions
    # pipeline   - Pipeline
    #
    # Examples
    #
    #     set_pipeline('path/file.js.erb', config[:mime_exts], config[:pipeline_exts], :source)
    #     # => 'path/file.source.js.erb'
    #
    #     set_pipeline('path/some.file.source.js.erb', config[:mime_exts], config[:pipeline_exts], :debug)
    #     # => 'path/some.file.debug.js.erb'
    #
    # Returns string path with pipeline parsed in
    def set_pipeline(path, mime_exts, pipeline_exts, pipeline)
      extension, _ = match_path_extname(path, mime_exts)
      path.chomp!(extension)
      pipeline_old, _ = match_path_extname(path, pipeline_exts)
      path.chomp!(pipeline_old)

      "#{path}.#{pipeline}#{extension}"
    end

    # Internal: Get relative path for root path and subpath.
    #
    # path    - String path
    # subpath - String subpath of path
    #
    # Returns relative String path if subpath is a subpath of path, or nil if
    # subpath is outside of path.
    def split_subpath(path, subpath)
      return "" if path == subpath
      path = File.join(path, ''.freeze)
      if subpath.start_with?(path)
        subpath[path.length..-1]
      else
        nil
      end
    end

    # Internal: Detect root path and base for file in a set of paths.
    #
    # paths    - Array of String paths
    # filename - String path of file expected to be in one of the paths.
    #
    # Returns [String root, String path]
    def paths_split(paths, filename)
      paths.each do |path|
        if subpath = split_subpath(path, filename)
          return path, subpath
        end
      end<commit_msg>check if subpath exists to get rid off '.rvm/gems/ruby-2.7.0/gems/sprockets-4.0.1/lib/sprockets/path_utils.rb:165:in : undefined method  for nil:NilClass (NoMethodError)' error<commit_after># frozen_string_literal: true
module Sprockets
  # Internal: File and path related utilities. Mixed into Environment.
  #
  # Probably would be called FileUtils, but that causes namespace annoyances
  # when code actually wants to reference ::FileUtils.
  module PathUtils
    extend self
    require 'pathname'

    # Public: Like `File.stat`.
    #
    # path - String file or directory path
    #
    # Returns nil if the file does not exist.
    def stat(path)
      if File.exist?(path)
        File.stat(path.to_s)
      else
        nil
      end
    end

    # Public: Like `File.file?`.
    #
    # path - String file path.
    #
    # Returns true path exists and is a file.
    def file?(path)
      if stat = self.stat(path)
        stat.file?
      else
        false
      end
    end

    # Public: Like `File.directory?`.
    #
    # path - String file path.
    #
    # Returns true path exists and is a directory.
    def directory?(path)
      if stat = self.stat(path)
        stat.directory?
      else
        false
      end
    end

    # Public: A version of `Dir.entries` that filters out `.` files and `~`
    # swap files.
    #
    # path - String directory path
    #
    # Returns an empty `Array` if the directory does not exist.
    def entries(path)
      if File.directory?(path)
        entries = Dir.entries(path, encoding: Encoding.default_internal)
        entries.reject! { |entry|
          entry.start_with?(".".freeze) ||
            (entry.start_with?("#".freeze) && entry.end_with?("#".freeze)) ||
            entry.end_with?("~".freeze)
        }
        entries.sort!
        entries
      else
        []
      end
    end

    # Public: Check if path is absolute or relative.
    #
    # path - String path.
    #
    # Returns true if path is absolute, otherwise false.
    if File::ALT_SEPARATOR
      # On Windows, ALT_SEPARATOR is \
      # Delegate to Pathname since the logic gets complex.
      def absolute_path?(path)
        Pathname.new(path).absolute?
      end
    else
      def absolute_path?(path)
        path.start_with?(File::SEPARATOR)
      end
    end

    if File::ALT_SEPARATOR
      SEPARATOR_PATTERN = "#{Regexp.quote(File::SEPARATOR)}|#{Regexp.quote(File::ALT_SEPARATOR)}"
    else
      SEPARATOR_PATTERN = "#{Regexp.quote(File::SEPARATOR)}"
    end

    # Public: Check if path is explicitly relative.
    # Starts with "./" or "../".
    #
    # path - String path.
    #
    # Returns true if path is relative, otherwise false.
    def relative_path?(path)
      path.match?(/^\.\.?($|#{SEPARATOR_PATTERN})/) ? true : false
    end

    # Public: Get relative path from `start` to `dest`.
    #
    # start - String start path (file or dir)
    # dest  - String destination path
    #
    # Returns relative String path from `start` to `dest`
    def relative_path_from(start, dest)
      start, dest = Pathname.new(start), Pathname.new(dest)
      start = start.dirname unless start.directory?
      dest.relative_path_from(start).to_s
    end

    # Public: Joins path to base path.
    #
    # base - Root path
    # path - Extending path
    #
    # Example
    #
    #     join('base/path/', '../file.js')
    #     # => 'base/file.js'
    #
    # Returns string path starting from base and ending at path
    def join(base, path)
      (Pathname.new(base) + path).to_s
    end

    # Public: Sets pipeline for path
    #
    # path       - String path
    # extensions - List of file extensions
    # pipeline   - Pipeline
    #
    # Examples
    #
    #     set_pipeline('path/file.js.erb', config[:mime_exts], config[:pipeline_exts], :source)
    #     # => 'path/file.source.js.erb'
    #
    #     set_pipeline('path/some.file.source.js.erb', config[:mime_exts], config[:pipeline_exts], :debug)
    #     # => 'path/some.file.debug.js.erb'
    #
    # Returns string path with pipeline parsed in
    def set_pipeline(path, mime_exts, pipeline_exts, pipeline)
      extension, _ = match_path_extname(path, mime_exts)
      path.chomp!(extension)
      pipeline_old, _ = match_path_extname(path, pipeline_exts)
      path.chomp!(pipeline_old)

      "#{path}.#{pipeline}#{extension}"
    end

    # Internal: Get relative path for root path and subpath.
    #
    # path    - String path
    # subpath - String subpath of path
    #
    # Returns relative String path if subpath is a subpath of path, or nil if
    # subpath is outside of path.
    def split_subpath(path, subpath)
      return "" if path == subpath
      path = File.join(path, ''.freeze)
      if subpath and subpath.start_with?(path)
        subpath[path.length..-1]
      else
        nil
      end
    end

    # Internal: Detect root path and base for file in a set of paths.
    #
    # paths    - Array of String paths
    # filename - String path of file expected to be in one of the paths.
    #
    # Returns [String root, String path]
    def paths_split(paths, filename)
      paths.each do |path|
        if subpath = split_subpath(path, filename)
          return path, subpath
        end
      end
{'fraction_non_alphanumeric': 0.10681488641855968, 'fraction_numerical': 0.0010633156114064765, 'mean_word_length': 2.169730392156863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3279799', 'n_tokens_mistral': 3241, 'n_tokens_neox': 3229, 'n_words': 935}