/[pcre]/code/trunk/pcre_scanner.cc
ViewVC logotype

Contents of /code/trunk/pcre_scanner.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (show annotations) (download)
Sat Feb 24 21:40:45 2007 UTC (7 years, 5 months ago) by nigel
File size: 4967 byte(s)
Load pcre-6.0 into code/trunk.

1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Sanjay Ghemawat
31
32 #include <vector>
33 #include <algorithm> // for count()
34 #include <assert.h>
35 #include "config.h"
36 #include "pcre_scanner.h"
37
38 using std::vector;
39
40 namespace pcrecpp {
41
42 Scanner::Scanner()
43 : data_(),
44 input_(data_),
45 skip_(NULL),
46 should_skip_(false),
47 save_comments_(false),
48 comments_(NULL),
49 comments_offset_(0) {
50 }
51
52 Scanner::Scanner(const string& in)
53 : data_(in),
54 input_(data_),
55 skip_(NULL),
56 should_skip_(false),
57 save_comments_(false),
58 comments_(NULL),
59 comments_offset_(0) {
60 }
61
62 Scanner::~Scanner() {
63 delete skip_;
64 delete comments_;
65 }
66
67 void Scanner::Skip(const char* re) {
68 delete skip_;
69 if (re != NULL) {
70 skip_ = new RE(re);
71 should_skip_ = true;
72 ConsumeSkip();
73 } else {
74 skip_ = NULL;
75 should_skip_ = false;
76 }
77 }
78
79 void Scanner::DisableSkip() {
80 assert(skip_ != NULL);
81 should_skip_ = false;
82 }
83
84 void Scanner::EnableSkip() {
85 assert(skip_ != NULL);
86 should_skip_ = true;
87 ConsumeSkip();
88 }
89
90 int Scanner::LineNumber() const {
91 // TODO: Make it more efficient by keeping track of the last point
92 // where we computed line numbers and counting newlines since then.
93 return 1 + std::count(data_.data(), input_.data(), '\n');
94 }
95
96 int Scanner::Offset() const {
97 return input_.data() - data_.c_str();
98 }
99
100 bool Scanner::LookingAt(const RE& re) const {
101 int consumed;
102 return re.DoMatch(input_, RE::ANCHOR_START, &consumed, 0, 0);
103 }
104
105
106 bool Scanner::Consume(const RE& re,
107 const Arg& arg0,
108 const Arg& arg1,
109 const Arg& arg2) {
110 const bool result = re.Consume(&input_, arg0, arg1, arg2);
111 if (result && should_skip_) ConsumeSkip();
112 return result;
113 }
114
115 // helper function to consume *skip_ and honour save_comments_
116 void Scanner::ConsumeSkip() {
117 if (save_comments_) {
118 if (NULL == comments_) {
119 comments_ = new vector<StringPiece>;
120 }
121 const char *start_data = input_.data();
122 skip_->Consume(&input_);
123 // already pointing one past end, so no need to +1
124 int length = input_.data() - start_data;
125 if (length > 0) {
126 comments_->push_back(StringPiece(start_data, length));
127 }
128 } else {
129 skip_->Consume(&input_);
130 }
131 }
132
133
134 void Scanner::GetComments(int start, int end, vector<StringPiece> *ranges) {
135 // short circuit out if we've not yet initialized comments_
136 // (e.g., when save_comments is false)
137 if (!comments_) {
138 return;
139 }
140 // TODO: if we guarantee that comments_ will contain StringPieces
141 // that are ordered by their start, then we can do a binary search
142 // for the first StringPiece at or past start and then scan for the
143 // ones contained in the range, quit early (use equal_range or
144 // lower_bound)
145 for (vector<StringPiece>::const_iterator it = comments_->begin();
146 it != comments_->end(); ++it) {
147 if ((it->data() >= data_.c_str() + start &&
148 it->data() + it->size() <= data_.c_str() + end)) {
149 ranges->push_back(*it);
150 }
151 }
152 }
153
154
155 void Scanner::GetNextComments(vector<StringPiece> *ranges) {
156 // short circuit out if we've not yet initialized comments_
157 // (e.g., when save_comments is false)
158 if (!comments_) {
159 return;
160 }
161 for (vector<StringPiece>::const_iterator it =
162 comments_->begin() + comments_offset_;
163 it != comments_->end(); ++it) {
164 ranges->push_back(*it);
165 ++comments_offset_;
166 }
167 }
168
169 } // namespace pcrecpp

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12