src/map/script.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/src/map/script.c b/src/map/script.c
index bba771a..8cc6033 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -46,6 +46,10 @@
#include "elemental.h"
#include "../config/core.h"
+#ifdef PCRE_SUPPORT
+#include "../../3rdparty/pcre/include/pcre.h" // regexp
+#endif
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18560,10 +18564,46 @@ bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)
return script->add_builtin(&buildin, true);
}
+BUILDIN(regexp) {
+#ifdef PCRE_SUPPORT
+ pcre *re;
+ pcre_extra *pcreExtra;
+ const char *error;
+ int erroffset, r, offset = 0;
+ int subStrVec[30];
+
+ const char* pattern = script_getstr(st,2);
+ const char* subject = script_getstr(st,3);
+ if (script_hasdata(st,4))
+ offset = script_getnum(st,4);
+
+ re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
+ pcreExtra = pcre_study(re, 0, &error);
+
+ r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
+
+ pcre_free(re);
+ if (pcreExtra != NULL)
+ pcre_free(pcreExtra);
+
+ if (r < 0)
+ script_pushint(st,0);
+ else
+ script_pushint(st,(r > 0) ? r : 30 / 3);
+
+ return true;
+#else
+ ShowError("script:regexp: cannot run without PCRE library enabled.\n");
+ script_pushint(st,0);
+ return false;
+#endif
+}
+
#define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
#define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
void script_parse_builtin(void) {
struct script_function BUILDIN[] = {
+ BUILDIN_DEF(regexp,"ss?"),
// NPC interaction
BUILDIN_DEF(mes,"s*"),
BUILDIN_DEF(next,""),