X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fregex.cpp;h=c2590f2c8f8d9109606d06499da24b6b51c87e80;hp=d3fd0b7ebc4e58b733947387a6e4f349e83bd7ce;hb=5c8c0d5e52bbba8dc3f9c7c71b6e4c973216c600;hpb=b254c706617223da1dd1b9543a74715e42a8a5b0 diff --git a/source/regex.cpp b/source/regex.cpp index d3fd0b7..c2590f2 100644 --- a/source/regex.cpp +++ b/source/regex.cpp @@ -4,7 +4,9 @@ This file is part of libmspstrings Copyright © 2007 Mikko Rasa Distributed under the LGPL */ + #include +#include #include #include "formatter.h" #include "regex.h" @@ -84,11 +86,21 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns bool has_branches=false; unsigned level=0; bool escape=false; + unsigned bracket=0; string::const_iterator end; for(end=iter; end!=expr.end(); ++end) { if(escape) escape=false; + else if(bracket) + { + if(bracket==3 && *end==']') + bracket=0; + else if(bracket==1 && *end=='^') + bracket=2; + else + bracket=3; + } else if(*end=='\\') escape=true; else if(*end=='(') @@ -111,6 +123,8 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns else if(level==0) has_branches=true; } + else if(*end=='[') + bracket=1; } if(level>0) @@ -244,8 +258,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; } @@ -307,7 +320,7 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite } string::const_iterator end=iter; - for(; (end!=str.end() && (end==iter || *end!=']')); ++end); + for(; (end!=str.end() && (end==iter || *end!=']')); ++end) ; if(end==str.end()) throw InvalidParameterValue("Unmatched '['"); @@ -388,11 +401,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; @@ -517,11 +526,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) @@ -575,8 +580,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 "<