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

Contents of /code/trunk/pcre_scanner.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (hide annotations) (download)
Sat Feb 24 21:41:21 2007 UTC (7 years, 6 months ago) by nigel
File size: 5058 byte(s)
Load pcre-6.5 into code/trunk.

1 nigel 77 // 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 <assert.h>
34     #include "config.h"
35     #include "pcre_scanner.h"
36    
37     using std::vector;
38    
39     namespace pcrecpp {
40    
41     Scanner::Scanner()
42     : data_(),
43     input_(data_),
44     skip_(NULL),
45     should_skip_(false),
46     save_comments_(false),
47     comments_(NULL),
48     comments_offset_(0) {
49     }
50    
51     Scanner::Scanner(const string& in)
52     : data_(in),
53     input_(data_),
54     skip_(NULL),
55     should_skip_(false),
56     save_comments_(false),
57     comments_(NULL),
58     comments_offset_(0) {
59     }
60    
61     Scanner::~Scanner() {
62     delete skip_;
63     delete comments_;
64     }
65    
66     void Scanner::Skip(const char* re) {
67     delete skip_;
68     if (re != NULL) {
69     skip_ = new RE(re);
70     should_skip_ = true;
71     ConsumeSkip();
72     } else {
73     skip_ = NULL;
74     should_skip_ = false;
75     }
76     }
77    
78     void Scanner::DisableSkip() {
79     assert(skip_ != NULL);
80     should_skip_ = false;
81     }
82    
83     void Scanner::EnableSkip() {
84     assert(skip_ != NULL);
85     should_skip_ = true;
86     ConsumeSkip();
87     }
88    
89     int Scanner::LineNumber() const {
90     // TODO: Make it more efficient by keeping track of the last point
91     // where we computed line numbers and counting newlines since then.
92 nigel 87 // We could use std:count, but not all systems have it. :-(
93     int count = 1;
94     for (const char* p = data_.data(); p < input_.data(); ++p)
95     if (*p == '\n')
96     ++count;
97     return count;
98 nigel 77 }
99    
100     int Scanner::Offset() const {
101     return input_.data() - data_.c_str();
102     }
103    
104     bool Scanner::LookingAt(const RE& re) const {
105     int consumed;
106     return re.DoMatch(input_, RE::ANCHOR_START, &consumed, 0, 0);
107     }
108    
109    
110     bool Scanner::Consume(const RE& re,
111     const Arg& arg0,
112     const Arg& arg1,
113     const Arg& arg2) {
114     const bool result = re.Consume(&input_, arg0, arg1, arg2);
115     if (result && should_skip_) ConsumeSkip();
116     return result;
117     }
118    
119     // helper function to consume *skip_ and honour save_comments_
120     void Scanner::ConsumeSkip() {
121     if (save_comments_) {
122     if (NULL == comments_) {
123     comments_ = new vector<StringPiece>;
124     }
125     const char *start_data = input_.data();
126     skip_->Consume(&input_);
127     // already pointing one past end, so no need to +1
128     int length = input_.data() - start_data;
129     if (length > 0) {
130     comments_->push_back(StringPiece(start_data, length));
131     }
132     } else {
133     skip_->Consume(&input_);
134     }
135     }
136    
137    
138     void Scanner::GetComments(int start, int end, vector<StringPiece> *ranges) {
139     // short circuit out if we've not yet initialized comments_
140     // (e.g., when save_comments is false)
141     if (!comments_) {
142     return;
143     }
144     // TODO: if we guarantee that comments_ will contain StringPieces
145     // that are ordered by their start, then we can do a binary search
146     // for the first StringPiece at or past start and then scan for the
147     // ones contained in the range, quit early (use equal_range or
148     // lower_bound)
149     for (vector<StringPiece>::const_iterator it = comments_->begin();
150     it != comments_->end(); ++it) {
151     if ((it->data() >= data_.c_str() + start &&
152     it->data() + it->size() <= data_.c_str() + end)) {
153     ranges->push_back(*it);
154     }
155     }
156     }
157    
158    
159     void Scanner::GetNextComments(vector<StringPiece> *ranges) {
160     // short circuit out if we've not yet initialized comments_
161     // (e.g., when save_comments is false)
162     if (!comments_) {
163     return;
164     }
165     for (vector<StringPiece>::const_iterator it =
166     comments_->begin() + comments_offset_;
167     it != comments_->end(); ++it) {
168     ranges->push_back(*it);
169     ++comments_offset_;
170     }
171     }
172    
173     } // namespace pcrecpp

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12