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