1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
[/
Copyright 2006-2007 John Maddock.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
]
[section:regex_grep regex_grep (Deprecated)]
The algorithm `regex_grep` is deprecated in favor of [regex_iterator]
which provides a more convenient and standard library friendly interface.
The following documentation is taken unchanged from the previous boost
release, and will not be updated in future.
#include <boost/regex.hpp>
`regex_grep` allows you to search through a bidirectional-iterator range and
locate all the (non-overlapping) matches with a given regular expression.
The function is declared as:
template <class Predicate, class iterator, class charT, class traits>
unsigned int regex_grep(Predicate foo,
iterator first,
iterator last,
const basic_regex<charT, traits>& e,
boost::match_flag_type flags = match_default)
The library also defines the following convenience versions, which take
either a `const charT*`, or a `const std::basic_string<>&` in place of a
pair of iterators.
template <class Predicate, class charT, class traits>
unsigned int regex_grep(Predicate foo,
const charT* str,
const basic_regex<charT, traits>& e,
boost::match_flag_type flags = match_default);
template <class Predicate, class ST, class SA, class charT, class traits>
unsigned int regex_grep(Predicate foo,
const std::basic_string<charT, ST, SA>& s,
const basic_regex<charT, traits>& e,
boost::match_flag_type flags = match_default);
The parameters for the primary version of `regex_grep` have the following meanings:
foo: A predicate function object or function pointer, see below for more information.
first: The start of the range to search.
last: The end of the range to search.
e: The regular expression to search for.
flags: The flags that determine how matching is carried out, one of the match_flags enumerators.
The algorithm finds all of the non-overlapping matches of the expression /e/,
for each match it fills a `match_results<iterator>` structure, which
contains information on what matched, and calls the predicate /foo/, passing the
`match_results<iterator>` as a single argument. If the predicate returns
/true/, then the grep operation continues, otherwise it terminates
without searching for further matches. The function returns the number
of matches found.
The general form of the predicate is:
struct grep_predicate
{
bool operator()(const match_results<iterator_type>& m);
};
For example the regular expression "a\*b" would find one match in the string
"aaaaab" and two in the string "aaabb".
Remember this algorithm can be used for a lot more than implementing a
version of grep, the predicate can be and do anything that you want,
grep utilities would output the results to the screen, another program could
index a file based on a regular expression and store a set of bookmarks in a list,
or a text file conversion utility would output to file. The results of one
`regex_grep` can even be chained into another `regex_grep` to create recursive parsers.
The algorithm may throw `std::runtime_error` if the complexity of matching the
expression against an /N/ character string begins to exceed O(N[super 2]), or
if the program runs out of stack space while matching the expression
(if Boost.Regex is configured in recursive mode), or if the matcher
exhausts it's permitted memory allocation (if Boost.Regex is configured in
non-recursive mode).
Example: convert the example from [regex_search] to use `regex_grep` instead:
#include <string>
#include <map>
#include <boost/regex.hpp>
// IndexClasses:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's
typedef std::map<std::string, int, std::less<std::string> > map_type;
const char* re =
// possibly leading whitespace:
"^[[:space:]]*"
// possible template declaration:
"(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
// class or struct:
"(class|struct)[[:space:]]*"
// leading declspec macros etc:
"("
"\\<\\w+\\>"
"("
"[[:blank:]]*\\([^)]*\\)"
")?"
"[[:space:]]*"
")*"
// the class name
"(\\<\\w*\\>)[[:space:]]*"
// template specialisation parameters
"(<[^;:{]+>)?[[:space:]]*"
// terminate in { or :
"(\\{|:[^;\\{()]*\\{)";
boost::regex expression(re);
class IndexClassesPred
{
map_type& m;
std::string::const_iterator base;
public:
IndexClassesPred(map_type& a, std::string::const_iterator b) : m(a), base(b) {}
bool operator()(const smatch& what)
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
what[5].first - base;
return true;
}
};
void IndexClasses(map_type& m, const std::string& file)
{
std::string::const_iterator start, end;
start = file.begin();
end = file.end();
regex_grep(IndexClassesPred(m, start), start, end, expression);
}
Example: Use `regex_grep` to call a global callback function:
#include <string>
#include <map>
#include <boost/regex.hpp>
// purpose:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's
typedef std::map<std::string, int, std::less<std::string> > map_type;
const char* re =
// possibly leading whitespace:
"^[[:space:]]*"
// possible template declaration:
"(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
// class or struct:
"(class|struct)[[:space:]]*"
// leading declspec macros etc:
"("
"\\<\\w+\\>"
"("
"[[:blank:]]*\\([^)]*\\)"
")?"
"[[:space:]]*"
")*"
// the class name
"(\\<\\w*\\>)[[:space:]]*"
// template specialisation parameters
"(<[^;:{]+>)?[[:space:]]*"
// terminate in { or :
"(\\{|:[^;\\{()]*\\{)";
boost::regex expression(re);
map_type class_index;
std::string::const_iterator base;
bool grep_callback(const boost::smatch& what)
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
class_index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
what[5].first - base;
return true;
}
void IndexClasses(const std::string& file)
{
std::string::const_iterator start, end;
start = file.begin();
end = file.end();
base = start;
regex_grep(grep_callback, start, end, expression, match_default);
}
Example: use `regex_grep` to call a class member function, use the standard
library adapters `std::mem_fun` and `std::bind1st` to convert the member
function into a predicate:
#include <string>
#include <map>
#include <boost/regex.hpp>
#include <functional>
// purpose:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's
typedef std::map<std::string, int, std::less<std::string> > map_type;
class class_index
{
boost::regex expression;
map_type index;
std::string::const_iterator base;
bool grep_callback(boost::smatch what);
public:
void IndexClasses(const std::string& file);
class_index()
: index(),
expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
"(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
"[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
"(\\{|:[^;\\{()]*\\{)"
){}
};
bool class_index::grep_callback(boost::smatch what)
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
what[5].first - base;
return true;
}
void class_index::IndexClasses(const std::string& file)
{
std::string::const_iterator start, end;
start = file.begin();
end = file.end();
base = start;
regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this),
start,
end,
expression);
}
Finally, C++ Builder users can use C++ Builder's closure type as a callback argument:
#include <string>
#include <map>
#include <boost/regex.hpp>
#include <functional>
// purpose:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's
typedef std::map<std::string, int, std::less<std::string> > map_type;
class class_index
{
boost::regex expression;
map_type index;
std::string::const_iterator base;
typedef boost::smatch arg_type;
bool grep_callback(const arg_type& what);
public:
typedef bool (__closure* grep_callback_type)(const arg_type&);
void IndexClasses(const std::string& file);
class_index()
: index(),
expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
"(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
"[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"
"(\\{|:[^;\\{()]*\\{)"
){}
};
bool class_index::grep_callback(const arg_type& what)
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
what[5].first - base;
return true;
}
void class_index::IndexClasses(const std::string& file)
{
std::string::const_iterator start, end;
start = file.begin();
end = file.end();
base = start;
class_index::grep_callback_type cl = &(this->grep_callback);
regex_grep(cl,
start,
end,
expression);
}
[endsect]
|