]> git.tdb.fi Git - libs/core.git/blobdiff - source/regex.cpp
Remove the "optimization" of omitting MATCH_CHAR instruction for characters >LAST_INS...
[libs/core.git] / source / regex.cpp
index 839435ef3bf62fd9c95134efd005e97d307dc011..48fe4a7c3422fb027c40481d1f7d39b8fc7010d3 100644 (file)
@@ -1,12 +1,16 @@
+/* $Id$
+
+This file is part of libmspstrings
+Copyright © 2007 Mikko Rasa
+Distributed under the LGPL
+*/
 #include <stack>
-#include <msp/core/error.h>
+#include <msp/core/except.h>
 #include "formatter.h"
 #include "regex.h"
 
 using namespace std;
 
-#include <iostream>
-
 namespace {
 
 /**
@@ -240,8 +244,7 @@ Regex::Code Regex::parse_atom(const string &expr, string::const_iterator &i, uns
 
        if(flag)
        {
-               if(static_cast<unsigned char>(*i)<=LAST_INSTRUCTION_)
-                       result+=MATCH_CHAR;
+               result+=MATCH_CHAR;
                result+=*i;
        }
 
@@ -303,14 +306,14 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite
        }
 
        string::const_iterator end=iter;
-       for(; (end!=str.end() && *end!=']'); ++end);
+       for(; (end!=str.end() && (end==iter || *end!=']')); ++end);
        if(end==str.end())
                throw InvalidParameterValue("Unmatched '['");
 
        uint8_t mask[32]={0};
        unsigned type=0;
        bool range=false;
-       unsigned char first, last;
+       unsigned char first=0, last=0;
        for(string::const_iterator i=iter; i!=end; ++i)
        {
                unsigned char c=*i;
@@ -384,11 +387,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch
                        bool negate_match=false;
                        for(; j->citer!=code.end();)
                        {
-                               Instruction instr=static_cast<Instruction>(*j->citer);
-                               if(instr>LAST_INSTRUCTION_)
-                                       instr=MATCH_CHAR;
-                               else
-                                       ++j->citer;
+                               Instruction instr=static_cast<Instruction>(*j->citer++);
 
                                if(instr==NEGATE)
                                        negate_match=true;
@@ -496,7 +495,7 @@ bool Regex::group_compare(const RegMatch::Group &g1, const RegMatch::Group &g2)
 {
        if(!g1.match)
                return false;
-       
+
        // Any match is better than no match
        if(!g2.match)
                return true;
@@ -513,11 +512,7 @@ bool Regex::group_compare(const RegMatch::Group &g1, const RegMatch::Group &g2)
 
 string Regex::disassemble_instruction(Code::const_iterator &i) const
 {
-       Instruction instr=static_cast<Instruction>(*i);
-       if(instr>=LAST_INSTRUCTION_)
-               instr=MATCH_CHAR;
-       else
-               ++i;
+       Instruction instr=static_cast<Instruction>(*i++);
 
        ostringstream result;
        switch(instr)
@@ -571,8 +566,7 @@ string Regex::disassemble_instruction(Code::const_iterator &i) const
        case MATCH_ANY:
                result<<"MATCH_ANY";
                break;
-       case FIRST_INSTRUCTION_:
-       case LAST_INSTRUCTION_:
+       default:
                result<<"UNKNOWN "<<instr;
        }