diff --git a/re/Ubuntu/makefile b/re/Ubuntu/makefile new file mode 100644 index 0000000000000000000000000000000000000000..4acf5cf1c43b3f57c8997c0d76c58fb6c6263800 --- /dev/null +++ b/re/Ubuntu/makefile @@ -0,0 +1,10 @@ +CFLAGS = -O2 -std=c99 -DELS_CONF_OS_LINUX -I ../buildenv -L ../buildenv -fPIC -s -l:libeasylosu.so +CPPFLAGS = -O2 -std=c++11 -DELS_CONF_OS_LINUX -I ../buildenv -L ../buildenv -l:libeasylosu.so + +re: + gcc cre.c -c $(CFLAGS) + g++ re.cpp -c $(CPPFLAGS) + gcc -o ElsLib_re.lsd *.o -shared $(CFLAGS) -lstdc++ + +clean: + rm ./*.o diff --git a/re/Windows/makefile b/re/Windows/makefile new file mode 100644 index 0000000000000000000000000000000000000000..b89d02c5a9f22761f8dcfa7d6ad1bdb87179bae6 --- /dev/null +++ b/re/Windows/makefile @@ -0,0 +1,10 @@ +CFLAGS = -O2 -std=c99 -DELS_CONF_OS_WINDOWS -I ../buildenv -L ../buildenv -fPIC -s -l:libeasylosu.dll +CPPFLAGS = -O2 -std=c++11 -DELS_CONF_OS_WINDOWS -I ../buildenv -L ../buildenv -l:libeasylosu.dll + +re: + gcc cre.c -c $(CFLAGS) + g++ re.cpp -c $(CPPFLAGS) + gcc -o ElsLib_re.lsd *.o -shared $(CFLAGS) -lstdc++ + +clean: + rm ./*.o diff --git a/re/cre.c b/re/cre.c new file mode 100644 index 0000000000000000000000000000000000000000..500510d597813a0315c2c6882240658c52568113 --- /dev/null +++ b/re/cre.c @@ -0,0 +1,32 @@ +#include "cre.h" +#include "re.h" + +int ELSAPI_re_re_match(els_VmObj *vm) +{ + const char *target = arg_getstr(vm, unit_arg(1)); + const char *p = arg_getstr(vm, unit_arg(2)); + int output = re_match(target, p); + if (output == 1) + arg_returntrue(vm); + else + arg_returnfalse(vm); + return 1; +} + +int ELSAPI_re_re_search(els_VmObj *vm) +{ + const char *target = arg_getstr(vm, unit_arg(1)); + const char *p = arg_getstr(vm, unit_arg(2)); + arg_return(vm, re_search(target, p, vm)); + return 1; +} + +int ELSAPI_re_re_replace(els_VmObj *vm) +{ + const char *target = arg_getstr(vm, unit_arg(1)); + const char *p = arg_getstr(vm, unit_arg(2)); + const char *r = arg_getstr(vm, unit_arg(3)); + LsString output = re_replace(target, p, r, vm); + arg_return(vm, output); + return 1; +} diff --git a/re/cre.h b/re/cre.h new file mode 100644 index 0000000000000000000000000000000000000000..d36572c3f7c4d75759cb8875500673aa7d014279 --- /dev/null +++ b/re/cre.h @@ -0,0 +1,10 @@ +#ifndef __XKIT_RE_H__ +#define __XKIT_RE_H__ + +#include "cxkit.h" + +XKIT_API int re_match(const char *targert, const char *p); +XKIT_API LsUnit re_search(const char *targert, const char *p, els_VmObj *vm); +XKIT_API LsString re_replace(const char *targert, const char *p, const char *replace_string, els_VmObj *vm); + +#endif \ No newline at end of file diff --git a/re/cxkit.h b/re/cxkit.h new file mode 100644 index 0000000000000000000000000000000000000000..c25334eba91c98a440ab2ffdf9e127c4a7c9753a --- /dev/null +++ b/re/cxkit.h @@ -0,0 +1,71 @@ +#ifndef __xkit_cxkit_h__ +#define __xkit_cxkit_h__ + +#include +#include + +//包含els.h +#ifdef __cplusplus +extern "C" { + #include "els.h" +} +#else +#include "els.h" +#endif + +//用于unit内函数的处理宏 +#define unit_arg(x) x+1 +#define unit_argtype(vm, x) arg_gettype(vm, x+1) +#define unit_argisnum(vm, x) arg_gettype(vm, x+1) == ELS_API_TYPE_NUMBER +#define unit_argisstr(vm, x) arg_gettype(vm, x+1) == ELS_API_TYPE_STRING +#define unit_argisunit(vm, x) arg_gettype(vm, x+1) == ELS_API_TYPE_UNIT +#define unit_argisptr(vm, x) arg_gettype(vm, x+1) == ELS_API_TYPE_ptr +#define unit_argisbyte(vm, x) arg_gettype(vm, x+1) == ELS_API_TYPE_byte +#define unit_argisfunc(vm, x) arg_gettype(vm, x+1) == ELS_API_TYPE_function + +//unit操作宏 +#define unit_getval(vm, u, s) obj_indexunitbystr(vm, u, (char*)s) +#define unit_addfunc(vm, u, s, f) obj_setunitbystr(vm, u, s, obj_newfunction(vm, (els_C_API_function)f)) + +//API接口标识 +#ifdef __cplusplus +#define XKIT_API extern "C" +#else +#define XKIT_API extern +#endif + +//LosuObj的别称 +typedef LosuObj LsUnit; +typedef LosuObj LsNumber; +typedef LosuObj LsString; +typedef LosuObj LsByte; +typedef LosuObj LsPoiter; +typedef LosuObj LsFunction; + +//返回值的bool处理 +#define arg_returntrue(vm) arg_return(vm, obj_newnum(vm, 1)) +#define arg_returnfalse(vm) arg_returnnull(vm) + +//关于gbk和utf8编码的输入输出宏 +#ifdef _WIN32 +#define outstr(vm, x) (char*)obj_toGBK(vm, (char*)x) +#define instr(vm, x) (char*)obj_toUTF8(vm, (char*)x) +#endif +#ifdef __linux__ +#define outstr(vm, x) (char*)x +#define instr(vm, x) (char*)x +#endif + +//操作系统宏开关 +#ifdef __linux__ +#ifndef ELS_CONF_OS_LINUX +#define ELS_CONF_OS_LINUX +#endif +#endif +#ifdef _WIN32 +#ifndef ELS_CONF_OS_WINDOWS +#define ELS_CONF_OS_WINDOWS +#endif +#endif + +#endif diff --git a/re/info b/re/info new file mode 100644 index 0000000000000000000000000000000000000000..37523f008c21ba97091b629c942d442c17ee8ce3 --- /dev/null +++ b/re/info @@ -0,0 +1,24 @@ +{ + type = "pre", + info = { + name = "re", + text = "正则表达式模块", + version = "24.3.10" + }, + source = { + all = { + "cre.c", + "cre.h", + "cxkit.h", + "re.cpp", + "re.els" + "re.h", + }, + Ubuntu = {"makefile"}, + Windows = {"makefile"} + }, + target = { + Ubuntu = "make ; cp ElsLib_re.lsd target/ElsLib_re.lsd_Ubuntu ; make clean", + Windows = "make ; cp ElsLib_re.lsd target/ElsLib_re.lsd_Windows ; make clean", + } +} \ No newline at end of file diff --git a/re/re.cpp b/re/re.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44a4ddbe4727cd54b7eb263847919424dc9cf48f --- /dev/null +++ b/re/re.cpp @@ -0,0 +1,45 @@ +#include +#include + +#include "cre.h" + +using namespace std; +using namespace __cxx11; + +XKIT_API int re_match(const char *targert, const char *p) +{ + smatch results; + string _t = targert; + string _p = p; + return regex_match(_t, results, regex(_p)); +} + +XKIT_API LsUnit re_search(const char *targert, const char *p, els_VmObj *vm) +{ + smatch results; + string _t = targert; + regex pattern(p); + LsUnit output = obj_newunit(vm); + string::const_iterator iter = _t.begin(); + string::const_iterator iterEnd = _t.end(); + + for (uint64_t i = 0; regex_search(iter, iterEnd, results, pattern);) + { + string tmp = results[0]; + i ++; + obj_setunitbynum(vm, output, i, obj_newstr(vm, (char*)tmp.c_str())); + iter = results[0].second; + } + + return output; +} + +XKIT_API LsString re_replace(const char *targert, const char *p, const char *replace_string, els_VmObj *vm) +{ + smatch results; + string _t = targert; + string _p = p; + string _r = replace_string; + string output = regex_replace(_t, regex(_p), _r); + return obj_newstr(vm, (char*)output.c_str()); +} diff --git a/re/re.els b/re/re.els new file mode 100644 index 0000000000000000000000000000000000000000..ed1cbe24fa7a7a2b841356b399b046e2f960001b --- /dev/null +++ b/re/re.els @@ -0,0 +1,15 @@ +#!declare +re_match() +re_search() +re_replace() +#!end + +#!script + +var re = { + match = re_match, + search = re_search, + replace = re_replace +} + +#!end \ No newline at end of file diff --git a/re/re.h b/re/re.h new file mode 100644 index 0000000000000000000000000000000000000000..34ebdc8aecdebc2bccb275478d31993fcfa64d62 --- /dev/null +++ b/re/re.h @@ -0,0 +1,23 @@ +#include "els.h" +int ELSAPI_re_re_match(els_VmObj* vm); // re_match() +int ELSAPI_re_re_search(els_VmObj* vm); // re_search() +int ELSAPI_re_re_replace(els_VmObj* vm); // re_replace() +#ifdef ELS_CONF_TOKEN_EN +static const char LibScript[]={ +10, +118,97,114,32,114,101,32,61,32,123,10, +32,32,32,32,109,97,116,99,104,32,61,32,114,101,95,109,97,116,99,104,44,10, +32,32,32,32,115,101,97,114,99,104,32,61,32,114,101,95,115,101,97,114,99,104,44,10, +32,32,32,32,114,101,112,108,97,99,101,32,61,32,114,101,95,114,101,112,108,97,99,101,10, +125,10, +10, +0}; +#endif +void ElsLib_re_libinit(els_VmObj *vm){ + vm_register(vm,"re_match",ELSAPI_re_re_match); + vm_register(vm,"re_search",ELSAPI_re_re_search); + vm_register(vm,"re_replace",ELSAPI_re_re_replace); + #ifdef ELS_CONF_TOKEN_EN + vm_dostring(vm,LibScript); + #endif +}; diff --git a/re/readme.md b/re/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..c181fec81e2e1a8db1b53ea32a3b37f3cadfce24 --- /dev/null +++ b/re/readme.md @@ -0,0 +1,24 @@ +# re中文文档 + +re是一个正则表达式模块。该模块基于C++的regex模块。 + +# 例程 + +```lua +var s = "aabbccaa" +var res = re.replace(s, "aa", "yyy") +print(res) +``` + +# 函数 + +## re.match(s:str, pattern:str) +检查字符串s是否符合pattern指定的模式。是则返回1,否则返回null。 + +## re.search(s:str, pattern:str) +搜索字符串s中所有符合pattern的子字符串,并储存在一个unit中返回。 + +返回值的索引从1开始,如果没有找到任何子字符串,则返回空unit。 + +## re.repalce(a:str, pattern:str, b:str) +将字符串a中所有符合pattern的子字符串都替换为b。返回替换后的字符串。