Sha256: 52bbe18dede6627d1c784c255a12e640109fed4feb4a0f824c9aea7987c28b39

Contents?: true

Size: 1 KB

Versions: 10

Compression:

Stored size: 1 KB

Contents

class Parser::Source::Range
  def with(begin_pos: @begin_pos, end_pos: @end_pos)
    Parser::Source::Range.new(@source_buffer, begin_pos, end_pos)
  end

  # Similar to `end`, but is just after the current Range
  def succ
    with begin_pos: @end_pos+1, end_pos: @end_pos+1
  end

  # (1...10).split(2...3, 6...8) => [1...2, 3...6, 7...10]
  # Assumes inner_ranges are exclusive, and included in self
  def split(*inner_ranges)
    inner_ranges.sort_by!(&:begin_pos)
    [self.begin, *inner_ranges, self.end]
      .each_cons(2)
      .map{|i, j| with(begin_pos: i.end_pos, end_pos: j.begin_pos)}
      .reject(&:empty?)
  end

  def lstrip(pattern = /\s*/)
    if (match = /^#{pattern}/.match(source))
      with(begin_pos: @begin_pos + match[0].length)
    else
      self
    end
  end

  def rstrip(pattern = /\s*/)
    if (match = /#{pattern}$/.match(source))
      with(end_pos: @end_pos - match[0].length)
    else
      self
    end
  end

  def strip(pattern = /\s*/)
    lstrip(pattern).rstrip(pattern)
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
deep-cover-0.1.10 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.9 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.8 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.7 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.6 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.5 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.4 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.3 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.2 lib/deep_cover/parser_ext/range.rb
deep-cover-0.1.1 lib/deep_cover/parser_ext/range.rb