lua-regex.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef LUA_REGEX_H
  2. #define LUA_REGEX_H
  3. /******************************************************************************
  4. * Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. ******************************************************************************/
  25. /*
  26. Adapted by Domingo Alvarez Duarte 2012
  27. http://code.google.com/p/lua-regex-standalone/
  28. */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #include <sys/types.h>
  33. #include <stddef.h>
  34. /*
  35. ** maximum number of captures that a pattern can do during
  36. ** pattern-matching. This limit is arbitrary.
  37. */
  38. #define LUA_REGEX_MAXCAPTURES 32
  39. #define CAP_UNFINISHED (-1)
  40. #define CAP_POSITION (-2)
  41. typedef struct char_buffer_st {
  42. size_t size, used;
  43. char buf[1];
  44. } char_buffer_st;
  45. typedef struct LuaCapture {
  46. const char *init;
  47. ptrdiff_t len;
  48. } LuaCapture;
  49. typedef struct LuaMatchState {
  50. const char *src_init; /* init of source string */
  51. const char *src_end; /* end ('\0') of source string */
  52. const char *p_end; /* end ('\0') of pattern */
  53. const char *error;
  54. int level; /* total number of captures (finished or unfinished) */
  55. LuaCapture capture[LUA_REGEX_MAXCAPTURES];
  56. } LuaMatchState;
  57. typedef int (*luaregex_func_param)(LuaMatchState *ms, void *udata, char_buffer_st **b);
  58. int str_find (LuaMatchState *ms, const char *s, int ls,
  59. const char *p, int lp, int init, int raw_find,
  60. luaregex_func_param fp, void *udata);
  61. int str_match (LuaMatchState *ms, const char *s, int ls,
  62. const char *p, int lp, int init, int raw_find,
  63. luaregex_func_param fp, void *udata);
  64. char_buffer_st *str_gsub (const char *src, int srcl, const char *p, int lp,
  65. const char *tr, int ltr, size_t max_s, const char **error_ptr,
  66. luaregex_func_param fp, void *udata);
  67. int char_buffer_add_char(LuaMatchState *ms, char_buffer_st **b, char c);
  68. int char_buffer_add_str(LuaMatchState *ms, char_buffer_st **b, const char *str, int len);
  69. #ifdef __cplusplus
  70. }
  71. #endif
  72. #endif //LUA_REGEX_H