Sha256: 585052bcb01d071e2534352e1c067654538cdb3a80615599f3d3ad0c07d1ece8

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

# based on http://cpan.uwinnipeg.ca/htdocs/Text-Glob/Text/Glob.pm.html#glob_to_regex_string-

# todo: release as separate gem
#
module Rerun
  class Glob
    NO_LEADING_DOT = '(?=[^\.])'   # todo

    def initialize glob_string
      @glob_string = glob_string
    end

    def to_regexp_string
      chars = @glob_string.split('')

      chars = smoosh(chars)

      curlies = 0;
      escaping = false;
      chars.map do |char|
        if escaping
          escaping = false
          char
        else
          case char
            when '**'
              "([^/]+/)*"
            when '*'
              ".*"
            when "?"
              "."
            when "."
              "\\."

            when "{"
              curlies += 1
              "("
            when "}"
              if curlies > 0
                curlies -= 1
                ")"
              else
                char
              end
            when ","
              if curlies > 0
                "|"
              else
                char
              end
            when "\\"
              escaping = true
              "\\"

            else
              char

          end
        end
      end.join
    end

    def to_regexp
      Regexp.new(to_regexp_string)
    end

    def smoosh chars
      out = []
      until chars.empty?
        char = chars.shift
        if char == "*" and chars.first == "*"
          chars.shift
          chars.shift if chars.first == "/"
          out.push("**")
        else
          out.push(char)
        end
      end
      out
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rerun-0.7.0.pre5 lib/rerun/glob.rb
rerun-0.7.0.pre4 lib/rerun/glob.rb
rerun-0.7.0.pre3 lib/rerun/glob.rb