]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/example/snippets/partial_regex_iterate.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / regex / example / snippets / partial_regex_iterate.cpp
1 /*
2 *
3 * Copyright (c) 1998-2007
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12 /*
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE partial_regex_iterate.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Search example using partial matches.
17 */
18
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <string>
23 #include <cstring>
24 #include <boost/regex.hpp>
25
26 #ifdef BOOST_NO_STDC_NAMESPACE
27 namespace std{ using ::memmove; }
28 #endif
29
30 // match some kind of html tag:
31 boost::regex e("<[^>]*>");
32 // count how many:
33 unsigned int tags = 0;
34
35 void search(std::istream& is)
36 {
37 // buffer we'll be searching in:
38 char buf[4096];
39 // saved position of end of partial match:
40 const char* next_pos = buf + sizeof(buf);
41 // flag to indicate whether there is more input to come:
42 bool have_more = true;
43
44 while(have_more)
45 {
46 // how much do we copy forward from last try:
47 std::ptrdiff_t leftover = (buf + sizeof(buf)) - next_pos;
48 // and how much is left to fill:
49 std::ptrdiff_t size = next_pos - buf;
50 // copy forward whatever we have left:
51 std::memmove(buf, next_pos, leftover);
52 // fill the rest from the stream:
53 is.read(buf + leftover, size);
54 std::streamsize read = is.gcount();
55 // check to see if we've run out of text:
56 have_more = read == size;
57 // reset next_pos:
58 next_pos = buf + sizeof(buf);
59 // and then iterate:
60 boost::cregex_iterator a(
61 buf,
62 buf + read + leftover,
63 e,
64 boost::match_default | boost::match_partial);
65 boost::cregex_iterator b;
66
67 while(a != b)
68 {
69 if((*a)[0].matched == false)
70 {
71 // Partial match, save position and break:
72 next_pos = (*a)[0].first;
73 break;
74 }
75 else
76 {
77 // full match:
78 ++tags;
79 }
80
81 // move to next match:
82 ++a;
83 }
84 }
85 }
86
87 int main(int argc, char* argv[])
88 {
89 if(argc > 1)
90 {
91 for(int i = 1; i < argc; ++i)
92 {
93 std::ifstream fs(argv[i]);
94 if(fs.bad()) continue;
95 search(fs);
96 fs.close();
97 }
98 }
99 else
100 {
101 std::string one("<META NAME=\"keywords\" CONTENT=\"regex++, regular expressions, regular expression library, C++\">");
102 std::string what;
103 while(what.size() < 10000)
104 {
105 what.append(one);
106 what.append(13, ' ');
107 }
108 std::stringstream ss;
109 ss.str(what);
110 search(ss);
111 }
112 std::cout << "total tag count was " << tags << std::endl;
113 return 0;
114 }
115
116
117
118