plugins/include/regex.inc
author David Anderson <dvander@alliedmods.net>
Sat May 17 05:12:52 2008 +0000 (2008-05-17)
changeset 2056 f1fcde46ac7c
parent 2018 9466b2246fd3
child 3834 408d8ace4e7d
permissions -rw-r--r--
added amb1119 - sm_revote command
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
ds@1952
    33
#if defined _regex_included
ds@1952
    34
  #endinput
ds@1952
    35
#endif
ds@1952
    36
#define _regex_included
ds@1952
    37
ds@1952
    38
/**
ds@1952
    39
 * @section 	Flags for compiling regex expressions.  These come directly from the 
ds@1952
    40
 * pcre library and can be used in MatchRegex and CompileRegex.
ds@1952
    41
 */
ds@1952
    42
#define PCRE_CASELESS           0x00000001 /* Ignore Case */
ds@1952
    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). */
ds@1952
    44
#define PCRE_DOTALL             0x00000004 /* Single line (affects . so that it matches any character, even new line characters). */
ds@1952
    45
#define PCRE_EXTENDED           0x00000008 /* Pattern extension (ignore whitespace and # comments). */
ds@1952
    46
#define PCRE_UNGREEDY           0x00000200 /* Invert greediness of quantifiers */
ds@1952
    47
#define PCRE_UTF8               0x00000800 /* Use UTF-8 Chars */
ds@1952
    48
#define PCRE_NO_UTF8_CHECK      0x00002000 /* Do not check the pattern for UTF-8 validity (only relevant if PCRE_UTF8 is set) */
ds@1952
    49
ds@1952
    50
ds@1952
    51
/**
ds@1952
    52
 * Regex expression error codes.
ds@1952
    53
 */
ds@1952
    54
enum RegexError
ds@1952
    55
{
ds@1952
    56
	REGEX_ERROR_NONE = 0,				/* No error */
ds@1952
    57
	REGEX_ERROR_NOMATCH = -1,			/* No match was found */
ds@1952
    58
	REGEX_ERROR_NULL = -2,
ds@1952
    59
	REGEX_ERROR_BADOPTION = -3,
ds@1952
    60
	REGEX_ERROR_BADMAGIC = -4,
ds@1952
    61
	REGEX_ERROR_UNKNOWN_OPCODE = -5,
ds@1952
    62
	REGEX_ERROR_NOMEMORY = -6,
ds@1952
    63
	REGEX_ERROR_NOSUBSTRING = -7,
ds@1952
    64
	REGEX_ERROR_MATCHLIMIT = -8,
ds@1952
    65
	REGEX_ERROR_CALLOUT = -9,  			/* Never used by PCRE itself */
ds@1952
    66
	REGEX_ERROR_BADUTF8 = -10,
ds@1952
    67
	REGEX_ERROR_BADUTF8_OFFSET = -11,
ds@1952
    68
	REGEX_ERROR_PARTIAL = -12,
ds@1952
    69
	REGEX_ERROR_BADPARTIAL = -13,
ds@1952
    70
	REGEX_ERROR_INTERNAL = -14,
ds@1952
    71
	REGEX_ERROR_BADCOUNT = -15,
ds@1952
    72
	REGEX_ERROR_DFA_UITEM = -16,
ds@1952
    73
	REGEX_ERROR_DFA_UCOND = -17,
ds@1952
    74
	REGEX_ERROR_DFA_UMLIMIT = -18,
ds@1952
    75
	REGEX_ERROR_DFA_WSSIZE = -19,
ds@1952
    76
	REGEX_ERROR_DFA_RECURSE = -20,
ds@1952
    77
	REGEX_ERROR_RECURSIONLIMIT = -21,
ds@1952
    78
	REGEX_ERROR_NULLWSLIMIT = -22,  	/* No longer actually used */
ds@1952
    79
	REGEX_ERROR_BADNEWLINE = -23
pred@2018
    80
};
ds@1952
    81
ds@1952
    82
/**
ds@1952
    83
 * Precompile a regular expression.  Use this if you intend on using the 
ds@1952
    84
 * same expression multiple times.  Pass the regex handle returned here to
ds@1952
    85
 * MatchRegex to check for matches.
ds@1952
    86
 *
ds@1952
    87
 * @param pattern		The regular expression pattern.
ds@1952
    88
 * @param flags			General flags for the regular expression.
ds@1952
    89
 * @param error			Error message encountered, if applicable.
ds@1952
    90
 * @param maxLen		Maximum string length of the error buffer.
ds@1952
    91
 * @param errcode		Regex type error code encountered, if applicable.
ds@1952
    92
 * @return				Valid regex handle on success, INVALID_HANDLE on failure.
ds@1952
    93
 */
ds@1952
    94
native Handle:CompileRegex(const String:pattern[], flags = 0, String:error[]="", maxLen = 0, &RegexError:errcode = REGEX_ERROR_NONE);
ds@1952
    95
ds@1952
    96
/**
ds@1952
    97
 * Matches a string against a pre-compiled regular expression pattern.
ds@1952
    98
 *
ds@1952
    99
 * @param str			The string to check.
ds@1952
   100
 * @param regex			Regex Handle from CompileRegex()
ds@1952
   101
 * @param ret			Error code, if applicable.
ds@1952
   102
 * @return				Number of substrings found or -1 on failure.
ds@1952
   103
 *
ds@1952
   104
 * @note				Use the regex handle passed to this function to extract
ds@1952
   105
 *						matches with GetRegexSubString().
ds@1952
   106
 */
ds@1952
   107
native MatchRegex(Handle:regex, const String:str[], &RegexError:ret = REGEX_ERROR_NONE);
ds@1952
   108
ds@1952
   109
/**
ds@1952
   110
 * Returns a matched substring from a regex handle.
ds@1952
   111
 * Substring ids start at 0 and end at substrings-1, where substrings is the number returned 
ds@1952
   112
 * by MatchRegex
ds@1952
   113
 *
ds@1952
   114
 * @param regex			The regex handle to extract data from.
ds@1952
   115
 * @param str_id		The index of the expression to get - starts at 0, and ends at substrings - 1.
ds@1952
   116
 * @param buffer		The buffer to set to the matching substring.
ds@1952
   117
 * @param maxLen		The maximum string length of the buffer.
ds@1952
   118
 * @return				True if a substring was found, False on fail/error
ds@1952
   119
 */
ds@1952
   120
native bool:GetRegexSubString(Handle:regex, str_id, String:buffer[], maxlen);
ds@1952
   121
ds@1952
   122
/**
ds@1952
   123
 * Matches a string against a regular expression pattern.
ds@1952
   124
 *
ds@1952
   125
 * @note				If you intend on using the same regular expression pattern
ds@1952
   126
 *						multiple times, consider using CompileRegex and MatchRegex 
ds@1952
   127
 *						instead of making this function reparse the expression each time.
ds@1952
   128
 *
ds@1952
   129
 * @param str			The string to check.
ds@1952
   130
 * @param pattern		The regular expression pattern.
ds@1952
   131
 * @param flags			General flags for the regular expression.
ds@1952
   132
 * @param error			Error message, if applicable.
ds@1952
   133
 * @param maxLen		Maximum length of the error buffer.
ds@1952
   134
 * @return				Number of substrings found or -1 on failure.
ds@1952
   135
 */
ds@1952
   136
stock SimpleRegexMatch(const String:str[], const String:pattern[], flags = 0, String:error[]="", maxLen = 0)
ds@1952
   137
{
ds@1952
   138
	new Handle:regex = CompileRegex(pattern, flags, error, maxLen);
ds@1952
   139
	
ds@1952
   140
	if (regex == INVALID_HANDLE)
ds@1952
   141
	{
ds@1952
   142
		return -1;	
ds@1952
   143
	}
ds@1952
   144
	
ds@1952
   145
	new substrings = MatchRegex(regex, str);
ds@1952
   146
	
ds@1952
   147
	CloseHandle(regex);
ds@1952
   148
	
ds@1952
   149
	return substrings;	
ds@1952
   150
}
ds@1952
   151
ds@1952
   152
/**
ds@1952
   153
 * @endsection
ds@1952
   154
 */
ds@1952
   155
ds@1952
   156
/**
ds@1952
   157
 * Do not edit below this line!
ds@1952
   158
 */
ds@1952
   159
public Extension:__ext_regex = 
ds@1952
   160
{
ds@1952
   161
	name = "Regex Extension",
ds@1952
   162
	file = "regex.ext",
ds@1952
   163
#if defined AUTOLOAD_EXTENSIONS
ds@1952
   164
	autoload = 1,
ds@1952
   165
#else
ds@1952
   166
	autoload = 0,
ds@1952
   167
#endif
ds@1952
   168
#if defined REQUIRE_EXTENSIONS
ds@1952
   169
	required = 1,
ds@1952
   170
#else
ds@1952
   171
	required = 0,
ds@1952
   172
#endif
ds@1952
   173
};