Ver código fonte

refactor & feat: 调整Regex架构

SongZihuan 3 anos atrás
pai
commit
0a405d3bc3

+ 1 - 1
include/core/inter.h

@@ -37,7 +37,7 @@ namespace aFuncore {
         friend Activation::Activation(Inter *inter_);
 
         struct LiteralRegex {
-            Regex *rg;
+            Regex rg;
             std::string pattern;  // 派生 LiteralRegex 时使用
             std::string literaler;  // 调用的函数
             bool in_protect;  // 是否在protect空间

+ 2 - 2
include/tool/dlc.template.h

@@ -15,8 +15,8 @@ namespace aFuntool {
      */
     template <typename SYMBOL>
     class DlcSymbol {
-        const SYMBOL *symbol;
-        const DlcHandle *dlc = nullptr;
+        SYMBOL *symbol;
+        DlcHandle *dlc = nullptr;
     public:
         /**
          * 从句柄和符号指针创建一个符号

+ 4 - 1
include/tool/regex.h

@@ -15,7 +15,10 @@ namespace aFuntool {
     public:
         explicit Regex(const std::string &pattern_);
         Regex(const Regex &regex);
-        Regex &operator=(const Regex &regex);
+        Regex(Regex &&regex) noexcept;
+        Regex &operator=(const Regex &regex)=delete;
+        Regex &operator=(Regex &&regex)=delete;
+
         ~Regex ();
         int match(const char *subject);
         int match(const std::string &subject);

+ 4 - 0
include/tool/regex.inline.h

@@ -7,6 +7,10 @@ namespace aFuntool {
     inline int Regex::match(const std::string &subject){
         return match(subject.c_str());
     }
+
+    inline Regex::Regex(Regex &&regex) noexcept : pattern {std::move(regex.pattern)}, re {regex.re} {
+        regex.re = nullptr;
+    }
 }
 
 #endif //AFUN_REGEX_INLINE_H

+ 5 - 6
src/core/inter.cpp

@@ -51,11 +51,11 @@ Inter::Inter(int argc, char **argv, ExitMode em) : base{this}, is_derive{false}
 Inter::~Inter(){
     if (!is_derive) {
         delete global_varlist;
+
         Object::destruct(gc->obj);
         Var::destruct(gc->var);
         VarSpace::destruct(gc->varspace);
-        for (auto &it : *literal)
-            delete it.rg;
+
         delete literal;
         delete gc;
         delete son_inter;
@@ -143,7 +143,7 @@ bool Inter::checkLiteral(const std::string &element) const {
 
     for(NULL;it != end;it++){
         try {
-            if (it->rg->match(element) != 1)
+            if (it->rg.match(element) != 1)
                 continue;
             return true;
         } catch (RegexException &e) {
@@ -169,7 +169,7 @@ bool Inter::checkLiteral(const std::string &element, std::string &literaler, boo
 
     for(NULL;it != end;it++){
         try {
-            if (it->rg->match(element) != 1)
+            if (it->rg.match(element) != 1)
                 continue;
             literaler = it->literaler;
             in_protect = it->in_protect;
@@ -183,8 +183,7 @@ bool Inter::checkLiteral(const std::string &element, std::string &literaler, boo
 
 bool Inter::pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect){
     try {
-        auto rg =  new Regex(pattern);
-        literal->push_front({rg, pattern, literaler, in_protect});
+        literal->push_front({Regex(pattern), pattern, literaler, in_protect});
     } catch (RegexException &e) {
         return false;
     }

+ 0 - 20
src/tool/regex.cpp

@@ -40,26 +40,6 @@ Regex::Regex(const Regex &regex){
     }
 }
 
-Regex &Regex::operator=(const Regex &regex){
-    if (this == &regex)
-        return *this;
-
-    this->~Regex();
-
-    int error_code;
-    size_t erroroffset;
-    char regex_error[REGEX_ERROR_SIZE];
-
-    this->re = pcre2_compile((PCRE2_SPTR)regex.pattern.c_str(), PCRE2_ZERO_TERMINATED, 0, &error_code, &erroroffset, nullptr);
-    if (re == nullptr) {
-        PCRE2_UCHAR buffer[256];
-        pcre2_get_error_message(error_code, buffer, sizeof(buffer));
-        snprintf(regex_error, sizeof(regex_error), "R%d: %s\n", (int) erroroffset, buffer);
-        throw RegexException(regex_error);
-    }
-    return *this;
-}
-
 aFuntool::Regex::~Regex() {
     if (re != nullptr)
         pcre2_code_free(re);

+ 6 - 8
test/src/tool-regex.cpp

@@ -4,10 +4,9 @@
 using namespace aFuntool;
 
 int main() {
-    Regex *rg = new Regex("Hello嘿.*d");
-    int rc1 = rg->match("Hello嘿World");
-    int rc2 = rg->match("Nossss");
-    delete rg;
+    Regex rg {"Hello嘿.*d"};
+    int rc1 = rg.match("Hello嘿World");
+    int rc2 = rg.match("Nossss");
 
     if (rc1 != 1 || rc2 != 0) {
         printf("Failed rg1: %d/1, %d/0\n", rc1, rc2);
@@ -15,10 +14,9 @@ int main() {
     } else
         printf("rg1 success\n");
 
-    Regex *rg2 = new Regex("你|好");
-    int rc3 = rg2->match("你");
-    int rc4 = rg2->match("Nosssss");
-    delete rg2;
+    Regex rg2 {"你|好"};
+    int rc3 = rg2.match("你");
+    int rc4 = rg2.match("Nosssss");
 
     if (rc3 != 1 || rc4 != 0) {
         printf("Failed rg2: %d/1, %d/0\n", rc1, rc2);