mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-09 18:31:04 +00:00
* Add RegEx to the compiled boost files.
* clean-up the search code in biblio.C. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5595 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
125a79c569
commit
3f8d73282c
@ -1,3 +1,7 @@
|
|||||||
|
2002-11-07 Angus Leeming <leeming@lyx.org>
|
||||||
|
|
||||||
|
* libs/regex/src/Makefile.am: add cregex.cpp and fileiter.cpp
|
||||||
|
|
||||||
2002-10-15 Lars Gullik Bjønnes <larsbj@birdstep.com>
|
2002-10-15 Lars Gullik Bjønnes <larsbj@birdstep.com>
|
||||||
|
|
||||||
* update boost to version 1.29.0.
|
* update boost to version 1.29.0.
|
||||||
|
@ -8,14 +8,14 @@ libboostregex_la_SOURCES = \
|
|||||||
cpp_regex_traits.cpp \
|
cpp_regex_traits.cpp \
|
||||||
c_regex_traits_common.cpp \
|
c_regex_traits_common.cpp \
|
||||||
c_regex_traits.cpp \
|
c_regex_traits.cpp \
|
||||||
|
cregex.cpp \
|
||||||
|
fileiter.cpp \
|
||||||
instances.cpp \
|
instances.cpp \
|
||||||
primary_transform.hpp \
|
primary_transform.hpp \
|
||||||
regex.cpp \
|
regex.cpp \
|
||||||
w32_regex_traits.cpp \
|
w32_regex_traits.cpp \
|
||||||
regex_synch.cpp
|
regex_synch.cpp
|
||||||
|
|
||||||
# cregex.cpp \
|
|
||||||
# fileiter.cpp \
|
|
||||||
# posix_api.cpp \
|
# posix_api.cpp \
|
||||||
# wide_posix_api.cpp \
|
# wide_posix_api.cpp \
|
||||||
# regex_debug.cpp
|
# regex_debug.cpp
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
2002-11-06 Angus Leeming <leeming@lyx.org>
|
||||||
|
|
||||||
|
* biblio.C (searchKeys and helper functions): rewritten entirely
|
||||||
|
using the STL. Boost::regex searching now works as the old 1.2 code
|
||||||
|
did.
|
||||||
|
|
||||||
2002-11-06 Angus Leeming <leeming@lyx.org>
|
2002-11-06 Angus Leeming <leeming@lyx.org>
|
||||||
|
|
||||||
* biblio.C: clean-up of the code. No change in functionality.
|
* biblio.C: clean-up of the code. No change in functionality.
|
||||||
|
@ -150,7 +150,7 @@ struct compareNoCase: public std::binary_function<string, string, bool>
|
|||||||
return compare_ascii_no_case(s1, s2) < 0;
|
return compare_ascii_no_case(s1, s2) < 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace anon
|
} // namespace anon
|
||||||
|
|
||||||
|
|
||||||
@ -238,81 +238,56 @@ string const getInfo(InfoMap const & map, string const & key)
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// The functions doing the dirty work for the search.
|
// Escape special chars.
|
||||||
vector<string>::const_iterator
|
// All characters are literals except: .|*?+(){}[]^$\
|
||||||
simpleSearch(InfoMap const & theMap,
|
// These characters are literals when preceded by a "\", which is done here
|
||||||
vector<string> const & keys,
|
string const escape_special_chars(string const & expr)
|
||||||
string const & expr,
|
|
||||||
vector<string>::const_iterator start,
|
|
||||||
Direction dir,
|
|
||||||
bool caseSensitive)
|
|
||||||
{
|
{
|
||||||
string tmp = expr;
|
// Search for all chars .|*?+(){}[]^$\
|
||||||
if (!caseSensitive)
|
// Note that they must be escaped in the RE.
|
||||||
tmp = lowercase(tmp);
|
boost::RegEx reg("[\\.\\|\\*\\?\\+\\(\\)\\{\\}\\[\\]\\^\\$\\\\]");
|
||||||
|
|
||||||
vector<string> searchwords = getVectorFromString(tmp, " ");
|
// $& 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:
|
||||||
|
string const fmt("\\\\$&");
|
||||||
|
|
||||||
// Loop over all keys from start...
|
return reg.Merge(expr, fmt);
|
||||||
for (vector<string>::const_iterator it = start;
|
|
||||||
// End condition is direction-dependent.
|
|
||||||
(dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
|
|
||||||
// increment is direction-dependent.
|
|
||||||
(dir == FORWARD) ? (++it) : (--it)) {
|
|
||||||
|
|
||||||
string data = (*it);
|
|
||||||
InfoMap::const_iterator info = theMap.find(*it);
|
|
||||||
if (info != theMap.end())
|
|
||||||
data += " " + info->second;
|
|
||||||
if (!caseSensitive)
|
|
||||||
data = lowercase(data);
|
|
||||||
|
|
||||||
bool found = true;
|
|
||||||
|
|
||||||
// Loop over all search words...
|
|
||||||
for (vector<string>::const_iterator sit = searchwords.begin();
|
|
||||||
sit != searchwords.end(); ++sit) {
|
|
||||||
if (data.find(*sit) == string::npos) {
|
|
||||||
found = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (found) return it;
|
|
||||||
}
|
|
||||||
|
|
||||||
return keys.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<string>::const_iterator
|
// A functor for use with std::find_if, used to ascertain whether a
|
||||||
regexSearch(InfoMap const & theMap,
|
// data entry matches the required regex_
|
||||||
vector<string> const & keys,
|
struct RegexMatch
|
||||||
string const & expr,
|
|
||||||
vector<string>::const_iterator start,
|
|
||||||
Direction dir)
|
|
||||||
{
|
{
|
||||||
boost::regex reg(STRCONV(expr));
|
// 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) {}
|
||||||
|
|
||||||
for (vector<string>::const_iterator it = start;
|
bool operator()(string const & key) {
|
||||||
// End condition is direction-dependent.
|
if (!validRE())
|
||||||
(dir == FORWARD) ? (it < keys.end()) : (it >= keys.begin());
|
return false;
|
||||||
// increment is direction-dependent.
|
|
||||||
(dir == FORWARD) ? (++it) : (--it)) {
|
|
||||||
|
|
||||||
string data = (*it);
|
// the data searched is the key + its associated BibTeX/biblio
|
||||||
InfoMap::const_iterator info = theMap.find(*it);
|
// fields
|
||||||
if (info != theMap.end())
|
string data = key;
|
||||||
|
InfoMap::const_iterator info = map_.find(key);
|
||||||
|
if (info != map_.end())
|
||||||
data += " " + info->second;
|
data += " " + info->second;
|
||||||
|
|
||||||
if (boost::regex_match(STRCONV(data), reg)) {
|
// Attempts to find a match for the current RE
|
||||||
return it;
|
// somewhere in data.
|
||||||
}
|
return regex_.Search(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return keys.end();
|
bool validRE() const { return regex_.error_code() == 0; }
|
||||||
}
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
InfoMap const map_;
|
||||||
|
boost::RegEx regex_;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace anon
|
} // namespace anon
|
||||||
|
|
||||||
@ -320,7 +295,7 @@ regexSearch(InfoMap const & theMap,
|
|||||||
vector<string>::const_iterator
|
vector<string>::const_iterator
|
||||||
searchKeys(InfoMap const & theMap,
|
searchKeys(InfoMap const & theMap,
|
||||||
vector<string> const & keys,
|
vector<string> const & keys,
|
||||||
string const & expr,
|
string const & search_expr,
|
||||||
vector<string>::const_iterator start,
|
vector<string>::const_iterator start,
|
||||||
Search type,
|
Search type,
|
||||||
Direction dir,
|
Direction dir,
|
||||||
@ -330,15 +305,34 @@ searchKeys(InfoMap const & theMap,
|
|||||||
if (start < keys.begin() || start >= keys.end())
|
if (start < keys.begin() || start >= keys.end())
|
||||||
return keys.end();
|
return keys.end();
|
||||||
|
|
||||||
string search_expr = trim(expr);
|
string expr = trim(search_expr);
|
||||||
if (search_expr.empty())
|
if (expr.empty())
|
||||||
return keys.end();
|
return keys.end();
|
||||||
|
|
||||||
if (type == SIMPLE)
|
if (type == SIMPLE)
|
||||||
return simpleSearch(theMap, keys, search_expr, start, dir,
|
// We must escape special chars in the search_expr so that
|
||||||
caseSensitive);
|
// it is treated as a simple string by boost::regex.
|
||||||
|
expr = escape_special_chars(expr);
|
||||||
|
|
||||||
return regexSearch(theMap, keys, search_expr, start, dir);
|
// 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)
|
||||||
|
return keys.end();
|
||||||
|
// This is correct and always safe.
|
||||||
|
// (See Meyer's Effective STL, Item 28.)
|
||||||
|
return (++rit).base();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user