Sha256: 19eff684be51d28721e5a22ed23e81bf38528aaea9769dc224912de5b9cadeb3

Contents?: true

Size: 833 Bytes

Versions: 3

Compression:

Stored size: 833 Bytes

Contents

class MatchData
  # An alternate to #to_a which returns the matches in
  # order corresponding with the regular expression.
  #
  #   require 'facet/matchdata/matchtree'
  #
  #   md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX"
  #   md.to_a      #=> ["XXaabbccddeeffXX", "bb", "ccdd", "dd", "ee"]
  #   md.matches  #=> ["XXaa", [["bb"], ["cc", ["dd"]], "ee"], "ffXX"]
  #
  def matchtree(index=0)
    ret=[]
    b, e=self.begin(index), self.end(index)
    while (index+=1)<=length
      if index==length || (bi=self.begin(index))>=e
        # we are finished, if something is left, then add it
        ret << string[b, e-b] if e>b
        break
      else
        if bi>=b
          ret << string[b, bi-b] if bi>b
          ret << matchtree(index)
          b=self.end(index)
        end
      end
    end
    return ret
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/matchdata/matchtree.rb
facets-0.7.1 lib/facet/matchdata/matchtree.rb
facets-0.7.2 lib/facet/matchdata/matchtree.rb