diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-05-01 00:41:15 +0200 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-05-01 00:41:15 +0200 |
commit | 722a87ebc272bcccc4cf9c954251467a0f46e2e0 (patch) | |
tree | f87d18987ff026d40170cf829020536ead1e2333 /lib | |
parent | 9306780e41b8e16ad111376a0baba58545634557 (diff) |
messagemapper: Regexp#mm_cleanup method instead of repeated (and wrong) progressive cleanup
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rbot/messagemapper.rb | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/rbot/messagemapper.rb b/lib/rbot/messagemapper.rb index 40c4e51f..b08a503f 100644 --- a/lib/rbot/messagemapper.rb +++ b/lib/rbot/messagemapper.rb @@ -15,6 +15,21 @@ class Regexp } Regexp.new(new, self.options) end + + # We may want to remove head and tail anchors + def remove_head_tail + new = self.source.sub(/^\^/,'').sub(/\$$/,'') + Regexp.new(new, self.options) + end + + # The MessageMapper cleanup method: does both remove_capture + # and remove_head_tail + def mm_cleanup + new = self.source.gsub(/(^|[^\\])((?:\\\\)*)\(([^?])/) { + "%s%s(?:%s" % [$1, $2, $3] + }.sub(/^\^/,'').sub(/\$$/,'') + Regexp.new(new, self.options) + end end module Irc @@ -484,13 +499,13 @@ class Bot sub = is_single ? "\\S+" : ".*?" when Regexp # Remove captures and the ^ and $ that are sometimes placed in requirement regexps - sub = has_req.remove_captures.source.sub(/^\^/,'').sub(/\$$/,'') + sub = has_req.mm_cleanup when String sub = Regexp.escape(has_req) when Array - sub = has_req[0].remove_captures.source.sub(/^\^/,'').sub(/\$$/,'') + sub = has_req[0].mm_cleanup when Hash - sub = has_req[:regexp].remove_captures.source.sub(/^\^/,'').sub(/\$$/,'') + sub = has_req[:regexp].mm_cleanup else warning "Odd requirement #{has_req.inspect} of class #{has_req.class} for parameter '#{name}'" sub = Regexp.escape(has_req.to_s) rescue "\\S+" |