From: Mikko Rasa Date: Fri, 10 Aug 2007 19:43:55 +0000 (+0000) Subject: Fix a bug in Regex with brackets X-Git-Tag: strings-1.0~9 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=df1a66180655104af15f9bb6067b4eaa1421f5ff;p=libs%2Fcore.git Fix a bug in Regex with brackets Make the disassembly output of grep optional --- diff --git a/grep.cpp b/grep.cpp index 8c369a5..eaf1663 100644 --- a/grep.cpp +++ b/grep.cpp @@ -1,6 +1,7 @@ /* $Id$ */ #include #include +#include #include "regex.h" using namespace std; @@ -8,21 +9,27 @@ using namespace Msp; int main(int argc, char **argv) { - if(argc<2) + bool debug=false; + GetOpt getopt; + getopt.add_option('d', "debug", debug, GetOpt::NO_ARG); + getopt(argc, argv); + + const list &args=getopt.get_args(); + + if(args.empty()) { cerr<<"Usage: "<\n"; return 1; } Regex regex(argv[1]); - cout< - namespace { /** @@ -309,14 +307,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; @@ -502,7 +500,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; diff --git a/source/regex.h b/source/regex.h index 31b66d2..3750025 100644 --- a/source/regex.h +++ b/source/regex.h @@ -54,7 +54,7 @@ the input string, respectively. They do not consume input. The MATCH_CHAR instruction consumes the input character and matches it against a single character. Since regexes often match sequences of printable character, -a match for such a character may be encoded as the character itself. +a match for a non-opcode character may be encoded as the character itself. The MATCH_RANGE instruction consumes the input character and matches it against an inclusive character range.