yet another helper function: remove matching term from query string

This commit is contained in:
Fischlurch 2008-04-06 08:56:18 +02:00
parent b361fc9672
commit b53d8655fd
5 changed files with 70 additions and 21 deletions

View file

@ -77,8 +77,16 @@ namespace lumiera
{
map<Symbol, regex> regexTable;
Symbol matchArgument = "\\(\\s*([\\w_\\.\\-]+)\\s*\\)";
Symbol matchArgument = "\\(\\s*([\\w_\\.\\-]+)\\s*\\),?\\s*";
regex findPredicate (string("(\\w+)")+matchArgument);
inline regex&
getTermRegex (Symbol sym)
{
if (!contains (regexTable, sym))
regexTable[sym] = regex (string(sym)+=matchArgument);
return regexTable[sym];
}
}
/** (preliminary) helper: instead of really parsing and evaluating the terms,
@ -90,17 +98,31 @@ namespace lumiera
const string
extractID (Symbol sym, const string& termString)
{
if (!contains (regexTable, sym))
regexTable[sym] = regex (string(sym)+=matchArgument);
smatch match;
if (regex_search (termString, match, regexTable[sym]))
return string (match[1]);
if (regex_search (termString, match, getTermRegex (sym)))
return (match[1]);
else
return "";
}
}
/** (preliminary) helper: cut a term with the given symbol.
* The term is matched, removed from the original string and returned
* @note parameter termString will be modified!
*/
const string
removeTerm (Symbol sym, string& termString)
{
smatch match;
if (regex_search (termString, match, getTermRegex (sym)))
{
string res (sym); res += "("+match[1]+")";
termString.erase (match.position(), match[0].length());
return res;
}
else
return "";
}
/** @note this is a very hackish preliminary implementation.

View file

@ -69,7 +69,6 @@ namespace lumiera
*/
void normalizeID (string& id);
const string extractID (Symbol, const string& termString);
/** count the top-level predicates in the query string.
* usable for ordering queries, as more predicates usually
@ -77,7 +76,12 @@ namespace lumiera
*/
uint countPraed (const string&);
const string extractID (Symbol, const string& termString);
const string removeTerm (Symbol, string& termString);
} // namespace query
} // namespace lumiera

View file

@ -18,6 +18,11 @@ return: 0
END
TEST "QueryUtils_test" QueryUtils_test removeTerm <<END
return: 0
END
TEST "QueryUtils_test" QueryUtils_test countPraed <<END
return: 0
END

View file

@ -1,5 +1,5 @@
/*
QUERYDIAGNOSTICS.hpp - herlpers for writing tests covering config queries
QUERYDIAGNOSTICS.hpp - helpers for writing tests covering config queries
Copyright (C) Lumiera.org
2008, Hermann Vosseler <Ichthyostega@web.de>
@ -25,20 +25,11 @@
#define LUMIERA_TEST_QUERY_QUERYDIAGNOSTICS_H
//#include "common/factory.hpp"
//#include "common/util.hpp"
//#include <boost/algorithm/string.hpp>
//#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
//#include <iostream>
#include <cstdlib>
//using boost::algorithm::join;
//using boost::lexical_cast;
using boost::format;
using std::string;
//using std::cout;
using std::rand;

View file

@ -68,11 +68,12 @@ namespace lumiera
virtual void
run (Arg arg)
{
if (isnil(arg)) arg = Cmdline ("Query normalizeID extractID countPraed");
if (isnil(arg)) arg = Cmdline ("Query normalizeID extractID removeTerm countPraed");
if (contains (arg, "Query" )) check_Query ();
if (contains (arg, "normalizeID")) check_normalizeID ();
if (contains (arg, "extractID" )) check_extractID ();
if (contains (arg, "removeTerm" )) check_removeTerm ();
if (contains (arg, "countPraed" )) check_countPraed ();
}
@ -122,6 +123,32 @@ namespace lumiera
/** @test the regexp based cutting of a term with given symbol */
void
check_removeTerm ()
{
// successfull-----Symbol---input-string----------------------extracted------remaining-------------
ASSERT_removeTerm ("pred", "pred(tok).", "pred(tok)", "." );
ASSERT_removeTerm ("pred", " pred( tok )", "pred(tok)", " " );
ASSERT_removeTerm ("pred", "pred(tok), pred(tux).", "pred(tok)", "pred(tux)." );
ASSERT_removeTerm ("pred", "other(xyz) pred(tok) pred(tux)", "pred(tok)", "other(xyz) pred(tux)" );
ASSERT_removeTerm ("pred", "some( pred(tok)", "pred(tok)", "some( " );
// not successfull
ASSERT_removeTerm ("pred", "pred (tok", "", "pred (tok" );
ASSERT_removeTerm ("pred", "pred tok)", "", "pred tok)" );
ASSERT_removeTerm ("pred", "pred(tok", "", "pred(tok" );
}
void
ASSERT_removeTerm (Symbol sym, string input, Symbol extracted, Symbol modified)
{
ASSERT (extracted == removeTerm (sym, input));
ASSERT (modified == input);
}
/** @test counting of predicates in a quiery
* (currently 4/08 regexp based...)
*/