<commit_before># encoding: utf-8
# rubocop:disable Metrics/ModuleLength

module RuboCop
  module Cop
    # This module contains a collection of useful utility methods.
    module Util
      include PathUtil
      extend Astrolabe::Sexp

      EQUALS_ASGN_NODES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn, :masgn]
      SHORTHAND_ASGN_NODES = [:op_asgn, :or_asgn, :and_asgn]
      ASGN_NODES = EQUALS_ASGN_NODES + SHORTHAND_ASGN_NODES

      # http://phrogz.net/programmingruby/language.html#table_18.4
      # Backtick is added last just to help editors parse this code.
      OPERATOR_METHODS = %w(
        | ^ & <=> == === =~ > >= < <= << >>
        + - * / % ** ~ +@ -@ [] []= ! != !~
      ).map(&:to_sym) + [:'`']

      STRING_ESCAPES = {
        '\a' => "\a", '\b' => "\b", '\e' => "\e", '\f' => "\f", '\n' => "\n",
        '\r' => "\r", '\s' => ' ',  '\t' => "\t", '\v' => "\v", "\\\n" => ''
      }.freeze
      STRING_ESCAPE_REGEX = /\\(?:
                              [abefnrstv\n]    |   # simple escapes (above)
                              \d{1,3}          |   # octal byte escape
                              x\d{1,2}         |   # hex byte escape
                              u[0-9a-fA-F]{4}  |   # unicode char escape
                              u\{[^}]*\}       |   # extended unicode escape
                              .                    # any other escaped char
                            )/x

      # Match literal regex characters, not including anchors, character
      # classes, alternatives, groups, repetitions, references, etc
      LITERAL_REGEX = /[\w\s\-,"'!#%&<>=;:`~]|\\[^AbBdDgGkwWszZS0-9]/

      module_function

      def operator?(symbol)
        OPERATOR_METHODS.include?(symbol)
      end

      def strip_quotes(str)
        if str[0] == '"' || str[0] == "'"
          str[0] = ''
        else
          # we're dealing with %q or %Q
          str[0, 3] = ''
        end
        str[-1] = ''

        str
      end

      def block_length(block_node)
        block_node.loc.end.line - block_node.loc.begin.line
      end

      def comment_line?(line_source)
        line_source =~ /^\s*#/
      end

      def line_range(arg)
        source_range = case arg
                       when Parser::Source::Range
                         arg
                       when Parser::AST::Node
                         arg.source_range
                       else
                         fail ArgumentError, "Invalid argument #{arg}"
                       end

        source_range.begin.line..source_range.end.line
      end

      def parentheses?(node)
        node.loc.respond_to?(:end) && node.loc.end &&
          node.loc.end.is?(')'.freeze)
      end

      def on_node(syms, sexp, excludes = [], &block)
        return to_enum(:on_node, syms, sexp, excludes) unless block_given?

        yield sexp if Array(syms).include?(sexp.type)
        return if Array(excludes).include?(sexp.type)

        sexp.children.each do |elem|
          next unless elem.is_a?(Parser::AST::Node)
          on_node(syms, elem, excludes, &block)
        end
      end

      def source_range(source_buffer, line_number, column, length = 1)
        if column.is_a?(Range)
          column_index = column.begin
          length = numeric_range_size(column)
        else
          column_index = column
        end

        preceding_line_numbers = (1...line_number)

        line_begin_pos = preceding_line_numbers.reduce(0) do |pos, line|
          pos + source_buffer.source_line(line).length + 1
        end

        begin_pos = line_begin_pos + column_index
        end_pos = begin_pos + length

        Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
      end

      def range_with_surrounding_comma(range, side = :both, buffer = nil)
        buffer ||= @processed_source.buffer
        src = buffer.source

        go_left, go_right = directions(side)

        begin_pos = range.begin_pos
        end_pos = range.end_pos
        begin_pos = move_pos(src, begin_pos, -1, go_left, /,/)
        end_pos = move_pos(src, end_pos, 1, go_right, /,/)

        Parser::Source::Range.new(buffer, begin_pos, end_pos)
      end

      def range_with_surrounding_space(range, side = :both, buffer = nil,
                                       with_newline = true)
        buffer ||= @processed_source.buffer
        src = buffer.source

        go_left, go_right = directions(side)

        begin_pos = range.begin_pos
        end_pos = range.end_pos
        begin_pos = move_pos(src, begin_pos, -1, go_left, /[ \t]/)
        begin_pos = move_pos(src, begin_pos, -1, go_left && with_newline, /\n/)
        end_pos = move_pos(src, end_pos, 1, go_right, /[ \t]/)
        end_pos = move_pos(src, end_pos, 1, go_right && with_newline, /\n/)
        Parser::Source::Range.new(buffer, begin_pos, end_pos)
      end

      def move_pos(src, pos, step, condition, regexp)
        offset = step == -1 ? -1 : 0
        pos += step while condition && src[pos + offset] =~ regexp
        pos
      end

      def directions(side)
        if side == :both
          [true, true]
        else
          [side == :left, side == :right]
        end
      end

      def begins_its_line?(range)
        source_before_range = range.source_buffer.source[0...range.begin_pos]
        source_before_range.rpartition("\n").last.strip.empty?
      end

      def within_node?(inner, outer)
        o = outer.is_a?(Astrolabe::Node) ? outer.source_range : outer
        i = inner.is_a?(Astrolabe::Node) ? inner.source_range : inner
        i.begin_pos >= o.begin_pos && i.end_pos <= o.end_pos
      end

      # Returns, for example, a bare `if` node if the given node is an `if`
      # with calls chained to the end of it.
      def first_part_of_call_chain(node)<commit_msg>Much faster implementation of Util#begins_its_line?<commit_after># encoding: utf-8
# rubocop:disable Metrics/ModuleLength

module RuboCop
  module Cop
    # This module contains a collection of useful utility methods.
    module Util
      include PathUtil
      extend Astrolabe::Sexp

      EQUALS_ASGN_NODES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn, :masgn]
      SHORTHAND_ASGN_NODES = [:op_asgn, :or_asgn, :and_asgn]
      ASGN_NODES = EQUALS_ASGN_NODES + SHORTHAND_ASGN_NODES

      # http://phrogz.net/programmingruby/language.html#table_18.4
      # Backtick is added last just to help editors parse this code.
      OPERATOR_METHODS = %w(
        | ^ & <=> == === =~ > >= < <= << >>
        + - * / % ** ~ +@ -@ [] []= ! != !~
      ).map(&:to_sym) + [:'`']

      STRING_ESCAPES = {
        '\a' => "\a", '\b' => "\b", '\e' => "\e", '\f' => "\f", '\n' => "\n",
        '\r' => "\r", '\s' => ' ',  '\t' => "\t", '\v' => "\v", "\\\n" => ''
      }.freeze
      STRING_ESCAPE_REGEX = /\\(?:
                              [abefnrstv\n]    |   # simple escapes (above)
                              \d{1,3}          |   # octal byte escape
                              x\d{1,2}         |   # hex byte escape
                              u[0-9a-fA-F]{4}  |   # unicode char escape
                              u\{[^}]*\}       |   # extended unicode escape
                              .                    # any other escaped char
                            )/x

      # Match literal regex characters, not including anchors, character
      # classes, alternatives, groups, repetitions, references, etc
      LITERAL_REGEX = /[\w\s\-,"'!#%&<>=;:`~]|\\[^AbBdDgGkwWszZS0-9]/

      module_function

      def operator?(symbol)
        OPERATOR_METHODS.include?(symbol)
      end

      def strip_quotes(str)
        if str[0] == '"' || str[0] == "'"
          str[0] = ''
        else
          # we're dealing with %q or %Q
          str[0, 3] = ''
        end
        str[-1] = ''

        str
      end

      def block_length(block_node)
        block_node.loc.end.line - block_node.loc.begin.line
      end

      def comment_line?(line_source)
        line_source =~ /^\s*#/
      end

      def line_range(arg)
        source_range = case arg
                       when Parser::Source::Range
                         arg
                       when Parser::AST::Node
                         arg.source_range
                       else
                         fail ArgumentError, "Invalid argument #{arg}"
                       end

        source_range.begin.line..source_range.end.line
      end

      def parentheses?(node)
        node.loc.respond_to?(:end) && node.loc.end &&
          node.loc.end.is?(')'.freeze)
      end

      def on_node(syms, sexp, excludes = [], &block)
        return to_enum(:on_node, syms, sexp, excludes) unless block_given?

        yield sexp if Array(syms).include?(sexp.type)
        return if Array(excludes).include?(sexp.type)

        sexp.children.each do |elem|
          next unless elem.is_a?(Parser::AST::Node)
          on_node(syms, elem, excludes, &block)
        end
      end

      def source_range(source_buffer, line_number, column, length = 1)
        if column.is_a?(Range)
          column_index = column.begin
          length = numeric_range_size(column)
        else
          column_index = column
        end

        preceding_line_numbers = (1...line_number)

        line_begin_pos = preceding_line_numbers.reduce(0) do |pos, line|
          pos + source_buffer.source_line(line).length + 1
        end

        begin_pos = line_begin_pos + column_index
        end_pos = begin_pos + length

        Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
      end

      def range_with_surrounding_comma(range, side = :both, buffer = nil)
        buffer ||= @processed_source.buffer
        src = buffer.source

        go_left, go_right = directions(side)

        begin_pos = range.begin_pos
        end_pos = range.end_pos
        begin_pos = move_pos(src, begin_pos, -1, go_left, /,/)
        end_pos = move_pos(src, end_pos, 1, go_right, /,/)

        Parser::Source::Range.new(buffer, begin_pos, end_pos)
      end

      def range_with_surrounding_space(range, side = :both, buffer = nil,
                                       with_newline = true)
        buffer ||= @processed_source.buffer
        src = buffer.source

        go_left, go_right = directions(side)

        begin_pos = range.begin_pos
        end_pos = range.end_pos
        begin_pos = move_pos(src, begin_pos, -1, go_left, /[ \t]/)
        begin_pos = move_pos(src, begin_pos, -1, go_left && with_newline, /\n/)
        end_pos = move_pos(src, end_pos, 1, go_right, /[ \t]/)
        end_pos = move_pos(src, end_pos, 1, go_right && with_newline, /\n/)
        Parser::Source::Range.new(buffer, begin_pos, end_pos)
      end

      def move_pos(src, pos, step, condition, regexp)
        offset = step == -1 ? -1 : 0
        pos += step while condition && src[pos + offset] =~ regexp
        pos
      end

      def directions(side)
        if side == :both
          [true, true]
        else
          [side == :left, side == :right]
        end
      end

      def begins_its_line?(range)
        (range.source_line =~ /\S/) == range.column
      end

      def within_node?(inner, outer)
        o = outer.is_a?(Astrolabe::Node) ? outer.source_range : outer
        i = inner.is_a?(Astrolabe::Node) ? inner.source_range : inner
        i.begin_pos >= o.begin_pos && i.end_pos <= o.end_pos
      end

      # Returns, for example, a bare `if` node if the given node is an `if`
      # with calls chained to the end of it.
      def first_part_of_call_chain(node)
{'fraction_non_alphanumeric': 0.12847854356306893, 'fraction_numerical': 0.005635023840485479, 'mean_word_length': 1.770413064361191, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4191446', 'n_tokens_mistral': 3828, 'n_tokens_neox': 3677, 'n_words': 809}