File: slice.rb

package info (click to toggle)
ruby-jmespath 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 2,039; makefile: 4
file content (86 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true
module JMESPath
  # @api private
  module Nodes
    class Slice < Node
      def initialize(start, stop, step)
        @start = start
        @stop = stop
        @step = step
        raise Errors::InvalidValueError, 'slice step cannot be 0' if @step == 0
      end

      def visit(value)
        if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
          start, stop, step = adjust_slice(value.size, @start, @stop, @step)
          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
          value.respond_to?(:to_str) ? result.join : result
        end
      end

      def optimize
        if (@step.nil? || @step == 1) && @start && @stop && @start > 0 && @stop > @start
          SimpleSlice.new(@start, @stop)
        else
          self
        end
      end

      private

      def adjust_slice(length, start, stop, step)
        step = 1 if step.nil?

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

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

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

    class SimpleSlice < Slice
      def initialize(start, stop)
        super(start, stop, 1)
      end

      def visit(value)
        if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
          value[@start, @stop - @start]
        end
      end
    end
  end
end