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