Parent Directory | Revision Log
Revision 17 - (view) (download)
1 : | ian | 1 | /* Regular Expression API |
2 : | * (C)2004 by David "BAILOPAN" Anderson | ||
3 : | * Licensed under the GNU General Public License. | ||
4 : | * No warranties of any kind. | ||
5 : | */ | ||
6 : | |||
7 : | #if defined _regex_included | ||
8 : | #endinput | ||
9 : | #endif | ||
10 : | #define _regex_included | ||
11 : | |||
12 : | #if AMXX_VERSION_NUM >= 175 | ||
13 : | #pragma reqlib regex | ||
14 : | #if !defined AMXMODX_NOAUTOLOAD | ||
15 : | #pragma loadlib regex | ||
16 : | #endif | ||
17 : | #else | ||
18 : | #pragma library regex | ||
19 : | #endif | ||
20 : | |||
21 : | enum Regex | ||
22 : | { | ||
23 : | REGEX_MATCH_FAIL = -2, | ||
24 : | REGEX_PATTERN_FAIL, | ||
25 : | REGEX_NO_MATCH, | ||
26 : | REGEX_OK | ||
27 : | }; | ||
28 : | |||
29 : | ian | 17 | /** |
30 : | * Precompile a regular expression. Use this if you intend on using the | ||
31 : | * same expression multiple times. Pass the regex handle returned here to | ||
32 : | * regex_match_c to check for matches. | ||
33 : | * | ||
34 : | * @param pattern The regular expression pattern. | ||
35 : | * @param errcode Error code encountered, if applicable. | ||
36 : | * @param error Error message encountered, if applicable. | ||
37 : | * @param maxLen Maximum string length of the error buffer. | ||
38 : | * @param flags General flags for the regular expression. | ||
39 : | * i = Ignore case | ||
40 : | * m = Multilines (affects ^ and $ so that they match | ||
41 : | * the start/end of a line rather than matching the | ||
42 : | * start/end of the string). | ||
43 : | * s = Single line (affects . so that it matches any character, | ||
44 : | * even new line characters). | ||
45 : | * x = Pattern extension (ignore whitespace and # comments). | ||
46 : | * | ||
47 : | * @return -1 on error in the pattern, > valid regex handle (> 0) on success. | ||
48 : | * | ||
49 : | * @note This handle is automatically freed on map change. However, | ||
50 : | * if you are completely done with it before then, you should | ||
51 : | * call regex_free on this handle. | ||
52 : | ian | 1 | */ |
53 : | ian | 17 | native Regex:regex_compile(const pattern[], &ret, error[], maxLen, const flags[]=""); |
54 : | ian | 1 | |
55 : | ian | 17 | /** |
56 : | * Matches a string against a pre-compiled regular expression pattern. | ||
57 : | * | ||
58 : | * | ||
59 : | * @param pattern The regular expression pattern. | ||
60 : | * @param string The string to check. | ||
61 : | * @param ret Error code, if applicable, or number of results on success. | ||
62 : | * | ||
63 : | * @return -2 = Matching error (error code is stored in ret) | ||
64 : | * 0 = No match. | ||
65 : | * >1 = Number of results. | ||
66 : | * | ||
67 : | * @note You should free the returned handle (with regex_free()) | ||
68 : | * when you are done with this pattern. | ||
69 : | * | ||
70 : | * @note Use the regex handle passed to this function to extract | ||
71 : | * matches with regex_substr(). | ||
72 : | ian | 1 | */ |
73 : | ian | 17 | native regex_match_c(const string[], Regex:pattern, &ret); |
74 : | |||
75 : | /** | ||
76 : | * Matches a string against a regular expression pattern. | ||
77 : | * | ||
78 : | * @note If you intend on using the same regular expression pattern | ||
79 : | * multiple times, consider using regex_compile and regex_match_c | ||
80 : | * instead of making this function reparse the expression each time. | ||
81 : | * | ||
82 : | * @param string The string to check. | ||
83 : | * @param pattern The regular expression pattern. | ||
84 : | * @param ret Error code, or result state of the match. | ||
85 : | * @param error Error message, if applicable. | ||
86 : | * @param maxLen Maximum length of the error buffer. | ||
87 : | * @param flags General flags for the regular expression. | ||
88 : | * i = Ignore case | ||
89 : | * m = Multilines (affects ^ and $ so that they match | ||
90 : | * the start/end of a line rather than matching the | ||
91 : | * start/end of the string). | ||
92 : | * s = Single line (affects . so that it matches any character, | ||
93 : | * even new line characters). | ||
94 : | * x = Pattern extension (ignore whitespace and # comments). | ||
95 : | * | ||
96 : | * @return -2 = Matching error (error code is stored in ret) | ||
97 : | * -1 = Error in pattern (error message and offset # in error and ret) | ||
98 : | * 0 = No match. | ||
99 : | * >1 = Handle for getting more information (via regex_substr) | ||
100 : | * | ||
101 : | * @note Flags only exist in amxmodx 1.8 and later. | ||
102 : | * @note You should free the returned handle (with regex_free()) | ||
103 : | * when you are done extracting all of the substrings. | ||
104 : | */ | ||
105 : | native Regex:regex_match(const string[], const pattern[], &ret, error[], maxLen, const flags[] = ""); | ||
106 : | |||
107 : | /** | ||
108 : | * Returns a matched substring from a regex handle. | ||
109 : | * Substring ids start at 0 and end at ret-1, where ret is from the corresponding | ||
110 : | * regex_match or regex_match_c function call. | ||
111 : | * | ||
112 : | * @param id The regex handle to extract data from. | ||
113 : | * @param str_id The index of the expression to get - starts at 0, and ends at ret - 1. | ||
114 : | * @param buffer The buffer to set to the matching substring. | ||
115 : | * @param maxLen The maximum string length of the buffer. | ||
116 : | * | ||
117 : | */ | ||
118 : | ian | 1 | native regex_substr(Regex:id, str_id, buffer[], maxLen); |
119 : | |||
120 : | ian | 17 | /** |
121 : | * Frees the memory associated with a regex result, and sets the handle to 0. | ||
122 : | * This must be called on all results from regex_match() when you are done extracting | ||
123 : | * the results with regex_substr(). | ||
124 : | * The results of regex_compile() (and subsequently, regex_match_c()) only need to be freed | ||
125 : | * when you are done using the pattern. | ||
126 : | * | ||
127 : | * | ||
128 : | * @param id The regex handle to free. | ||
129 : | * | ||
130 : | * @noreturn | ||
131 : | * | ||
132 : | * @note Do not use the handle again after freeing it! | ||
133 : | */ | ||
134 : | native regex_free(&Regex:id); |
Contact | ViewVC Help |
Powered by ViewVC 1.0.4 |