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