From 7d78cbcf4bb53b976b7e27d851720fa914c93c1c Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Wed, 23 May 2007 17:51:31 +0000 Subject: [PATCH] Adding wildcard_to_regexp() to functions_general and cleaning up some imports. --- commands_privileged.py | 1 - functions_comsys.py | 4 ---- functions_general.py | 24 ++++++++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/commands_privileged.py b/commands_privileged.py index 2051dbbf0d..04633b235d 100644 --- a/commands_privileged.py +++ b/commands_privileged.py @@ -8,7 +8,6 @@ import session_mgr import functions_general import functions_db import commands_general -import commands_unloggedin import ansi """ Any command here is prefixed by an '@' sign, usually denoting a diff --git a/functions_comsys.py b/functions_comsys.py index e140a9d698..edd0d0776e 100644 --- a/functions_comsys.py +++ b/functions_comsys.py @@ -1,9 +1,5 @@ from apps.objects.models import CommChannel import session_mgr -import commands_privileged -import commands_general -import commands_comsys -import commands_unloggedin import ansi """ Comsys functions. diff --git a/functions_general.py b/functions_general.py index fc2c870bdf..128b1131eb 100644 --- a/functions_general.py +++ b/functions_general.py @@ -7,6 +7,30 @@ import commands_unloggedin General commonly used functions. """ +def wildcard_to_regexp(instring): + """ + Converts a player-supplied string that may have wildcards in it to regular + expressions. This is useful for name matching. + + instring: (string) A string that may potentially contain wildcards (* or ?). + """ + regexp_string = "" + + # If the string starts with an asterisk, we can't impose the beginning of + # string (^) limiter. + if instring[0] != "*": + regexp_string += "^" + + # Replace any occurances of * or ? with the appropriate groups. + regexp_string += instring.replace("*","(.*)").replace("?", "(.{1})") + + # If there's an asterisk at the end of the string, we can't impose the + # end of string ($) limiter. + if instring[-1] != "*": + regexp_string += "$" + + return regexp_string + def cmd_check_num_args(session, arg_list, min_args, errortext="Missing arguments!"): """ Check a player command's splitted argument list to make sure it contains