SilcRegex: Fixed silc_regex to return correct value for optional args
[runtime.git] / lib / silcutil / silcregex.c
index dba552b7e34b9fb976fcc9b893b6dec6d18a0267..3c83ed783d693149003ffa7cdaa47564c834952e 100644 (file)
@@ -36,7 +36,7 @@
   - SilcStack support         compile/match without real memory allocations
 */
 
-#include "silc.h"
+#include "silcruntime.h"
 
 #define RE_NREGS       128     /* number of registers available */
 
@@ -2315,7 +2315,7 @@ SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
     regexp->rstack = silc_stack_alloc(512, regexp->rstack);
 
   /* Compile */
-  ret = silc_re_compile_pattern((char *)regex, strlen(regex), regexp);
+  ret = silc_re_compile_pattern((unsigned char *)regex, strlen(regex), regexp);
   if (ret != SILC_OK)
     silc_set_errno(ret);
 
@@ -2359,8 +2359,8 @@ SilcBool silc_regex_match(SilcRegex regexp, const char *string,
     f |= RE_NOTEOL;
 
   /* Search */
-  ret = silc_re_search(regexp, (char *)string, string_len, 0, string_len,
-                      num_match ? &regs : NULL, f);
+  ret = silc_re_search(regexp, (unsigned char *)string, string_len, 0,
+                      string_len, num_match ? &regs : NULL, f);
   if (ret < 0) {
     if (ret == -1)
       silc_set_errno(SILC_ERR_NOT_FOUND);
@@ -2411,6 +2411,7 @@ SilcBool silc_regex_va(const char *string, SilcUInt32 string_len,
       silc_regex_free(&reg);
       return FALSE;
     }
+    silc_buffer_set(match, NULL, 0);
     rets[c++] = match;
 
     while ((buf = va_arg(va, SilcBuffer))) {
@@ -2421,6 +2422,7 @@ SilcBool silc_regex_va(const char *string, SilcUInt32 string_len,
        silc_regex_free(&reg);
        return FALSE;
       }
+      silc_buffer_set(buf, NULL, 0);
       rets[c++] = buf;
     }
 
@@ -2444,10 +2446,8 @@ SilcBool silc_regex_va(const char *string, SilcUInt32 string_len,
 
   /* Return matches */
   for (i = 0; i < c; i++) {
-    if (m[i].start == -1) {
-      silc_buffer_set(rets[i], NULL, 0);
+    if (m[i].start == -1)
       continue;
-    }
     silc_buffer_set(rets[i], (unsigned char *)string + m[i].start,
                    m[i].end - m[i].start);
   }