Skip to content
Browse files

regex tuning

  • Loading branch information...
1 parent 7463021 commit fbfc76c877889b4d2cc963a889c8ffc32d9fca8b Michael Amygdalidis committed
Showing with 5 additions and 5 deletions.
  1. +1 −1 benchmark/benchmark.rb
  2. +4 −4 lib/textquery/textquery.rb
View
2 benchmark/benchmark.rb
@@ -15,7 +15,7 @@
text << s
}
-n = 1000
+n = 10000
Benchmark.bm do |x|
x.report do
n.times do
View
8 lib/textquery/textquery.rb
@@ -8,7 +8,7 @@
end
-BLAH = Regexp.new("(\d)*(~)?([^~]+)(~)?(\d)*$")
+FUZZY = /(\d)*(~)?([^~]+)(~)?(\d)*$/
class WordMatch < Treetop::Runtime::SyntaxNode
@@ -17,8 +17,8 @@ class WordMatch < Treetop::Runtime::SyntaxNode
@@regex_case ||= {}
def eval(text, opt)
-
- fuzzy = query.match(BLAH)
+
+ fuzzy = query.match(FUZZY)
q = []
q.push "." if fuzzy[2]
@@ -28,7 +28,7 @@ def eval(text, opt)
q.push fuzzy[5].nil? ? "*" : "{#{fuzzy[5]}}" if fuzzy[4]
q = q.join
- regex = "^#{q}#{opt[:delim]}|#{opt[:delim]}#{q}#{opt[:delim]}|#{opt[:delim]}#{q}$|^#{q}$"
+ regex = "(^|#{opt[:delim]})#{q}(#{opt[:delim]}|$)"
unless @@regex[regex] then
@@regex[regex] = Regexp.new(regex, Regexp::IGNORECASE)

0 comments on commit fbfc76c

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