lib/marvin/util.rb in Sutto-marvin-0.1.20081120 vs lib/marvin/util.rb in Sutto-marvin-0.2.0

- old
+ new

@@ -1,8 +1,15 @@ module Marvin module Util + GLOB_PATTERN_MAP = { + '*' => '.*', + '?' => '.', + '[' => '[', + ']' => ']' + } + # Return the channel-name version of a string by # appending "#" to the front if it doesn't already # start with it. def channel_name(name) return name.to_s[0..0] == "#" ? name.to_s : "##{name}" @@ -18,12 +25,22 @@ # Specifies the last parameter of a response, used to # specify parameters which have spaces etc (for example, # the actual message part of a response). def last_param(section) - section && ":#{section.to_s.strip} " + section && ":#{section.to_s.strip}" end alias lp last_param + + # Converts a glob-like pattern into a regular + # expression for easy / fast matching. Code is + # from PLEAC at http://pleac.sourceforge.net/pleac_ruby/patternmatching.html + def glob2pattern(glob_string) + inner_pattern = glob_string.gsub(/(.)/) do |c| + GLOB_PATTERN_MAP[c] || Regexp::escape(c) + end + return Regexp.new("^#{inner_pattern}$") + end extend self end end \ No newline at end of file