Parcourir la source

Add escape char extension from https://github.com/jcgoble3/lua-matchext

mingodad il y a 9 ans
Parent
commit
431f12d930
1 fichiers modifiés avec 10 ajouts et 6 suppressions
  1. 10 6
      SquiLu/squirrel/lua-regex.c

+ 10 - 6
SquiLu/squirrel/lua-regex.c

@@ -29,6 +29,7 @@ http://code.google.com/p/lua-regex-standalone/
 #include <stdio.h>
 #include <stdio.h>
 #include <string.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdlib.h>
+#include <limits.h>
 #include <ctype.h>
 #include <ctype.h>
 #include <inttypes.h>
 #include <inttypes.h>
 
 
@@ -158,10 +159,11 @@ static int singlematch (int c, const char *p, const char *ep) {
 
 
 static const char *match (LuaMatchState *ms, const char *s, const char *p);
 static const char *match (LuaMatchState *ms, const char *s, const char *p);
 
 
-
+//add escape char extension from https://github.com/jcgoble3/lua-matchext
 static const char *matchbalance (LuaMatchState *ms, const char *s,
 static const char *matchbalance (LuaMatchState *ms, const char *s,
                                    const char *p) {
                                    const char *p) {
-  if (p >= ms->p_end - 1){
+  int escaped = (*(p-1) == 'B'); /* EXT */
+  if (p >= ms->p_end - 1 - escaped){
     ms->error = "malformed pattern "
     ms->error = "malformed pattern "
                       "(missing arguments to " LUA_QL("%%b") ")";
                       "(missing arguments to " LUA_QL("%%b") ")";
     return NULL;
     return NULL;
@@ -169,10 +171,12 @@ static const char *matchbalance (LuaMatchState *ms, const char *s,
   if (*s != *p) return NULL;
   if (*s != *p) return NULL;
   else {
   else {
     int b = *p;
     int b = *p;
-    int e = *(p+1);
+    int e = *(p + (escaped ? 2 : 1));  /* EXT */
+    int esc = escaped ? *(p + 1) : INT_MAX;  /* EXT */
     int cont = 1;
     int cont = 1;
     while (++s < ms->src_end) {
     while (++s < ms->src_end) {
-      if (*s == e) {
+      if (*s == esc) s++; /* EXT */
+      else if (*s == e) {
         if (--cont == 0) return s+1;
         if (--cont == 0) return s+1;
       }
       }
       else if (*s == b) cont++;
       else if (*s == b) cont++;
@@ -272,10 +276,10 @@ static const char *match (LuaMatchState *ms, const char *s, const char *p) {
     }
     }
     case L_ESC: {  /* escaped sequences not in the format class[*+?-]? */
     case L_ESC: {  /* escaped sequences not in the format class[*+?-]? */
       switch (*(p+1)) {
       switch (*(p+1)) {
-        case 'b': {  /* balanced string? */
+        case 'b': case 'B': { /* balanced string? */ /* EXT */
           s = matchbalance(ms, s, p+2);
           s = matchbalance(ms, s, p+2);
           if (s == NULL) return NULL;
           if (s == NULL) return NULL;
-          p+=4; goto init;  /* else return match(ms, s, p+4); */
+          p += (*(p + 1) == 'b') ? 4 : 5; /* EXT */ goto init;  /* else return match(ms, s, p+4); */
         }
         }
         case 'f': {  /* frontier? */
         case 'f': {  /* frontier? */
           const char *ep; char previous;
           const char *ep; char previous;