slre.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
  3. * All rights reserved
  4. *
  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.
  9. */
  10. /*
  11. * Downloaded Sat Nov 5 17:42:08 CET 2011 at
  12. * http://slre.sourceforge.net/1.0/slre.h
  13. */
  14. /*
  15. * This is a regular expression library that implements a subset of Perl RE.
  16. * Please refer to http://slre.sourceforge.net for detailed description.
  17. *
  18. * Usage example (parsing HTTP request):
  19. *
  20. * struct slre slre;
  21. * struct cap captures[4 + 1]; // Number of braket pairs + 1
  22. * ...
  23. *
  24. * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
  25. *
  26. * if (slre_match(&slre, buf, len, captures)) {
  27. * printf("Request line length: %d\n", captures[0].len);
  28. * printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
  29. * printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
  30. * }
  31. *
  32. * Supported syntax:
  33. * ^ Match beginning of a buffer
  34. * $ Match end of a buffer
  35. * () Grouping and substring capturing
  36. * [...] Match any character from set
  37. * [^...] Match any character but ones from set
  38. * \s Match whitespace
  39. * \S Match non-whitespace
  40. * \d Match decimal digit
  41. * \r Match carriage return
  42. * \n Match newline
  43. * + Match one or more times (greedy)
  44. * +? Match one or more times (non-greedy)
  45. * * Match zero or more times (greedy)
  46. * *? Match zero or more times (non-greedy)
  47. * ? Match zero or once
  48. * \xDD Match byte with hex value 0xDD
  49. * \meta Match one of the meta character: ^$().[*+?\
  50. */
  51. #ifndef SLRE_HEADER_DEFINED
  52. #define SLRE_HEADER_DEFINED
  53. /*
  54. * Compiled regular expression
  55. */
  56. struct slre {
  57. unsigned char code[256];
  58. unsigned char data[256];
  59. int code_size;
  60. int data_size;
  61. int num_caps; /* Number of bracket pairs */
  62. int anchored; /* Must match from string start */
  63. const char *err_str; /* Error string */
  64. };
  65. /*
  66. * Captured substring
  67. */
  68. struct cap {
  69. const char *ptr; /* Pointer to the substring */
  70. int len; /* Substring length */
  71. };
  72. /*
  73. * Compile regular expression. If success, 1 is returned.
  74. * If error, 0 is returned and slre.err_str points to the error message.
  75. */
  76. int slre_compile(struct slre *, const char *re);
  77. /*
  78. * Return 1 if match, 0 if no match.
  79. * If `captured_substrings' array is not NULL, then it is filled with the
  80. * values of captured substrings. captured_substrings[0] element is always
  81. * a full matched substring. The round bracket captures start from
  82. * captured_substrings[1].
  83. * It is assumed that the size of captured_substrings array is enough to
  84. * hold all captures. The caller function must make sure it is! So, the
  85. * array_size = number_of_round_bracket_pairs + 1
  86. */
  87. int slre_match(const struct slre *, const char *buf, int buf_len,
  88. struct cap *captured_substrings);
  89. #ifdef SLRE_TEST
  90. void slre_dump(const struct slre *r, FILE *fp);
  91. #endif /* SLRE_TEST */
  92. #endif /* SLRE_HEADER_DEFINED */