mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-26 11:16:55 +00:00
Update for boost::regex v4, add test
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13306 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
0605e01f44
commit
bd9c55944f
@ -1,3 +1,9 @@
|
||||
2006-03-06 Lars Gullik Bjønnes <larsbj@lyx.org>
|
||||
|
||||
* biblio.C (escape_special_chars): Update for boost::regex v4
|
||||
(RegexMatch): Update for bosot::regex v4
|
||||
(searchKeys): Handle exception if it occurs.
|
||||
|
||||
2005-12-12 Jürgen Spitzmüller <j.spitzmueller@gmx.de>
|
||||
|
||||
* ControlPrefs.[Ch]: new helper functions toPaperSize and
|
||||
|
@ -370,24 +370,29 @@ namespace {
|
||||
// Escape special chars.
|
||||
// All characters are literals except: '.|*?+(){}[]^$\'
|
||||
// These characters are literals when preceded by a "\", which is done here
|
||||
// @todo: This function should be moved to support, and then the test in tests
|
||||
// should be moved there as well.
|
||||
string const escape_special_chars(string const & expr)
|
||||
{
|
||||
// Search for all chars '.|*?+(){}[^$]\'
|
||||
// Note that '[' and '\' must be escaped.
|
||||
// This is a limitation of boost::regex, but all other chars in BREs
|
||||
// are assumed literal.
|
||||
boost::RegEx reg("[].|*?+(){}^$\\[\\\\]");
|
||||
boost::regex reg("[].|*?+(){}^$\\[\\\\]");
|
||||
|
||||
// $& is a perl-like expression that expands to all of the current match
|
||||
// $& is a perl-like expression that expands to all
|
||||
// of the current match
|
||||
// The '$' must be prefixed with the escape character '\' for
|
||||
// boost to treat it as a literal.
|
||||
// Thus, to prefix a matched expression with '\', we use:
|
||||
return reg.Merge(expr, "\\\\$&");
|
||||
return boost::regex_replace(expr, reg, "\\\\$&");
|
||||
}
|
||||
|
||||
|
||||
// A functor for use with std::find_if, used to ascertain whether a
|
||||
// data entry matches the required regex_
|
||||
// @throws: boost::regex_error if the supplied regex pattern is not valid
|
||||
// @todo: This function should be moved to support.
|
||||
class RegexMatch : public std::unary_function<string, bool>
|
||||
{
|
||||
public:
|
||||
@ -397,9 +402,6 @@ public:
|
||||
: map_(m), regex_(re, icase) {}
|
||||
|
||||
bool operator()(string const & key) const {
|
||||
if (!validRE())
|
||||
return false;
|
||||
|
||||
// the data searched is the key + its associated BibTeX/biblio
|
||||
// fields
|
||||
string data = key;
|
||||
@ -409,14 +411,11 @@ public:
|
||||
|
||||
// Attempts to find a match for the current RE
|
||||
// somewhere in data.
|
||||
return regex_.Search(data);
|
||||
return boost::regex_search(data, regex_);
|
||||
}
|
||||
|
||||
bool validRE() const { return regex_.error_code() == 0; }
|
||||
|
||||
private:
|
||||
InfoMap const map_;
|
||||
mutable boost::RegEx regex_;
|
||||
mutable boost::regex regex_;
|
||||
};
|
||||
|
||||
} // namespace anon
|
||||
@ -442,27 +441,31 @@ searchKeys(InfoMap const & theMap,
|
||||
if (type == SIMPLE)
|
||||
// We must escape special chars in the search_expr so that
|
||||
// it is treated as a simple string by boost::regex.
|
||||
expr = escape_special_chars(expr);
|
||||
expr = escape_special_chars(expr);
|
||||
|
||||
// Build the functor that will be passed to find_if.
|
||||
RegexMatch const match(theMap, expr, !caseSensitive);
|
||||
if (!match.validRE())
|
||||
try {
|
||||
// Build the functor that will be passed to find_if.
|
||||
RegexMatch const match(theMap, expr, !caseSensitive);
|
||||
|
||||
// Search the vector of 'keys' from 'start' for one
|
||||
// that matches the predicate 'match'. Searching can
|
||||
// be forward or backward from start.
|
||||
if (dir == FORWARD)
|
||||
return std::find_if(start, keys.end(), match);
|
||||
|
||||
vector<string>::const_reverse_iterator rit(start);
|
||||
vector<string>::const_reverse_iterator rend = keys.rend();
|
||||
rit = std::find_if(rit, rend, match);
|
||||
|
||||
if (rit == rend)
|
||||
return keys.end();
|
||||
// This is correct and always safe.
|
||||
// (See Meyer's Effective STL, Item 28.)
|
||||
return (++rit).base();
|
||||
}
|
||||
catch (boost::regex_error & regerr) {
|
||||
return keys.end();
|
||||
|
||||
// Search the vector of 'keys' from 'start' for one that matches the
|
||||
// predicate 'match'. Searching can be forward or backward from start.
|
||||
if (dir == FORWARD)
|
||||
return std::find_if(start, keys.end(), match);
|
||||
|
||||
vector<string>::const_reverse_iterator rit(start);
|
||||
vector<string>::const_reverse_iterator rend = keys.rend();
|
||||
rit = std::find_if(rit, rend, match);
|
||||
|
||||
if (rit == rend)
|
||||
return keys.end();
|
||||
// This is correct and always safe.
|
||||
// (See Meyer's Effective STL, Item 28.)
|
||||
return (++rit).base();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
23
src/frontends/controllers/tests/Makefile.am
Normal file
23
src/frontends/controllers/tests/Makefile.am
Normal file
@ -0,0 +1,23 @@
|
||||
include $(top_srcdir)/config/common.am
|
||||
|
||||
EXTRA_DIST = pch.h test_biblio
|
||||
|
||||
BUILT_SOURCES = $(PCH_FILE)
|
||||
|
||||
TESTS = \
|
||||
test_biblio
|
||||
|
||||
check_PROGRAMS = \
|
||||
biblio
|
||||
|
||||
AM_CPPFLAGS += $(BOOST_INCLUDES)
|
||||
|
||||
biblio_LDADD = $(BOOST_REGEX)
|
||||
biblio_SOURCES = \
|
||||
biblio.C \
|
||||
boost.C
|
||||
|
||||
makeregfiles: ${check_PROGRAMS}
|
||||
for all in ${check_PROGRAMS} ; do \
|
||||
./$$all > ${srcdir}/regfiles/$$all ; \
|
||||
done
|
95
src/frontends/controllers/tests/biblio.C
Executable file
95
src/frontends/controllers/tests/biblio.C
Executable file
@ -0,0 +1,95 @@
|
||||
#include <config.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::string;
|
||||
|
||||
// Escape special chars.
|
||||
// All characters are literals except: '.|*?+(){}[]^$\'
|
||||
// These characters are literals when preceded by a "\", which is done here
|
||||
// This function is unfortunately copied from ../biblio.C, so we should
|
||||
// try to make sure to keep the two in sync.
|
||||
string const escape_special_chars(string const & expr)
|
||||
{
|
||||
// Search for all chars '.|*?+(){}[^$]\'
|
||||
// Note that '[' and '\' must be escaped.
|
||||
// This is a limitation of boost::regex, but all other chars in BREs
|
||||
// are assumed literal.
|
||||
boost::regex reg("[].|*?+(){}^$\\[\\\\]");
|
||||
|
||||
// $& is a perl-like expression that expands to all of the current match
|
||||
// The '$' must be prefixed with the escape character '\' for
|
||||
// boost to treat it as a literal.
|
||||
// Thus, to prefix a matched expression with '\', we use:
|
||||
return boost::regex_replace(expr, reg, "\\\\$&");
|
||||
}
|
||||
|
||||
|
||||
typedef std::map<string, string> InfoMap;
|
||||
|
||||
// A functor for use with std::find_if, used to ascertain whether a
|
||||
// data entry matches the required regex_
|
||||
// This class is unfortunately copied from ../biblio.C, so we should
|
||||
// try to make sure to keep the two in sync.
|
||||
class RegexMatch : public std::unary_function<string, bool>
|
||||
{
|
||||
public:
|
||||
// re and icase are used to construct an instance of boost::RegEx.
|
||||
// if icase is true, then matching is insensitive to case
|
||||
RegexMatch(InfoMap const & m, string const & re, bool icase)
|
||||
: map_(m), regex_(re, icase) {}
|
||||
|
||||
bool operator()(string const & key) const {
|
||||
// the data searched is the key + its associated BibTeX/biblio
|
||||
// fields
|
||||
string data = key;
|
||||
InfoMap::const_iterator info = map_.find(key);
|
||||
if (info != map_.end())
|
||||
data += ' ' + info->second;
|
||||
|
||||
// Attempts to find a match for the current RE
|
||||
// somewhere in data.
|
||||
return boost::regex_search(data, regex_);
|
||||
}
|
||||
private:
|
||||
InfoMap const map_;
|
||||
mutable boost::regex regex_;
|
||||
};
|
||||
|
||||
|
||||
void test_escape_special_chars()
|
||||
{
|
||||
cout << escape_special_chars("abcd") << endl;
|
||||
cout << escape_special_chars("ab&cd") << endl;
|
||||
cout << escape_special_chars(".|*?+(){}[]^$\"") << endl;
|
||||
cout << escape_special_chars("..||**??++(()){{}}[[]]^^$$\\\\") << endl;
|
||||
}
|
||||
|
||||
|
||||
void test_RegexMatch()
|
||||
{
|
||||
InfoMap im;
|
||||
im["hello"] = "hei";
|
||||
|
||||
try {
|
||||
RegexMatch rm(im, "h.*o", false);
|
||||
|
||||
cout << rm("hello") << endl;
|
||||
cout << rm("hei") << endl;
|
||||
}
|
||||
catch (boost::regex_error & regerr) {
|
||||
cout << regerr.what() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
test_escape_special_chars();
|
||||
test_RegexMatch();
|
||||
}
|
33
src/frontends/controllers/tests/boost.C
Normal file
33
src/frontends/controllers/tests/boost.C
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* \file boost.C
|
||||
* This file is part of LyX, the document processor.
|
||||
* Licence details can be found in the file COPYING.
|
||||
*
|
||||
* \author Lars Gullik Bjønnes
|
||||
*
|
||||
* Full author contact details are available in file CREDITS.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
|
||||
namespace boost {
|
||||
|
||||
void throw_exception(std::exception const & /*e*/)
|
||||
{
|
||||
BOOST_ASSERT(false);
|
||||
}
|
||||
|
||||
|
||||
void assertion_failed(char const * /*expr*/, char const * /*function*/,
|
||||
char const * /*file*/, long /*line*/)
|
||||
{
|
||||
::abort();
|
||||
}
|
||||
|
||||
|
||||
}
|
64
src/frontends/controllers/tests/pch.h
Normal file
64
src/frontends/controllers/tests/pch.h
Normal file
@ -0,0 +1,64 @@
|
||||
// -*- C++ -*-
|
||||
#include <config.h>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/crc.hpp>
|
||||
// #include <boost/format.hpp> // mult def symbols problem (_1,_2 etc)
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/scoped_array.hpp>
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/signal.hpp>
|
||||
#include <boost/signals/trackable.hpp>
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#if BOOST_VERSION < 103301
|
||||
# include <boost/test/detail/nullstream.hpp>
|
||||
#else
|
||||
# include <boost/test/utils/nullstream.hpp>
|
||||
#endif
|
||||
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <boost/utility.hpp>
|
||||
|
||||
#include <fcntl.h>
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
# include <sys/stat.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
#include <time.h>
|
||||
#ifdef HAVE_SYS_SOCKET_H
|
||||
# include <sys/socket.h>
|
||||
#endif
|
||||
#ifndef _WIN32
|
||||
# include <sys/un.h>
|
||||
# include <sys/wait.h>
|
||||
# ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <csignal>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <iterator>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
#include <queue>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
6
src/frontends/controllers/tests/regfiles/biblio
Normal file
6
src/frontends/controllers/tests/regfiles/biblio
Normal file
@ -0,0 +1,6 @@
|
||||
abcd
|
||||
ab&cd
|
||||
\.\|\*\?\+\(\)\{\}\[\]\^\$"
|
||||
\.\.\|\|\*\*\?\?\+\+\(\(\)\)\{\{\}\}\[\[\]\]\^\^\$\$\\\\
|
||||
1
|
||||
0
|
7
src/frontends/controllers/tests/test_biblio
Executable file
7
src/frontends/controllers/tests/test_biblio
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
regfile=`cat ${srcdir}/regfiles/biblio`
|
||||
output=`./biblio`
|
||||
|
||||
test "$regfile" = "$output"
|
||||
exit $?
|
Loading…
Reference in New Issue
Block a user