2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
12 * This is a regular expression library that implements a subset of Perl RE.
13 * Please refer to http://slre.sourceforge.net for detailed description.
15 * Usage example (parsing HTTP request):
18 * struct cap captures[4 + 1]; // Number of braket pairs + 1
21 * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
23 * if (slre_match(&slre, buf, len, captures)) {
24 * printf("Request line length: %d\n", captures[0].len);
25 * printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
26 * printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
30 * ^ Match beginning of a buffer
31 * $ Match end of a buffer
32 * () Grouping and substring capturing
33 * [...] Match any character from set
34 * [^...] Match any character but ones from set
36 * \S Match non-whitespace
37 * \d Match decimal digit
38 * \r Match carriage return
40 * + Match one or more times (greedy)
41 * +? Match one or more times (non-greedy)
42 * * Match zero or more times (greedy)
43 * *? Match zero or more times (non-greedy)
44 * ? Match zero or once
45 * \xDD Match byte with hex value 0xDD
46 * \meta Match one of the meta character: ^$().[*+?\
49 #ifndef SLRE_HEADER_DEFINED
50 #define SLRE_HEADER_DEFINED
53 * Compiled regular expression
56 unsigned char code[256];
57 unsigned char data[256];
60 int num_caps; /* Number of bracket pairs */
61 int anchored; /* Must match from string start */
62 const char *err_str; /* Error string */
69 const char *ptr; /* Pointer to the substring */
70 int len; /* Substring length */
74 * Compile regular expression. If success, 1 is returned.
75 * If error, 0 is returned and slre.err_str points to the error message.
77 int slre_compile(struct slre *, const char *re);
80 * Return 1 if match, 0 if no match.
81 * If `captured_substrings' array is not NULL, then it is filled with the
82 * values of captured substrings. captured_substrings[0] element is always
83 * a full matched substring. The round bracket captures start from
84 * captured_substrings[1].
85 * It is assumed that the size of captured_substrings array is enough to
86 * hold all captures. The caller function must make sure it is! So, the
87 * array_size = number_of_round_bracket_pairs + 1
89 int slre_match(const struct slre *, const char *buf, int buf_len,
90 struct cap *captured_substrings);
92 #endif /* SLRE_HEADER_DEFINED */