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 * Downloaded Sat Nov 5 17:42:08 CET 2011 at
13 * http://slre.sourceforge.net/1.0/slre.h
17 * This is a regular expression library that implements a subset of Perl RE.
18 * Please refer to http://slre.sourceforge.net for detailed description.
20 * Usage example (parsing HTTP request):
23 * struct cap captures[4 + 1]; // Number of braket pairs + 1
26 * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
28 * if (slre_match(&slre, buf, len, captures)) {
29 * printf("Request line length: %d\n", captures[0].len);
30 * printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
31 * printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
35 * ^ Match beginning of a buffer
36 * $ Match end of a buffer
37 * () Grouping and substring capturing
38 * [...] Match any character from set
39 * [^...] Match any character but ones from set
41 * \S Match non-whitespace
42 * \d Match decimal digit
43 * \r Match carriage return
45 * + Match one or more times (greedy)
46 * +? Match one or more times (non-greedy)
47 * * Match zero or more times (greedy)
48 * *? Match zero or more times (non-greedy)
49 * ? Match zero or once
50 * \xDD Match byte with hex value 0xDD
51 * \meta Match one of the meta character: ^$().[*+?\
54 #ifndef SLRE_HEADER_DEFINED
55 #define SLRE_HEADER_DEFINED
58 * Compiled regular expression
61 unsigned char code[256];
62 unsigned char data[256];
65 int num_caps; /* Number of bracket pairs */
66 int anchored; /* Must match from string start */
67 const char *err_str; /* Error string */
74 const char *ptr; /* Pointer to the substring */
75 int len; /* Substring length */
79 * Compile regular expression. If success, 1 is returned.
80 * If error, 0 is returned and slre.err_str points to the error message.
82 int slre_compile(struct slre *, const char *re);
85 * Return 1 if match, 0 if no match.
86 * If `captured_substrings' array is not NULL, then it is filled with the
87 * values of captured substrings. captured_substrings[0] element is always
88 * a full matched substring. The round bracket captures start from
89 * captured_substrings[1].
90 * It is assumed that the size of captured_substrings array is enough to
91 * hold all captures. The caller function must make sure it is! So, the
92 * array_size = number_of_round_bracket_pairs + 1
94 int slre_match(const struct slre *, const char *buf, int buf_len,
95 struct cap *captured_substrings);
98 void slre_dump(const struct slre *r, FILE *fp);
99 #endif /* SLRE_TEST */
100 #endif /* SLRE_HEADER_DEFINED */