regex_wrap.cpp 883 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <boost/cregex.hpp>
  2. extern "C"
  3. {
  4. #include "lua.h"
  5. #include "lauxlib.h"
  6. #include "lualib.h"
  7. }
  8. #include <luabind/luabind.hpp>
  9. namespace
  10. {
  11. bool match(boost::RegEx& r, const char* s)
  12. {
  13. return r.Match(s);
  14. }
  15. bool search(boost::RegEx& r, const char* s)
  16. {
  17. return r.Search(s);
  18. }
  19. } // namespace unnamed
  20. void wrap_regex(lua_State* L)
  21. {
  22. using boost::RegEx;
  23. using namespace luabind;
  24. module(L)
  25. [
  26. class_<RegEx>("regex")
  27. .def(constructor<const char*>())
  28. .def(constructor<const char*, bool>())
  29. .def("match", match)
  30. .def("search", search)
  31. .def("what", &RegEx::What)
  32. .def("matched", &RegEx::Matched)
  33. .def("length", &RegEx::Length)
  34. .def("position", &RegEx::Position)
  35. ];
  36. }
  37. int main()
  38. {
  39. lua_State* L = lua_open();
  40. lua_baselibopen(L);
  41. lua_strlibopen(L);
  42. luabind::open(L);
  43. wrap_regex(L);
  44. lua_dofile(L, "regex.lua");
  45. lua_close(L);
  46. }