\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/parslet-1.8.2/lib/parslet/source/

Viewing File: line_cache.rb


class Parslet::Source
  # A cache for line start positions. 
  #
  class LineCache 
    def initialize
      # Stores line endings as a simple position number. The first line always
      # starts at 0; numbers beyond the biggest entry are on any line > size, 
      # but probably make a scan to that position neccessary.
      @line_ends = []
      @line_ends.extend RangeSearch
      @last_line_end = nil
    end

    # Returns a <line, column> tuple for the given input position. Input
    # position must be given as byte offset into original string. 
    #
    def line_and_column(pos)
      pos = pos.bytepos if pos.respond_to? :bytepos
      eol_idx = @line_ends.lbound(pos)

      if eol_idx
        # eol_idx points to the offset that ends the current line.
        # Let's try to find the offset that starts it: 
        offset = eol_idx>0 && @line_ends[eol_idx-1] || 0
        return [eol_idx+1, pos-offset+1]
      else
        # eol_idx is nil, that means that we're beyond the last line end that
        # we know about. Pretend for now that we're just on the last line.
        offset = @line_ends.last || 0
        return [@line_ends.size+1, pos-offset+1]
      end
    end

    def scan_for_line_endings(start_pos, buf)
      return unless buf

      buf = StringScanner.new(buf)
      return unless buf.exist?(/\n/)

      ## If we have already read part or all of buf, we already know about
      ## line ends in that portion. remove it and correct cur (search index)
      if @last_line_end && start_pos < @last_line_end
        # Let's not search the range from start_pos to last_line_end again.
        buf.pos = @last_line_end - start_pos
      end

      ## Scan the string for line endings; store the positions of all endings
      ## in @line_ends. 
      while buf.skip_until(/\n/)
        @last_line_end = start_pos + buf.pos
        @line_ends << @last_line_end
      end
    end
  end

  # Mixin for arrays that implicitly give a number of ranges, where one range
  # begins where the other one ends.
  # 
  #   Example: 
  #
  #     [10, 20, 30]
  #     # would describe [0, 10], (10, 20], (20, 30]
  #
  module RangeSearch 
    def find_mid(left, right)
      # NOTE: Jonathan Hinkle reported that when mathn is required, just
      # dividing and relying on the integer truncation is not enough.
      left + ((right - left) / 2).floor
    end  
    
    # Scans the array for the first number that is > than bound. Returns the 
    # index of that number. 
    #
    def lbound(bound)
      return nil if empty?
      return nil unless last > bound

      left = 0
      right = size - 1 

      loop do
        mid = find_mid(left, right)

        if self[mid] > bound
          right = mid
        else
          # assert: self[mid] <= bound
          left = mid+1
        end

        if right <= left
          return right
        end
      end
    end
  end
end