6 Author: Tatu Ylonen <ylo@ngs.fi>
8 Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
10 Permission to use, copy, modify, distribute, and sell this software
11 and its documentation is hereby granted without fee, provided that the
12 above copyright notice appears in all source code copies, the name of
13 Tatu Ylonen is not used to advertise products containing this software
14 or a derivation thereof, and all modified versions are clearly marked
17 This software is provided "as is" without express or implied warranty.
19 Created: Thu Sep 26 17:15:36 1991 ylo
20 Last modified: Fri Jan 3 12:05:45 1992 ylo
22 The SILC Regex API by Pekka Riikonen, under the same license as the original
27 /****h* silcutil/SILC Regular Expression Interface
31 * SILC regular expression interface provides Unix and POSIX compliant
32 * regular expression compilation and matching.
36 * SilcRegexStruct reg;
38 * // Compile regular expression
39 * if (!silc_regex_compile(®, "foo[0-9]*", 0))
42 * // Match string against the compiled regex
43 * if (!silc_regex_match(®, "foo20", 0, NULL, 0))
46 * // Free the compiled regular expression
47 * silc_regex_free(®);
54 /****s* silcutil/SilcRegexAPI/SilcRegex
58 * typedef struct { ... } *SilcRegex, SilcRegexStruct;
62 * The regular expression context.
65 typedef struct SilcRegexObject {
66 char *buffer; /* compiled pattern */
67 int allocated; /* allocated size of compiled pattern */
68 int used; /* actual length of compiled pattern */
69 char *fastmap; /* fastmap[ch] is true if ch can start pattern */
70 char *translate; /* translation to apply during comp/match */
71 char fastmap_accurate; /* true if fastmap is valid */
72 char can_be_null; /* true if can match empty string */
73 char uses_registers; /* registers used and need to be initialized */
74 char anchor; /* anchor: 0=none 1=begline 2=begbuf */
75 } *SilcRegex, SilcRegexStruct;
77 /****s* silcutil/SilcRegexAPI/SilcRegexMatch
81 * typedef struct { ... } *SilcRegexMatch, SilcRegexMatchStruct;
88 typedef struct SilcRegexMatchObject {
89 int start; /* Start offset of region */
90 int end; /* End offset of region */
91 } *SilcRegexMatch, SilcRegexMatchStruct;
94 /****d* silcutil/SilcRegexAPI/SilcRegexFlags
98 * typedef enum { ... } SilcRegexFlags;
102 * Regular expression feature flags.
107 SILC_REGEX_FLAG_DEFAULT = 0,
111 /****f* silcutil/SilcRegexAPI/silc_regex_compile
115 * SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
116 * SilcRegexFlags flags);
120 * Compiles the regular expression string `regex'. The `regexp' is a
121 * pre-allocated regular expression context. The `flags' define
122 * various feature flags. This function must be called before the
123 * silc_regex_match can be used to find matches.
125 * Returns TRUE after the compilation is completed. Returns FALSE on
126 * error and sets silc_errno.
129 SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
130 SilcRegexFlags flags);
132 /****f* silcutil/SilcRegexAPI/silc_regex_compile
136 * SilcBool silc_regex_match(SilcRegex regexp, const char *string,
137 * SilcUInt32 num_match, SilcRegexMatch match,
138 * SilcRegexFlags flags);
142 * Finds one or more matches from the `string' using the pre-compiled
143 * regular expression `regexp'. It must be compiled by calling the
144 * silc_regex_compile before calling this function. The `flags' defines
145 * various feature flags.
147 * If only one match is needed the `num_match' may be set to 0 and the
148 * `match' is set to NULL. If multiple matches (substrings) are needed the
149 * `num_match' defines the size of the `match' array, where each of the
150 * matches (with parenthesized regular expression) will be stored. The
151 * `match' provides information on where the match was found in `string',
152 * providing the start offset and end offset of the match. Unused entires
153 * in the array will have -1 as the offset values.
155 * Returns TRUE if the string matched the regular expression or FALSE
156 * if it did not match or error occurred. The silc_errno will indicate
157 * the error. The silc_errno is set to SILC_ERR_NOT_FOUND if the regular
158 * expression did not match.
162 * // Find first match (check if string matches)
163 * if (!silc_regex_match(®, "foo20", 0, NULL, 0))
166 * // Find multiple matches, one by one
167 * SilcRegexMatchStruct match;
169 * while (silc_regex_match(®, string, 1, &match, 0)) {
170 * match_string = silc_memdup(string + match.start,
171 * match.end - match.start);
172 * string += match.end;
175 * // Parse URI into its components, available in the match[] array
176 * SilcRegexStruct reg;
177 * SilcRegexMatchStruct match[7];
179 * silc_regex_compile(®, "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)", 0);
180 * silc_regex_match(®, "http://example.com/page.html", 7, match, 0);
183 SilcBool silc_regex_match(SilcRegex regexp, const char *string,
184 SilcUInt32 num_match, SilcRegexMatch match,
185 SilcRegexFlags flags);
187 /****f* silcutil/SilcRegexAPI/silc_regex_free
191 * void silc_regex_free(SilcRegex regexp);
195 * Free's the compiled regular expression context `regexp'. This must
196 * be called even if `regexp' is statically allocated. If the
197 * silc_regex_compile has been called this function must be called.
200 void silc_regex_free(SilcRegex regexp);
202 #endif /* SILCREGEX_H */