viewing paste topic/4855- regexp.diff | Diff

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 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,""),
 
Viewed 631 times, submitted by AnnieRuru.