1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>regex_grep (Deprecated)</title> 5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../index.html" title="Boost.Regex 5.1.4"> 8<link rel="up" href="../deprecated.html" title="Deprecated Interfaces"> 9<link rel="prev" href="regex_format.html" title="regex_format (Deprecated)"> 10<link rel="next" href="regex_split.html" title="regex_split (deprecated)"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="regex_format.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../deprecated.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="regex_split.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_regex.ref.deprecated.regex_grep"></a><a class="link" href="regex_grep.html" title="regex_grep (Deprecated)">regex_grep (Deprecated)</a> 28</h4></div></div></div> 29<p> 30 The algorithm <code class="computeroutput"><span class="identifier">regex_grep</span></code> 31 is deprecated in favor of <a class="link" href="../regex_iterator.html" title="regex_iterator"><code class="computeroutput"><span class="identifier">regex_iterator</span></code></a> which provides 32 a more convenient and standard library friendly interface. 33 </p> 34<p> 35 The following documentation is taken unchanged from the previous boost 36 release, and will not be updated in future. 37 </p> 38<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 39</pre> 40<p> 41 <code class="computeroutput"><span class="identifier">regex_grep</span></code> allows you to 42 search through a bidirectional-iterator range and locate all the (non-overlapping) 43 matches with a given regular expression. The function is declared as: 44 </p> 45<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Predicate</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">iterator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traits</span><span class="special">></span> 46<span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">Predicate</span> <span class="identifier">foo</span><span class="special">,</span> 47 <span class="identifier">iterator</span> <span class="identifier">first</span><span class="special">,</span> 48 <span class="identifier">iterator</span> <span class="identifier">last</span><span class="special">,</span> 49 <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special"><</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">>&</span> <span class="identifier">e</span><span class="special">,</span> 50 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">)</span> 51</pre> 52<p> 53 The library also defines the following convenience versions, which take 54 either a <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">charT</span><span class="special">*</span></code>, or a <code class="computeroutput"><span class="keyword">const</span> 55 <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special"><>&</span></code> 56 in place of a pair of iterators. 57 </p> 58<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Predicate</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traits</span><span class="special">></span> 59<span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">Predicate</span> <span class="identifier">foo</span><span class="special">,</span> 60 <span class="keyword">const</span> <span class="identifier">charT</span><span class="special">*</span> <span class="identifier">str</span><span class="special">,</span> 61 <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special"><</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">>&</span> <span class="identifier">e</span><span class="special">,</span> 62 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">);</span> 63 64<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Predicate</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">ST</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SA</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traits</span><span class="special">></span> 65<span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">Predicate</span> <span class="identifier">foo</span><span class="special">,</span> 66 <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special"><</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">ST</span><span class="special">,</span> <span class="identifier">SA</span><span class="special">>&</span> <span class="identifier">s</span><span class="special">,</span> 67 <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special"><</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">>&</span> <span class="identifier">e</span><span class="special">,</span> 68 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">);</span> 69</pre> 70<p> 71 The parameters for the primary version of <code class="computeroutput"><span class="identifier">regex_grep</span></code> 72 have the following meanings: 73 </p> 74<p> 75 foo: A predicate function object or function pointer, see below for more 76 information. 77 </p> 78<p> 79 first: The start of the range to search. 80 </p> 81<p> 82 last: The end of the range to search. 83 </p> 84<p> 85 e: The regular expression to search for. 86 </p> 87<p> 88 flags: The flags that determine how matching is carried out, one of the 89 match_flags enumerators. 90 </p> 91<p> 92 The algorithm finds all of the non-overlapping matches of the expression 93 <span class="emphasis"><em>e</em></span>, for each match it fills a <code class="computeroutput"><span class="identifier">match_results</span><span class="special"><</span><span class="identifier">iterator</span><span class="special">></span></code> structure, which contains information 94 on what matched, and calls the predicate <span class="emphasis"><em>foo</em></span>, passing 95 the <code class="computeroutput"><span class="identifier">match_results</span><span class="special"><</span><span class="identifier">iterator</span><span class="special">></span></code> 96 as a single argument. If the predicate returns <span class="emphasis"><em>true</em></span>, 97 then the grep operation continues, otherwise it terminates without searching 98 for further matches. The function returns the number of matches found. 99 </p> 100<p> 101 The general form of the predicate is: 102 </p> 103<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">grep_predicate</span> 104<span class="special">{</span> 105 <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">match_results</span><span class="special"><</span><span class="identifier">iterator_type</span><span class="special">>&</span> <span class="identifier">m</span><span class="special">);</span> 106<span class="special">};</span> 107</pre> 108<p> 109 For example the regular expression "a*b" would find one match 110 in the string "aaaaab" and two in the string "aaabb". 111 </p> 112<p> 113 Remember this algorithm can be used for a lot more than implementing a 114 version of grep, the predicate can be and do anything that you want, grep 115 utilities would output the results to the screen, another program could 116 index a file based on a regular expression and store a set of bookmarks 117 in a list, or a text file conversion utility would output to file. The 118 results of one <code class="computeroutput"><span class="identifier">regex_grep</span></code> 119 can even be chained into another <code class="computeroutput"><span class="identifier">regex_grep</span></code> 120 to create recursive parsers. 121 </p> 122<p> 123 The algorithm may throw <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span></code> 124 if the complexity of matching the expression against an <span class="emphasis"><em>N</em></span> 125 character string begins to exceed O(N<sup>2</sup>), or if the program runs out of 126 stack space while matching the expression (if Boost.Regex is configured 127 in recursive mode), or if the matcher exhausts it's permitted memory allocation 128 (if Boost.Regex is configured in non-recursive mode). 129 </p> 130<p> 131 Example: convert the example from <a class="link" href="../regex_search.html" title="regex_search"><code class="computeroutput"><span class="identifier">regex_search</span></code></a> to use <code class="computeroutput"><span class="identifier">regex_grep</span></code> instead: 132 </p> 133<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 134<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">map</span><span class="special">></span> 135<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 136 137<span class="comment">// IndexClasses: </span> 138<span class="comment">// takes the contents of a file in the form of a string </span> 139<span class="comment">// and searches for all the C++ class definitions, storing </span> 140<span class="comment">// their locations in a map of strings/int's </span> 141<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="special">></span> <span class="identifier">map_type</span><span class="special">;</span> 142 143<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">re</span> <span class="special">=</span> 144 <span class="comment">// possibly leading whitespace: </span> 145 <span class="string">"^[[:space:]]*"</span> 146 <span class="comment">// possible template declaration:</span> 147 <span class="string">"(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"</span> 148 <span class="comment">// class or struct:</span> 149 <span class="string">"(class|struct)[[:space:]]*"</span> 150 <span class="comment">// leading declspec macros etc:</span> 151 <span class="string">"("</span> 152 <span class="string">"\\<\\w+\\>"</span> 153 <span class="string">"("</span> 154 <span class="string">"[[:blank:]]*\\([^)]*\\)"</span> 155 <span class="string">")?"</span> 156 <span class="string">"[[:space:]]*"</span> 157 <span class="string">")*"</span> 158 <span class="comment">// the class name</span> 159 <span class="string">"(\\<\\w*\\>)[[:space:]]*"</span> 160 <span class="comment">// template specialisation parameters</span> 161 <span class="string">"(<[^;:{]+>)?[[:space:]]*"</span> 162 <span class="comment">// terminate in { or :</span> 163 <span class="string">"(\\{|:[^;\\{()]*\\{)"</span><span class="special">;</span> 164 165<span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">expression</span><span class="special">(</span><span class="identifier">re</span><span class="special">);</span> 166<span class="keyword">class</span> <span class="identifier">IndexClassesPred</span> 167<span class="special">{</span> 168 <span class="identifier">map_type</span><span class="special">&</span> <span class="identifier">m</span><span class="special">;</span> 169 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">base</span><span class="special">;</span> 170<span class="keyword">public</span><span class="special">:</span> 171 <span class="identifier">IndexClassesPred</span><span class="special">(</span><span class="identifier">map_type</span><span class="special">&</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">b</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">m</span><span class="special">(</span><span class="identifier">a</span><span class="special">),</span> <span class="identifier">base</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span> <span class="special">{}</span> 172 <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">smatch</span><span class="special">&</span> <span class="identifier">what</span><span class="special">)</span> 173 <span class="special">{</span> 174 <span class="comment">// what[0] contains the whole string </span> 175 <span class="comment">// what[5] contains the class name. </span> 176 <span class="comment">// what[6] contains the template specialisation if any. </span> 177 <span class="comment">// add class name and position to map: </span> 178 <span class="identifier">m</span><span class="special">[</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">second</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">second</span><span class="special">)]</span> <span class="special">=</span> 179 <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span> <span class="special">-</span> <span class="identifier">base</span><span class="special">;</span> 180 <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> 181 <span class="special">}</span> 182<span class="special">};</span> 183<span class="keyword">void</span> <span class="identifier">IndexClasses</span><span class="special">(</span><span class="identifier">map_type</span><span class="special">&</span> <span class="identifier">m</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">file</span><span class="special">)</span> 184<span class="special">{</span> 185 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">end</span><span class="special">;</span> 186 <span class="identifier">start</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 187 <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> 188 <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">IndexClassesPred</span><span class="special">(</span><span class="identifier">m</span><span class="special">,</span> <span class="identifier">start</span><span class="special">),</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">end</span><span class="special">,</span> <span class="identifier">expression</span><span class="special">);</span> 189<span class="special">}</span> 190</pre> 191<p> 192 Example: Use <code class="computeroutput"><span class="identifier">regex_grep</span></code> 193 to call a global callback function: 194 </p> 195<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 196<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">map</span><span class="special">></span> 197<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 198 199<span class="comment">// purpose: </span> 200<span class="comment">// takes the contents of a file in the form of a string </span> 201<span class="comment">// and searches for all the C++ class definitions, storing </span> 202<span class="comment">// their locations in a map of strings/int's </span> 203<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="special">></span> <span class="identifier">map_type</span><span class="special">;</span> 204 205<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">re</span> <span class="special">=</span> 206 <span class="comment">// possibly leading whitespace: </span> 207 <span class="string">"^[[:space:]]*"</span> 208 <span class="comment">// possible template declaration:</span> 209 <span class="string">"(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"</span> 210 <span class="comment">// class or struct:</span> 211 <span class="string">"(class|struct)[[:space:]]*"</span> 212 <span class="comment">// leading declspec macros etc:</span> 213 <span class="string">"("</span> 214 <span class="string">"\\<\\w+\\>"</span> 215 <span class="string">"("</span> 216 <span class="string">"[[:blank:]]*\\([^)]*\\)"</span> 217 <span class="string">")?"</span> 218 <span class="string">"[[:space:]]*"</span> 219 <span class="string">")*"</span> 220 <span class="comment">// the class name</span> 221 <span class="string">"(\\<\\w*\\>)[[:space:]]*"</span> 222 <span class="comment">// template specialisation parameters</span> 223 <span class="string">"(<[^;:{]+>)?[[:space:]]*"</span> 224 <span class="comment">// terminate in { or :</span> 225 <span class="string">"(\\{|:[^;\\{()]*\\{)"</span><span class="special">;</span> 226 227<span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">expression</span><span class="special">(</span><span class="identifier">re</span><span class="special">);</span> 228<span class="identifier">map_type</span> <span class="identifier">class_index</span><span class="special">;</span> 229<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">base</span><span class="special">;</span> 230 231<span class="keyword">bool</span> <span class="identifier">grep_callback</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">smatch</span><span class="special">&</span> <span class="identifier">what</span><span class="special">)</span> 232<span class="special">{</span> 233 <span class="comment">// what[0] contains the whole string </span> 234 <span class="comment">// what[5] contains the class name. </span> 235 <span class="comment">// what[6] contains the template specialisation if any. </span> 236 <span class="comment">// add class name and position to map: </span> 237 <span class="identifier">class_index</span><span class="special">[</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">second</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">second</span><span class="special">)]</span> <span class="special">=</span> 238 <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span> <span class="special">-</span> <span class="identifier">base</span><span class="special">;</span> 239 <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> 240<span class="special">}</span> 241<span class="keyword">void</span> <span class="identifier">IndexClasses</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">file</span><span class="special">)</span> 242<span class="special">{</span> 243 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">end</span><span class="special">;</span> 244 <span class="identifier">start</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 245 <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> 246 <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">start</span><span class="special">;</span> 247 <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">grep_callback</span><span class="special">,</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">end</span><span class="special">,</span> <span class="identifier">expression</span><span class="special">,</span> <span class="identifier">match_default</span><span class="special">);</span> 248<span class="special">}</span> 249</pre> 250<p> 251 Example: use <code class="computeroutput"><span class="identifier">regex_grep</span></code> 252 to call a class member function, use the standard library adapters <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">mem_fun</span></code> and <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">bind1st</span></code> 253 to convert the member function into a predicate: 254 </p> 255<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 256<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">map</span><span class="special">></span> 257<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 258<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">functional</span><span class="special">></span> 259<span class="comment">// purpose: </span> 260<span class="comment">// takes the contents of a file in the form of a string </span> 261<span class="comment">// and searches for all the C++ class definitions, storing </span> 262<span class="comment">// their locations in a map of strings/int's </span> 263 264<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="special">></span> <span class="identifier">map_type</span><span class="special">;</span> 265<span class="keyword">class</span> <span class="identifier">class_index</span> 266<span class="special">{</span> 267 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">expression</span><span class="special">;</span> 268 <span class="identifier">map_type</span> <span class="identifier">index</span><span class="special">;</span> 269 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">base</span><span class="special">;</span> 270 <span class="keyword">bool</span> <span class="identifier">grep_callback</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">smatch</span> <span class="identifier">what</span><span class="special">);</span> 271<span class="keyword">public</span><span class="special">:</span> 272 <span class="keyword">void</span> <span class="identifier">IndexClasses</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">file</span><span class="special">);</span> 273 <span class="identifier">class_index</span><span class="special">()</span> 274 <span class="special">:</span> <span class="identifier">index</span><span class="special">(),</span> 275 <span class="identifier">expression</span><span class="special">(</span><span class="string">"^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"</span> 276 <span class="string">"(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"</span> 277 <span class="string">"[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"</span> 278 <span class="string">"(\\{|:[^;\\{()]*\\{)"</span> 279 <span class="special">){}</span> 280<span class="special">};</span> 281<span class="keyword">bool</span> <span class="identifier">class_index</span><span class="special">::</span><span class="identifier">grep_callback</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">smatch</span> <span class="identifier">what</span><span class="special">)</span> 282<span class="special">{</span> 283 <span class="comment">// what[0] contains the whole string </span> 284 <span class="comment">// what[5] contains the class name. </span> 285 <span class="comment">// what[6] contains the template specialisation if any. </span> 286 <span class="comment">// add class name and position to map: </span> 287 <span class="identifier">index</span><span class="special">[</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">second</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">second</span><span class="special">)]</span> <span class="special">=</span> 288 <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span> <span class="special">-</span> <span class="identifier">base</span><span class="special">;</span> 289 <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> 290<span class="special">}</span> 291 292<span class="keyword">void</span> <span class="identifier">class_index</span><span class="special">::</span><span class="identifier">IndexClasses</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">file</span><span class="special">)</span> 293<span class="special">{</span> 294 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">end</span><span class="special">;</span> 295 <span class="identifier">start</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 296 <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> 297 <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">start</span><span class="special">;</span> 298 <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">bind1st</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">mem_fun</span><span class="special">(&</span><span class="identifier">class_index</span><span class="special">::</span><span class="identifier">grep_callback</span><span class="special">),</span> <span class="keyword">this</span><span class="special">),</span> 299 <span class="identifier">start</span><span class="special">,</span> 300 <span class="identifier">end</span><span class="special">,</span> 301 <span class="identifier">expression</span><span class="special">);</span> 302<span class="special">}</span> 303</pre> 304<p> 305 Finally, C++ Builder users can use C++ Builder's closure type as a callback 306 argument: 307 </p> 308<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> 309<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">map</span><span class="special">></span> 310<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 311<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">functional</span><span class="special">></span> 312<span class="comment">// purpose: </span> 313<span class="comment">// takes the contents of a file in the form of a string </span> 314<span class="comment">// and searches for all the C++ class definitions, storing </span> 315<span class="comment">// their locations in a map of strings/int's </span> 316 317<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="special">></span> <span class="identifier">map_type</span><span class="special">;</span> 318<span class="keyword">class</span> <span class="identifier">class_index</span> 319<span class="special">{</span> 320 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">expression</span><span class="special">;</span> 321 <span class="identifier">map_type</span> <span class="identifier">index</span><span class="special">;</span> 322 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">base</span><span class="special">;</span> 323 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">smatch</span> <span class="identifier">arg_type</span><span class="special">;</span> 324 <span class="keyword">bool</span> <span class="identifier">grep_callback</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">arg_type</span><span class="special">&</span> <span class="identifier">what</span><span class="special">);</span> 325<span class="keyword">public</span><span class="special">:</span> 326 <span class="keyword">typedef</span> <span class="keyword">bool</span> <span class="special">(</span><span class="identifier">__closure</span><span class="special">*</span> <span class="identifier">grep_callback_type</span><span class="special">)(</span><span class="keyword">const</span> <span class="identifier">arg_type</span><span class="special">&);</span> 327 <span class="keyword">void</span> <span class="identifier">IndexClasses</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">file</span><span class="special">);</span> 328 <span class="identifier">class_index</span><span class="special">()</span> 329 <span class="special">:</span> <span class="identifier">index</span><span class="special">(),</span> 330 <span class="identifier">expression</span><span class="special">(</span><span class="string">"^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"</span> 331 <span class="string">"(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"</span> 332 <span class="string">"[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?"</span> 333 <span class="string">"(\\{|:[^;\\{()]*\\{)"</span> 334 <span class="special">){}</span> 335<span class="special">};</span> 336 337<span class="keyword">bool</span> <span class="identifier">class_index</span><span class="special">::</span><span class="identifier">grep_callback</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">arg_type</span><span class="special">&</span> <span class="identifier">what</span><span class="special">)</span> 338<span class="special">{</span> 339 <span class="comment">// what[0] contains the whole string </span> 340<span class="comment">// what[5] contains the class name. </span> 341<span class="comment">// what[6] contains the template specialisation if any. </span> 342<span class="comment">// add class name and position to map: </span> 343<span class="identifier">index</span><span class="special">[</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">second</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">what</span><span class="special">[</span><span class="number">6</span><span class="special">].</span><span class="identifier">second</span><span class="special">)]</span> <span class="special">=</span> 344 <span class="identifier">what</span><span class="special">[</span><span class="number">5</span><span class="special">].</span><span class="identifier">first</span> <span class="special">-</span> <span class="identifier">base</span><span class="special">;</span> 345 <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> 346<span class="special">}</span> 347 348<span class="keyword">void</span> <span class="identifier">class_index</span><span class="special">::</span><span class="identifier">IndexClasses</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">file</span><span class="special">)</span> 349<span class="special">{</span> 350 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">start</span><span class="special">,</span> <span class="identifier">end</span><span class="special">;</span> 351 <span class="identifier">start</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span> 352 <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">file</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> 353 <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">start</span><span class="special">;</span> 354 <span class="identifier">class_index</span><span class="special">::</span><span class="identifier">grep_callback_type</span> <span class="identifier">cl</span> <span class="special">=</span> <span class="special">&(</span><span class="keyword">this</span><span class="special">-></span><span class="identifier">grep_callback</span><span class="special">);</span> 355 <span class="identifier">regex_grep</span><span class="special">(</span><span class="identifier">cl</span><span class="special">,</span> 356 <span class="identifier">start</span><span class="special">,</span> 357 <span class="identifier">end</span><span class="special">,</span> 358 <span class="identifier">expression</span><span class="special">);</span> 359<span class="special">}</span> 360</pre> 361</div> 362<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 363<td align="left"></td> 364<td align="right"><div class="copyright-footer">Copyright © 1998-2013 John Maddock<p> 365 Distributed under the Boost Software License, Version 1.0. (See accompanying 366 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 367 </p> 368</div></td> 369</tr></table> 370<hr> 371<div class="spirit-nav"> 372<a accesskey="p" href="regex_format.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../deprecated.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="regex_split.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 373</div> 374</body> 375</html> 376