+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
// 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:
: 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;
// 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
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);
-
- // Build the functor that will be passed to find_if.
- RegexMatch const match(theMap, expr, !caseSensitive);
- if (!match.validRE())
- 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)
+ expr = escape_special_chars(expr);
+
+ 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();
- // This is correct and always safe.
- // (See Meyer's Effective STL, Item 28.)
- return (++rit).base();
+ }
}
--- /dev/null
+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
--- /dev/null
+#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();
+}
--- /dev/null
+/**
+ * \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();
+}
+
+
+}
--- /dev/null
+// -*- 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>
--- /dev/null
+abcd
+ab&cd
+\.\|\*\?\+\(\)\{\}\[\]\^\$"
+\.\.\|\|\*\*\?\?\+\+\(\(\)\)\{\{\}\}\[\[\]\]\^\^\$\$\\\\
+1
+0
--- /dev/null
+#!/bin/bash
+
+regfile=`cat ${srcdir}/regfiles/biblio`
+output=`./biblio`
+
+test "$regfile" = "$output"
+exit $?