X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fregex.cpp;h=a1babf46d019b77a1db433a92ed447a0fbd2d6a2;hp=839435ef3bf62fd9c95134efd005e97d307dc011;hb=e3c7585b472744c6101bb714022f7545bb650493;hpb=381c89769c0ffaa14e6a92c662323b5c45e1eba3 diff --git a/source/regex.cpp b/source/regex.cpp index 839435e..a1babf4 100644 --- a/source/regex.cpp +++ b/source/regex.cpp @@ -1,12 +1,18 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2007 Mikko Rasa +Distributed under the LGPL +*/ + #include -#include +#include +#include #include "formatter.h" #include "regex.h" using namespace std; -#include - namespace { /** @@ -240,8 +246,7 @@ Regex::Code Regex::parse_atom(const string &expr, string::const_iterator &i, uns if(flag) { - if(static_cast(*i)<=LAST_INSTRUCTION_) - result+=MATCH_CHAR; + result+=MATCH_CHAR; result+=*i; } @@ -303,14 +308,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 +389,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(*j->citer); - if(instr>LAST_INSTRUCTION_) - instr=MATCH_CHAR; - else - ++j->citer; + Instruction instr=static_cast(*j->citer++); if(instr==NEGATE) negate_match=true; @@ -496,7 +497,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 +514,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(*i); - if(instr>=LAST_INSTRUCTION_) - instr=MATCH_CHAR; - else - ++i; + Instruction instr=static_cast(*i++); ostringstream result; switch(instr) @@ -571,8 +568,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 "<