]> git.saurik.com Git - cydia.git/blobdiff - CyteKit/PerlCompatibleRegEx.hpp
Represent Sections as objects, not as strings. Also, a much cleaner implementation...
[cydia.git] / CyteKit / PerlCompatibleRegEx.hpp
index e1c0e2e7da8dc26ce9fdbc4abbe3c0cbb7b7265b..602f446c743572cdd07b62e3cd70530c448a52b5 100644 (file)
@@ -56,15 +56,20 @@ class Pcre {
   public:
     Pcre() :
         code_(NULL),
-        study_(NULL)
+        study_(NULL),
+        data_(NULL)
     {
     }
 
-    Pcre(const char *regex) :
+    Pcre(const char *regex, NSString *data = nil) :
         code_(NULL),
-        study_(NULL)
+        study_(NULL),
+        data_(NULL)
     {
         this->operator =(regex);
+
+        if (data != nil)
+            this->operator ()(data);
     }
 
     void operator =(const char *regex) {
@@ -80,6 +85,7 @@ class Pcre {
         }
 
         pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
+        _assert(capture_ >= 0);
         matches_ = new int[(capture_ + 1) * 3];
     }
 
@@ -102,8 +108,17 @@ class Pcre {
     }
 
     bool operator ()(const char *data, size_t size) {
-        data_ = data;
-        return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
+        if (pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0) {
+            data_ = data;
+            return true;
+        } else {
+            data_ = NULL;
+            return false;
+        }
+    }
+
+    operator bool() const {
+        return data_ != NULL;
     }
 
     NSString *operator ->*(NSString *format) const {