ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / extras / Regex.ec
index dab7d5e..1e9085c 100644 (file)
@@ -1,6 +1,8 @@
 
 #define __restrict
+#ifndef __restrict_arr
 #define __restrict_arr
+#endif
 
 #define uint _uint
 #ifdef __WIN32__
@@ -23,7 +25,7 @@ public import "ecere"
 public class Regex : struct
 {
 public:
-   property String regex
+   property const String regex
    {
       set
       {
@@ -31,15 +33,14 @@ public:
          {
             if(value)
             {
-               if(regex)
-                  delete regex;
+               delete regex;
                regex = CopyString(value);
                Compile();
             }
             else
                Free();
          }
-         
+
       }
       get { return regex; }
    }
@@ -48,9 +49,9 @@ public:
    {
       set
       {
-         if(value != (compileFlags & REG_ICASE))
+         if(value != ((compileFlags & REG_ICASE) != 0))
          {
-            compileFlags = value ? compileFlags | REG_ICASE : compileFlags & ~REG_ICASE;
+            value ? (compileFlags |= REG_ICASE) : (compileFlags &= ~REG_ICASE);
             if(regex)
                Compile();
          }
@@ -62,10 +63,13 @@ public:
    {
       set
       {
-         if(value != (compileFlags & REG_NEWLINE))
-            compileFlags = value ? compileFlags | REG_NEWLINE : compileFlags & ~REG_NEWLINE;
-         if(regex)
-            Compile();
+         //if(value != ((compileFlags & REG_NEWLINE) != 0))
+         if(value != ((compileFlags & REG_EXTENDED)))
+         {
+            value ? (compileFlags |= REG_NEWLINE) : (compileFlags &= ~REG_NEWLINE);
+            if(regex)
+               Compile();
+         }
       }
       get { return (compileFlags & REG_NEWLINE) != 0; }
    }
@@ -74,8 +78,8 @@ public:
    {
       set
       {
-         if(value != (executeFlags & REG_NOTBOL))
-            executeFlags = value ? executeFlags | REG_NOTBOL : executeFlags & ~REG_NOTBOL;
+         if(value != ((executeFlags & REG_NOTBOL) != 0))
+            value ? (executeFlags |= REG_NOTBOL) : (executeFlags &= ~REG_NOTBOL);
       }
       get { return (executeFlags & REG_NOTBOL) != 0; }
    }
@@ -84,8 +88,8 @@ public:
    {
       set
       {
-         if(value != (executeFlags & REG_NOTEOL))
-            executeFlags = value ? executeFlags | REG_NOTEOL : executeFlags & ~REG_NOTEOL;
+         if(value != ((executeFlags & REG_NOTEOL) != 0))
+            value ? (executeFlags |= executeFlags | REG_NOTEOL) : (executeFlags &= ~REG_NOTEOL);
       }
       get { return (executeFlags & REG_NOTEOL) != 0; }
    }
@@ -94,9 +98,9 @@ public:
    {
       set
       {
-         if(value != (compileFlags & REG_EXTENDED))
+         if(value != ((compileFlags & REG_EXTENDED) != 0))
          {
-            compileFlags = value ? compileFlags | REG_EXTENDED : compileFlags & ~REG_EXTENDED;
+            value ? (compileFlags |= REG_EXTENDED) : (compileFlags &= ~REG_EXTENDED);
             if(regex)
                Compile();
          }
@@ -122,13 +126,13 @@ public:
 
    property int matchCount { get { return matchCount; } }
 
-   char * Match(String string)
+   char * Match(const String string)
    {
       if(valid)
       {
          int c;
          int result;
-         result = regexec(&compiledRegex, string, maxMatchCount, matches, executeFlags); 
+         result = regexec(&compiledRegex, string, maxMatchCount, matches, executeFlags);
          if(result == 0) // != REG_NOMATCH
          {
             for(c = 0; c < maxMatchCount; c++)
@@ -140,8 +144,8 @@ public:
                }
             }
             if(c == maxMatchCount)
-               matchCount = maxMatchCount;      
-            return string + matches[0].rm_so;
+               matchCount = maxMatchCount;
+            return (char *)string + matches[0].rm_so;
          }
          else
             matchCount = 0;
@@ -153,7 +157,7 @@ public:
    {
       return matches[matchPos].rm_so;
    }
-   
+
    int GetMatchEndOffset(int matchPos)
    {
       return matches[matchPos].rm_eo;
@@ -175,6 +179,7 @@ private:
 
    void Free()
    {
+      delete matches;
       delete regex;
       regfree(&compiledRegex);
       valid = false;
@@ -189,22 +194,22 @@ private:
       valid = result == 0;
       if(valid && !maxMatchCount)
          property::maxMatchCount = 1;
-      
+
       // TODO: handle errors?
-      // size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size); 
-      // REG_BADBR      Invalid use of back reference operator. 
-      // REG_BADPAT     Invalid use of pattern operators such as group or list. 
-      // REG_BADRPT     Invalid use of repetition operators such as using '*' as the first character. 
-      // REG_EBRACE     Un-matched brace interval operators. 
-      // REG_EBRACK     Un-matched bracket list operators. 
-      // REG_ECOLLATE   Invalid collating element. 
-      // REG_ECTYPE     Unknown character class name. 
-      // REG_EEND       Non specific error. This is not defined by POSIX.2. 
-      // REG_EESCAPE    Trailing backslash. 
-      // REG_EPAREN     Un-matched parenthesis group operators. 
-      // REG_ERANGE     Invalid use of the range operator, eg. the ending point of the range occurs prior to the starting point. 
-      // REG_ESIZE      Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2. 
-      // REG_ESPACE     The regex routines ran out of memory. 
+      // size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
+      // REG_BADBR      Invalid use of back reference operator.
+      // REG_BADPAT     Invalid use of pattern operators such as group or list.
+      // REG_BADRPT     Invalid use of repetition operators such as using '*' as the first character.
+      // REG_EBRACE     Un-matched brace interval operators.
+      // REG_EBRACK     Un-matched bracket list operators.
+      // REG_ECOLLATE   Invalid collating element.
+      // REG_ECTYPE     Unknown character class name.
+      // REG_EEND       Non specific error. This is not defined by POSIX.2.
+      // REG_EESCAPE    Trailing backslash.
+      // REG_EPAREN     Un-matched parenthesis group operators.
+      // REG_ERANGE     Invalid use of the range operator, eg. the ending point of the range occurs prior to the starting point.
+      // REG_ESIZE      Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2.
+      // REG_ESPACE     The regex routines ran out of memory.
       // REG_ESUBREG    Invalid back reference to a subexpression.
    }