Skip to content
Browse files

1.9 friendly.. and ~55% faster

  • Loading branch information...
1 parent 744847b commit 1d92a7228eb519e5da276bb58c0ae19336b1f8a8 @igrigorik committed Sep 4, 2010
Showing with 18 additions and 9 deletions.
  1. +16 −9 lib/textquery/textquery.rb
  2. +2 −0 spec/textquery_spec.rb
View
25 lib/textquery/textquery.rb
@@ -1,24 +1,31 @@
require 'rubygems'
require 'treetop'
-require 'oniguruma'
-include Oniguruma
-
-# make it utf-8 compatible
+# make it utf-8 compatible for < 1.9 Ruby
if RUBY_VERSION < '1.9'
require 'active_support'
+ require 'oniguruma'
+
+ include Oniguruma
$KCODE = 'u'
+
+ RegExp = ORegexp
+ RegExp::IGNORECASE = ORegexp::OPTION_IGNORECASE
+
+else
+ RegExp = Regexp
+ RegExp::IGNORECACASE = Regexp::IGNORECASE
end
-FUZZY = ORegexp.new('(\d)*(~)?([^~]+)(~)?(\d)*$')
+FUZZY = RegExp.new('(\d)*(~)?([^~]+)(~)?(\d)*$')
class WordMatch < Treetop::Runtime::SyntaxNode
@@regex ||= {}
@@regex_case ||= {}
def eval(text, opt)
- query = ORegexp.escape(text_value)
+ query = RegExp.escape(text_value)
qkey = query + opt[:delim]
if not @@regex[qkey]
@@ -34,8 +41,8 @@ def eval(text, opt)
regex = "(^|#{opt[:delim]})#{q}(#{opt[:delim]}|$)"
- @@regex[qkey] = ORegexp.new(regex, :options => OPTION_IGNORECASE)
- @@regex_case[qkey] = ORegexp.new(regex, nil)
+ @@regex[qkey] = RegExp.new(regex, :options => RegExp::IGNORECASE)
+ @@regex_case[qkey] = RegExp.new(regex, nil)
end
if opt[:ignorecase]
@@ -89,6 +96,6 @@ def terminal_failures
def update_options(options)
@options = {:delim => ' '}.merge(options)
- @options[:delim] = "(#{[@options[:delim]].flatten.map { |opt| ORegexp.escape(opt) }.join("|")})"
+ @options[:delim] = "(#{[@options[:delim]].flatten.map { |opt| RegExp.escape(opt) }.join("|")})"
end
end
View
2 spec/textquery_spec.rb
@@ -1,3 +1,5 @@
+# encoding: UTF-8
+
require "rubygems"
require "spec"
require "pp"

0 comments on commit 1d92a72

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