Skip to content
Browse files

WideMathcerZeroの作成: w:0の時の検索を専用処理に

  • Loading branch information...
1 parent ff635fa commit b5a27db85a03c67101a8cb6e99b38cb226ab150c @ongaeshi committed Mar 22, 2013
Showing with 38 additions and 5 deletions.
  1. +2 −2 lib/milkode/cdweb/lib/grep.rb
  2. +34 −1 lib/milkode/common/wide_matcher.rb
  3. +2 −2 test/test_wide_matcher.rb
View
4 lib/milkode/cdweb/lib/grep.rb
@@ -19,7 +19,7 @@ def match_lines_stopover(patterns, max_match, start_index, is_sensitive, wide_ma
result = []
index = start_index
- matcher = WideMatcher.new(wide_match_range)
+ matcher = WideMatcher.create(wide_match_range)
lines = @content.split($/)
while (index < lines.size) do
@@ -48,7 +48,7 @@ def match_lines_and(patterns, is_sensitive, wide_match_range)
result = []
index = 0
- matcher = WideMatcher.new(wide_match_range)
+ matcher = WideMatcher.create(wide_match_range)
@content.each_line do |line|
matcher.add_line_matchs(index, match_regexps(line, regexps))
View
35 lib/milkode/common/wide_matcher.rb
@@ -10,6 +10,14 @@ module Milkode
class WideMatcher
attr_reader :num_max
+
+ def self.create(num_max)
+ if num_max == 0
+ WideMatcherZero.new
+ else
+ WideMatcher.new(num_max)
+ end
+ end
def initialize(num_max)
@num_max = num_max
@@ -22,7 +30,7 @@ def linenum
def add_line_matchs(index, matches)
@last_index = index
- @container.shift if @num_max > 0 && linenum >= @num_max
+ @container.shift if linenum >= @num_max
@container << matches
# p @container
end
@@ -46,6 +54,31 @@ def match_lines
end
end
end
+
+ class WideMatcherZero
+ attr_reader :num_max
+
+ def initialize
+ @num_max = 0
+ end
+
+ def linenum
+ 1
+ end
+
+ def add_line_matchs(index, matches)
+ @index = index
+ @matches = matches
+ end
+
+ def match?
+ @matches.any?
+ end
+
+ def match_lines
+ [MatchLineResult.new(@index, @matches.compact)]
+ end
+ end
end
View
4 test/test_wide_matcher.rb
@@ -20,7 +20,7 @@ class TestWideMatcher < Test::Unit::TestCase
EOF
def test_initialize
- obj = WideMatcher.new(5)
+ obj = WideMatcher.create(5)
assert_equal 5, obj.num_max
assert_equal 0, obj.linenum
end
@@ -29,7 +29,7 @@ def test_add_line_matches
lines = SRC.split("\n")
regexps = strs2regs(['a', 'c'])
- obj = WideMatcher.new(3)
+ obj = WideMatcher.create(3)
obj.add_line_matchs( 0, match_regexps(lines[0], regexps) )
assert_equal false, obj.match?

0 comments on commit b5a27db

Please sign in to comment.
Something went wrong with that request. Please try again.