Sha256: 2f8147fd0c9ed710a8eae457b9fcb95c6c3cd8b833ef571f36d42add87ebcfdd

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

module RegexpExamples
  class BaseRepeater
    attr_reader :group
    def initialize(group)
      @group = group
    end

    def result(min_repeats, max_repeats)
      group_results = @group.result[0 .. MaxGroupResults-1]
      results = []
      min_repeats.upto(max_repeats) do |repeats|
        group_results.each do |group_result|
          results << group_result * repeats
        end
      end
      results.uniq
    end
  end

  class OneTimeRepeater < BaseRepeater
    def initialize(group)
      super
    end

    def result
      super(1, 1)
    end
  end

  class StarRepeater < BaseRepeater
    def initialize(group)
      super
    end

    def result
      super(0, TIMES)
    end
  end

  class PlusRepeater < BaseRepeater
    def initialize(group)
      super
    end

    def result
      super(1, TIMES)
    end
  end

  class QuestionMarkRepeater < BaseRepeater
    def initialize(group)
      super
    end

    def result
      super(0, 1)
    end
  end

  class RangeRepeater < BaseRepeater
    def initialize(group, min, has_comma, max)
      super(group)
      @min = min
      if max
        @max = max
      elsif has_comma
        @max = min + TIMES
      else
        @max = min
      end
    end

    def result
      super(@min, @max)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
regexp-examples-0.2.2 lib/regexp-examples/repeaters.rb
regexp-examples-0.2.1 lib/regexp-examples/repeaters.rb
regexp-examples-0.2.0 lib/regexp-examples/repeaters.rb
regexp-examples-0.1.0 lib/regexp-examples/repeaters.rb
regexp-examples-0.0.2 lib/regexp-examples/repeaters.rb
regexp-examples-0.0.1 lib/regexp-examples/repeaters.rb