lua-regex.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. int start_pos; /* pattern match start position */
  54. int end_pos; /* pattern match end position */
  55. const char *error;
  56. int level; /* total number of captures (finished or unfinished) */
  57. LuaCapture capture[LUA_REGEX_MAXCAPTURES];
  58. } LuaMatchState;
  59. typedef int (*luaregex_func_param)(LuaMatchState *ms, void *udata, char_buffer_st **b);
  60. int str_find (LuaMatchState *ms, const char *s, int ls,
  61. const char *p, int lp, int init, int raw_find,
  62. luaregex_func_param fp, void *udata);
  63. int str_match (LuaMatchState *ms, const char *s, int ls,
  64. const char *p, int lp, int init, int raw_find,
  65. luaregex_func_param fp, void *udata);
  66. char_buffer_st *str_gsub (const char *src, int srcl, const char *p, int lp,
  67. const char *tr, int ltr, size_t max_s, const char **error_ptr,
  68. luaregex_func_param fp, void *udata);
  69. int char_buffer_add_char(LuaMatchState *ms, char_buffer_st **b, char c);
  70. int char_buffer_add_str(LuaMatchState *ms, char_buffer_st **b, const char *str, int len);
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif //LUA_REGEX_H