Sha256: 20de6d545d727be800f42e386e28bb58bb4604f67b18424c7b885f4514ab6d7a

Contents?: true

Size: 1.11 KB

Versions: 10

Compression:

Stored size: 1.11 KB

Contents

class MatchData

  # Returns [ pre_match, matchtree, post_match ]. (see matchtree)
  #
  #   md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX"
  #   md.to_a      #=> ["bbccddee", "bb", "ccdd", "dd", "ee"]
  #   md.matchset  #=> ["XXaa", [["bb"], ["cc", ["dd"]], ["ee"]], "ffXX"]
  #
  # CREDIT: Trans

  def matchset
     [pre_match, matchtree, post_match]
  end

  # An alternate to #to_a which returns the matches in
  # order corresponding with the regular expression.
  #
  #   md = /(bb)(cc(dd))(ee)/.match "XXaabbccddeeffXX"
  #   md.to_a       #=> ["bbccddee", "bb", "ccdd", "dd", "ee"]
  #   md.matchtree  #=> [["bb"], ["cc", ["dd"]], ["ee"]]
  #
  # CREDIT: Trans

  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

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/matchdata/matchset.rb
facets-3.1.0 lib/core/facets/matchdata/matchset.rb
facets-3.0.0 lib/core/facets/matchdata/matchset.rb
facets-2.9.3 lib/core/facets/matchdata/matchset.rb
facets-2.9.2 src/core/facets/matchdata/matchset.rb
facets-2.9.2 lib/core/facets/matchdata/matchset.rb
facets-2.9.1 lib/core/facets/matchdata/matchset.rb
facets-2.9.0 lib/core/facets/matchdata/matchset.rb
facets-2.9.0.pre.2 lib/core/facets/matchdata/matchset.rb
facets-2.9.0.pre.1 lib/core/facets/matchdata/matchset.rb