Rolf Meyer
/
slre
slre - Super Light Regular Expression library URL: http://slre.sourceforge.net/ Just ported to mbed.
slre.h@0:e0b85a04e7e5, 2009-11-18 (annotated)
- Committer:
- rolf
- Date:
- Wed Nov 18 18:01:01 2009 +0000
- Revision:
- 0:e0b85a04e7e5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rolf | 0:e0b85a04e7e5 | 1 | /* |
rolf | 0:e0b85a04e7e5 | 2 | * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com> |
rolf | 0:e0b85a04e7e5 | 3 | * All rights reserved |
rolf | 0:e0b85a04e7e5 | 4 | * |
rolf | 0:e0b85a04e7e5 | 5 | * "THE BEER-WARE LICENSE" (Revision 42): |
rolf | 0:e0b85a04e7e5 | 6 | * Sergey Lyubka wrote this file. As long as you retain this notice you |
rolf | 0:e0b85a04e7e5 | 7 | * can do whatever you want with this stuff. If we meet some day, and you think |
rolf | 0:e0b85a04e7e5 | 8 | * this stuff is worth it, you can buy me a beer in return. |
rolf | 0:e0b85a04e7e5 | 9 | */ |
rolf | 0:e0b85a04e7e5 | 10 | |
rolf | 0:e0b85a04e7e5 | 11 | /* |
rolf | 0:e0b85a04e7e5 | 12 | * This is a regular expression library that implements a subset of Perl RE. |
rolf | 0:e0b85a04e7e5 | 13 | * Please refer to http://slre.sourceforge.net for detailed description. |
rolf | 0:e0b85a04e7e5 | 14 | * |
rolf | 0:e0b85a04e7e5 | 15 | * Usage example (parsing HTTP request): |
rolf | 0:e0b85a04e7e5 | 16 | * |
rolf | 0:e0b85a04e7e5 | 17 | * struct slre slre; |
rolf | 0:e0b85a04e7e5 | 18 | * struct cap captures[4 + 1]; // Number of braket pairs + 1 |
rolf | 0:e0b85a04e7e5 | 19 | * ... |
rolf | 0:e0b85a04e7e5 | 20 | * |
rolf | 0:e0b85a04e7e5 | 21 | * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n"); |
rolf | 0:e0b85a04e7e5 | 22 | * |
rolf | 0:e0b85a04e7e5 | 23 | * if (slre_match(&slre, buf, len, captures)) { |
rolf | 0:e0b85a04e7e5 | 24 | * printf("Request line length: %d\n", captures[0].len); |
rolf | 0:e0b85a04e7e5 | 25 | * printf("Method: %.*s\n", captures[1].len, captures[1].ptr); |
rolf | 0:e0b85a04e7e5 | 26 | * printf("URI: %.*s\n", captures[2].len, captures[2].ptr); |
rolf | 0:e0b85a04e7e5 | 27 | * } |
rolf | 0:e0b85a04e7e5 | 28 | * |
rolf | 0:e0b85a04e7e5 | 29 | * Supported syntax: |
rolf | 0:e0b85a04e7e5 | 30 | * ^ Match beginning of a buffer |
rolf | 0:e0b85a04e7e5 | 31 | * $ Match end of a buffer |
rolf | 0:e0b85a04e7e5 | 32 | * () Grouping and substring capturing |
rolf | 0:e0b85a04e7e5 | 33 | * [...] Match any character from set |
rolf | 0:e0b85a04e7e5 | 34 | * [^...] Match any character but ones from set |
rolf | 0:e0b85a04e7e5 | 35 | * \s Match whitespace |
rolf | 0:e0b85a04e7e5 | 36 | * \S Match non-whitespace |
rolf | 0:e0b85a04e7e5 | 37 | * \d Match decimal digit |
rolf | 0:e0b85a04e7e5 | 38 | * \r Match carriage return |
rolf | 0:e0b85a04e7e5 | 39 | * \n Match newline |
rolf | 0:e0b85a04e7e5 | 40 | * + Match one or more times (greedy) |
rolf | 0:e0b85a04e7e5 | 41 | * +? Match one or more times (non-greedy) |
rolf | 0:e0b85a04e7e5 | 42 | * * Match zero or more times (greedy) |
rolf | 0:e0b85a04e7e5 | 43 | * *? Match zero or more times (non-greedy) |
rolf | 0:e0b85a04e7e5 | 44 | * ? Match zero or once |
rolf | 0:e0b85a04e7e5 | 45 | * \xDD Match byte with hex value 0xDD |
rolf | 0:e0b85a04e7e5 | 46 | * \meta Match one of the meta character: ^$().[*+?\ |
rolf | 0:e0b85a04e7e5 | 47 | */ |
rolf | 0:e0b85a04e7e5 | 48 | |
rolf | 0:e0b85a04e7e5 | 49 | #ifndef SLRE_HEADER_DEFINED |
rolf | 0:e0b85a04e7e5 | 50 | #define SLRE_HEADER_DEFINED |
rolf | 0:e0b85a04e7e5 | 51 | |
rolf | 0:e0b85a04e7e5 | 52 | /* |
rolf | 0:e0b85a04e7e5 | 53 | * Compiled regular expression |
rolf | 0:e0b85a04e7e5 | 54 | */ |
rolf | 0:e0b85a04e7e5 | 55 | struct slre { |
rolf | 0:e0b85a04e7e5 | 56 | unsigned char code[256]; |
rolf | 0:e0b85a04e7e5 | 57 | unsigned char data[256]; |
rolf | 0:e0b85a04e7e5 | 58 | int code_size; |
rolf | 0:e0b85a04e7e5 | 59 | int data_size; |
rolf | 0:e0b85a04e7e5 | 60 | int num_caps; /* Number of bracket pairs */ |
rolf | 0:e0b85a04e7e5 | 61 | int anchored; /* Must match from string start */ |
rolf | 0:e0b85a04e7e5 | 62 | const char *err_str; /* Error string */ |
rolf | 0:e0b85a04e7e5 | 63 | }; |
rolf | 0:e0b85a04e7e5 | 64 | |
rolf | 0:e0b85a04e7e5 | 65 | /* |
rolf | 0:e0b85a04e7e5 | 66 | * Captured substring |
rolf | 0:e0b85a04e7e5 | 67 | */ |
rolf | 0:e0b85a04e7e5 | 68 | struct cap { |
rolf | 0:e0b85a04e7e5 | 69 | const char *ptr; /* Pointer to the substring */ |
rolf | 0:e0b85a04e7e5 | 70 | int len; /* Substring length */ |
rolf | 0:e0b85a04e7e5 | 71 | }; |
rolf | 0:e0b85a04e7e5 | 72 | |
rolf | 0:e0b85a04e7e5 | 73 | /* |
rolf | 0:e0b85a04e7e5 | 74 | * Compile regular expression. If success, 1 is returned. |
rolf | 0:e0b85a04e7e5 | 75 | * If error, 0 is returned and slre.err_str points to the error message. |
rolf | 0:e0b85a04e7e5 | 76 | */ |
rolf | 0:e0b85a04e7e5 | 77 | int slre_compile(struct slre *, const char *re); |
rolf | 0:e0b85a04e7e5 | 78 | |
rolf | 0:e0b85a04e7e5 | 79 | /* |
rolf | 0:e0b85a04e7e5 | 80 | * Return 1 if match, 0 if no match. |
rolf | 0:e0b85a04e7e5 | 81 | * If `captured_substrings' array is not NULL, then it is filled with the |
rolf | 0:e0b85a04e7e5 | 82 | * values of captured substrings. captured_substrings[0] element is always |
rolf | 0:e0b85a04e7e5 | 83 | * a full matched substring. The round bracket captures start from |
rolf | 0:e0b85a04e7e5 | 84 | * captured_substrings[1]. |
rolf | 0:e0b85a04e7e5 | 85 | * It is assumed that the size of captured_substrings array is enough to |
rolf | 0:e0b85a04e7e5 | 86 | * hold all captures. The caller function must make sure it is! So, the |
rolf | 0:e0b85a04e7e5 | 87 | * array_size = number_of_round_bracket_pairs + 1 |
rolf | 0:e0b85a04e7e5 | 88 | */ |
rolf | 0:e0b85a04e7e5 | 89 | int slre_match(const struct slre *, const char *buf, int buf_len, |
rolf | 0:e0b85a04e7e5 | 90 | struct cap *captured_substrings); |
rolf | 0:e0b85a04e7e5 | 91 | |
rolf | 0:e0b85a04e7e5 | 92 | #endif /* SLRE_HEADER_DEFINED */ |