plugins/include/regex.inc
author Erik Minekus <erik@interwavestudios.com>
Wed Jun 23 23:41:47 2010 -0400 (2010-06-23)
changeset 3052 a102c645bf4b
parent 2018 9466b2246fd3
child 3834 408d8ace4e7d
permissions -rw-r--r--
Fixed lack of error checking on sm_motd. (bug 4460, r=dvander)
ds@1987
     1
/**
ds@1987
     2
 * vim: set ts=4 :
ds@1987
     3
 * =============================================================================
ds@1987
     4
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1987
     5
 * =============================================================================
ds@1987
     6
 *
ds@1987
     7
 * This file is part of the SourceMod/SourcePawn SDK.
ds@1987
     8
 *
ds@1987
     9
 * This program is free software; you can redistribute it and/or modify it under
ds@1987
    10
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1987
    11
 * Free Software Foundation.
ds@1987
    12
 * 
ds@1987
    13
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1987
    14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1987
    15
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1987
    16
 * details.
ds@1987
    17
 *
ds@1987
    18
 * You should have received a copy of the GNU General Public License along with
ds@1987
    19
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1987
    20
 *
ds@1987
    21
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1987
    22
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1987
    23
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1987
    24
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1987
    25
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1987
    26
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1987
    27
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1987
    28
 * or <http://www.sourcemod.net/license.php>.
ds@1987
    29
 *
ds@1987
    30
 * Version: $Id$
ds@1987
    31
 */
ds@1987
    32
pred@1842
    33
#if defined _regex_included
pred@1842
    34
  #endinput
pred@1842
    35
#endif
pred@1842
    36
#define _regex_included
pred@1842
    37
pred@1842
    38
/**
pred@1842
    39
 * @section 	Flags for compiling regex expressions.  These come directly from the 
pred@1842
    40
 * pcre library and can be used in MatchRegex and CompileRegex.
pred@1842
    41
 */
pred@1842
    42
#define PCRE_CASELESS           0x00000001 /* Ignore Case */
pred@1842
    43
#define PCRE_MULTILINE          0x00000002 /* Multilines (affects ^ and $ so that they match the start/end of a line rather than matching the start/end of the string). */
pred@1842
    44
#define PCRE_DOTALL             0x00000004 /* Single line (affects . so that it matches any character, even new line characters). */
pred@1842
    45
#define PCRE_EXTENDED           0x00000008 /* Pattern extension (ignore whitespace and # comments). */
pred@1842
    46
#define PCRE_UNGREEDY           0x00000200 /* Invert greediness of quantifiers */
pred@1842
    47
#define PCRE_UTF8               0x00000800 /* Use UTF-8 Chars */
pred@1842
    48
#define PCRE_NO_UTF8_CHECK      0x00002000 /* Do not check the pattern for UTF-8 validity (only relevant if PCRE_UTF8 is set) */
pred@1842
    49
pred@1842
    50
pred@1842
    51
/**
pred@1842
    52
 * Regex expression error codes.
pred@1842
    53
 */
pred@1842
    54
enum RegexError
pred@1842
    55
{
pred@1842
    56
	REGEX_ERROR_NONE = 0,				/* No error */
pred@1842
    57
	REGEX_ERROR_NOMATCH = -1,			/* No match was found */
pred@1842
    58
	REGEX_ERROR_NULL = -2,
pred@1842
    59
	REGEX_ERROR_BADOPTION = -3,
pred@1842
    60
	REGEX_ERROR_BADMAGIC = -4,
pred@1842
    61
	REGEX_ERROR_UNKNOWN_OPCODE = -5,
pred@1842
    62
	REGEX_ERROR_NOMEMORY = -6,
pred@1842
    63
	REGEX_ERROR_NOSUBSTRING = -7,
pred@1842
    64
	REGEX_ERROR_MATCHLIMIT = -8,
pred@1842
    65
	REGEX_ERROR_CALLOUT = -9,  			/* Never used by PCRE itself */
pred@1842
    66
	REGEX_ERROR_BADUTF8 = -10,
pred@1842
    67
	REGEX_ERROR_BADUTF8_OFFSET = -11,
pred@1842
    68
	REGEX_ERROR_PARTIAL = -12,
pred@1842
    69
	REGEX_ERROR_BADPARTIAL = -13,
pred@1842
    70
	REGEX_ERROR_INTERNAL = -14,
pred@1842
    71
	REGEX_ERROR_BADCOUNT = -15,
pred@1842
    72
	REGEX_ERROR_DFA_UITEM = -16,
pred@1842
    73
	REGEX_ERROR_DFA_UCOND = -17,
pred@1842
    74
	REGEX_ERROR_DFA_UMLIMIT = -18,
pred@1842
    75
	REGEX_ERROR_DFA_WSSIZE = -19,
pred@1842
    76
	REGEX_ERROR_DFA_RECURSE = -20,
pred@1842
    77
	REGEX_ERROR_RECURSIONLIMIT = -21,
pred@1842
    78
	REGEX_ERROR_NULLWSLIMIT = -22,  	/* No longer actually used */
pred@1842
    79
	REGEX_ERROR_BADNEWLINE = -23
pred@2018
    80
};
pred@1842
    81
pred@1842
    82
/**
pred@1842
    83
 * Precompile a regular expression.  Use this if you intend on using the 
pred@1842
    84
 * same expression multiple times.  Pass the regex handle returned here to
pred@1842
    85
 * MatchRegex to check for matches.
pred@1842
    86
 *
pred@1842
    87
 * @param pattern		The regular expression pattern.
pred@1842
    88
 * @param flags			General flags for the regular expression.
pred@1842
    89
 * @param error			Error message encountered, if applicable.
pred@1842
    90
 * @param maxLen		Maximum string length of the error buffer.
pred@1842
    91
 * @param errcode		Regex type error code encountered, if applicable.
pred@1842
    92
 * @return				Valid regex handle on success, INVALID_HANDLE on failure.
pred@1842
    93
 */
pred@1842
    94
native Handle:CompileRegex(const String:pattern[], flags = 0, String:error[]="", maxLen = 0, &RegexError:errcode = REGEX_ERROR_NONE);
pred@1842
    95
pred@1842
    96
/**
pred@1842
    97
 * Matches a string against a pre-compiled regular expression pattern.
pred@1842
    98
 *
pred@1842
    99
 * @param str			The string to check.
pred@1842
   100
 * @param regex			Regex Handle from CompileRegex()
pred@1842
   101
 * @param ret			Error code, if applicable.
pred@1842
   102
 * @return				Number of substrings found or -1 on failure.
pred@1842
   103
 *
pred@1842
   104
 * @note				Use the regex handle passed to this function to extract
pred@1842
   105
 *						matches with GetRegexSubString().
pred@1842
   106
 */
pred@1842
   107
native MatchRegex(Handle:regex, const String:str[], &RegexError:ret = REGEX_ERROR_NONE);
pred@1842
   108
pred@1842
   109
/**
pred@1842
   110
 * Returns a matched substring from a regex handle.
pred@1842
   111
 * Substring ids start at 0 and end at substrings-1, where substrings is the number returned 
pred@1842
   112
 * by MatchRegex
pred@1842
   113
 *
pred@1842
   114
 * @param regex			The regex handle to extract data from.
pred@1842
   115
 * @param str_id		The index of the expression to get - starts at 0, and ends at substrings - 1.
pred@1842
   116
 * @param buffer		The buffer to set to the matching substring.
pred@1842
   117
 * @param maxLen		The maximum string length of the buffer.
pred@1842
   118
 * @return				True if a substring was found, False on fail/error
pred@1842
   119
 */
pred@1842
   120
native bool:GetRegexSubString(Handle:regex, str_id, String:buffer[], maxlen);
pred@1842
   121
pred@1842
   122
/**
pred@1842
   123
 * Matches a string against a regular expression pattern.
pred@1842
   124
 *
pred@1842
   125
 * @note				If you intend on using the same regular expression pattern
pred@1842
   126
 *						multiple times, consider using CompileRegex and MatchRegex 
pred@1842
   127
 *						instead of making this function reparse the expression each time.
pred@1842
   128
 *
pred@1842
   129
 * @param str			The string to check.
pred@1842
   130
 * @param pattern		The regular expression pattern.
pred@1842
   131
 * @param flags			General flags for the regular expression.
pred@1842
   132
 * @param error			Error message, if applicable.
pred@1842
   133
 * @param maxLen		Maximum length of the error buffer.
pred@1842
   134
 * @return				Number of substrings found or -1 on failure.
pred@1842
   135
 */
pred@1842
   136
stock SimpleRegexMatch(const String:str[], const String:pattern[], flags = 0, String:error[]="", maxLen = 0)
pred@1842
   137
{
pred@1842
   138
	new Handle:regex = CompileRegex(pattern, flags, error, maxLen);
pred@1842
   139
	
pred@1842
   140
	if (regex == INVALID_HANDLE)
pred@1842
   141
	{
pred@1842
   142
		return -1;	
pred@1842
   143
	}
pred@1842
   144
	
pred@1842
   145
	new substrings = MatchRegex(regex, str);
pred@1842
   146
	
pred@1842
   147
	CloseHandle(regex);
pred@1842
   148
	
pred@1842
   149
	return substrings;	
pred@1842
   150
}
pred@1842
   151
pred@1842
   152
/**
pred@1842
   153
 * @endsection
pred@1842
   154
 */
pred@1842
   155
pred@1842
   156
/**
pred@1842
   157
 * Do not edit below this line!
pred@1842
   158
 */
pred@1842
   159
public Extension:__ext_regex = 
pred@1842
   160
{
pred@1842
   161
	name = "Regex Extension",
pred@1842
   162
	file = "regex.ext",
pred@1842
   163
#if defined AUTOLOAD_EXTENSIONS
pred@1842
   164
	autoload = 1,
pred@1842
   165
#else
pred@1842
   166
	autoload = 0,
pred@1842
   167
#endif
pred@1842
   168
#if defined REQUIRE_EXTENSIONS
pred@1842
   169
	required = 1,
pred@1842
   170
#else
pred@1842
   171
	required = 0,
pred@1842
   172
#endif
pred@1842
   173
};