class String

Public Instance Methods

&(*args) click to toggle source
# File lib/ext/string.rb, line 50
def &(*args)
  self.cfmt.%(*args)
end
c!()
Alias for: cfmt
cfmt() click to toggle source
# File lib/ext/string.rb, line 35
def cfmt
  colors = ANSI.colors.keys.join
  opts   = ANSI.options.keys.join
  formatted = self
  matches(/\{(?<color>[#{colors}]?)(?<opts>[#{opts}*^]*)\|(?<str>.*?)\}/).each do |m|
    opts = m.opts.split(//).map(&:sym)
    bright = (opts.delete(:'*') || opts.delete(:'^')) ? 90 : 30
    codes = [(ANSI.colors[m.color].to_i + bright)] + ANSI.options.values_at(*opts).compact
    formatted = formatted.sub(m[0], "\033[#{codes.join(';')}m#{m.str}\033[m")
  end

  formatted
end
Also aliased as: c!
matches(regexp) { |match| ... } click to toggle source
# File lib/ext/string.rb, line 54
def matches(regexp)
  start = 0
  matches = []
  while (match = match(regexp, start))
    start = match.end(0)
    matches << match
    yield match if block_given?
  end
  matches
end
tokenize() click to toggle source
# File lib/ext/string.rb, line 69
def tokenize
  tokens = []

  lit    = /\~(?<literal>[^\.]+)/
  regex  = /\/(?<regex>.*?(?<!\\))\//
  lookup = /\{\{(?<lookup>.*?)\}\}/
  up     = /(?<up>\^)/
  path   = /(?<path>[^\.\[]+)/
  indexes = /(?<indexes>[\d\+\.,\s-]+)/

  matcher = /#{lit}|#{regex}|#{lookup}|#{up}|#{path}(?:\[#{indexes}\])?/

  matches(matcher) do |match|
    if match.literal?
      tokens << Mobj::Token.new(:literal, match.literal)
    elsif match.lookup?
      tokens << Mobj::Token.new(:lookup, match.lookup.tokenize)
    elsif match.regex?
      tokens << Mobj::Token.new(:regex, Regexp.new(match.regex))
    elsif match.up?
      tokens << Mobj::Token.new(:up)
    elsif match.path?
      eachs = match.path.split(/\s*,\s*/)
      ors  = match.path.split(/\s*\|\s*/)
      ands = match.path.split(/\s*\&\s*/)
      if eachs.size > 1
        tokens << Mobj::Token.new(:each, eachs.map { |token| token.tokenize() })
      elsif ands.size > 1
        tokens << Mobj::Token.new(:all, ands.map { |token| token.tokenize() })
      elsif ors.size > 1
        tokens << Mobj::Token.new(:any, ors.map { |token| token.tokenize() })
      end

      unless ands.size + ors.size + eachs.size > 3
        options = {}
        index_matcher = /\s*(?<low>\d+)\s*(?:(?:\.\s*\.\s*(?<ex>\.)?\s*|-?)\s*(?<high>-?\d+|\+))?\s*/

        options[:indexes] = match.indexes.matches(index_matcher).map do |index|
          if index.high?
            Range.new(index.low.to_i, (index.high == "+" ? -1 : index.high.to_i), index.ex?)
          else
            index.low.to_i
          end
        end if match.indexes?

        if match.path[0] == '!'
          tokens << Mobj::Token.new(:inverse, Token.new(:path, match.path[1..-1].sym, options))
        else
          tokens << Mobj::Token.new(:path, match.path.sym, options)
        end
      end
    end
  end

  tokens.size == 1 ? tokens.first : Mobj::Token.new(:root, tokens)
end
walk(obj) click to toggle source
# File lib/ext/string.rb, line 65
def walk(obj)
  tokenize.walk(obj)
end