<commit_before>module JMESPath
  # @api private
  module Nodes
    class Slice < Node
      attr_reader :args

      def initialize(args)
        @args = args
      end

      def visit(value)
        function_slice(value, *@args)
      end

      def to_h
        {
          :type => :slice,
          :args => @args,
        }
      end

      private

      def function_slice(values, *args)
        if String === values || Array === values
          _slice(values, *args)
        else
          nil
        end
      end

      def _slice(values, start, stop, step)
        start, stop, step = _adjust_slice(values.size, start, stop, step)
        result = []
        if step > 0
          i = start
          while i < stop
            result << values[i]
            i += step
          end
        else
          i = start
          while i > stop
            result << values[i]
            i += step
          end
        end
        String === values ? result.join : result
      end

      def _adjust_slice(length, start, stop, step)
        if step.nil?
          step = 1
        elsif step == 0
          raise Errors::RuntimeError, 'slice step cannot be 0'
        end

        if start.nil?
          start = step < 0 ? length - 1 : 0
        else
          start = _adjust_endpoint(length, start, step)
        end

        if stop.nil?
          stop = step < 0 ? -1 : length
        else
          stop = _adjust_endpoint(length, stop, step)
        end

        [start, stop, step]
      end

      def _adjust_endpoint(length, endpoint, step)
        if endpoint < 0
          endpoint += length
          endpoint = 0 if endpoint < 0
          endpoint
        elsif endpoint >= length
          step < 0 ? length - 1 : length
        else
          endpoint
        end
      end

    end
  end
end<commit_msg>Inline slicing logic in Slice#visit<commit_after>module JMESPath
  # @api private
  module Nodes
    class Slice < Node
      attr_reader :args

      def initialize(args)
        @args = args
      end

      def visit(value)
        if String === value || Array === value
          start, stop, step = adjust_slice(value.size, *@args)
          result = []
          if step > 0
            i = start
            while i < stop
              result << value[i]
              i += step
            end
          else
            i = start
            while i > stop
              result << value[i]
              i += step
            end
          end
          String === value ? result.join : result
        else
          nil
        end
      end

      def to_h
        {
          :type => :slice,
          :args => @args,
        }
      end

      private

      def adjust_slice(length, start, stop, step)
        if step.nil?
          step = 1
        elsif step == 0
          raise Errors::RuntimeError, 'slice step cannot be 0'
        end

        if start.nil?
          start = step < 0 ? length - 1 : 0
        else
          start = adjust_endpoint(length, start, step)
        end

        if stop.nil?
          stop = step < 0 ? -1 : length
        else
          stop = adjust_endpoint(length, stop, step)
        end

        [start, stop, step]
      end

      def adjust_endpoint(length, endpoint, step)
        if endpoint < 0
          endpoint += length
          endpoint = 0 if endpoint < 0
          endpoint
        elsif endpoint >= length
          step < 0 ? length - 1 : length
        else
          endpoint
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07687943262411348, 'fraction_numerical': 0.007943262411347518, 'mean_word_length': 1.1900621118012422, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 13, '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': '7191731', 'n_tokens_mistral': 991, 'n_tokens_neox': 944, 'n_words': 323}