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

Diff of /code/trunk/pcre_scanner.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 77 by nigel, Sat Feb 24 21:40:45 2007 UTC revision 96 by nigel, Fri Mar 2 13:10:43 2007 UTC
# Line 30  Line 30 
30  // Author: Sanjay Ghemawat  // Author: Sanjay Ghemawat
31    
32  #include <vector>  #include <vector>
 #include <algorithm>     // for count()  
33  #include <assert.h>  #include <assert.h>
34  #include "config.h"  #include "config.h"
35  #include "pcre_scanner.h"  #include "pcre_scanner.h"
# Line 44  Scanner::Scanner() Line 43  Scanner::Scanner()
43      input_(data_),      input_(data_),
44      skip_(NULL),      skip_(NULL),
45      should_skip_(false),      should_skip_(false),
46        skip_repeat_(false),
47      save_comments_(false),      save_comments_(false),
48      comments_(NULL),      comments_(NULL),
49      comments_offset_(0) {      comments_offset_(0) {
# Line 54  Scanner::Scanner(const string& in) Line 54  Scanner::Scanner(const string& in)
54      input_(data_),      input_(data_),
55      skip_(NULL),      skip_(NULL),
56      should_skip_(false),      should_skip_(false),
57        skip_repeat_(false),
58      save_comments_(false),      save_comments_(false),
59      comments_(NULL),      comments_(NULL),
60      comments_offset_(0) {      comments_offset_(0) {
# Line 64  Scanner::~Scanner() { Line 65  Scanner::~Scanner() {
65    delete comments_;    delete comments_;
66  }  }
67    
68    void Scanner::SetSkipExpression(const char* re) {
69      delete skip_;
70      if (re != NULL) {
71        skip_ = new RE(re);
72        should_skip_ = true;
73        skip_repeat_ = true;
74        ConsumeSkip();
75      } else {
76        skip_ = NULL;
77        should_skip_ = false;
78        skip_repeat_ = false;
79      }
80    }
81    
82  void Scanner::Skip(const char* re) {  void Scanner::Skip(const char* re) {
83    delete skip_;    delete skip_;
84    if (re != NULL) {    if (re != NULL) {
85      skip_ = new RE(re);      skip_ = new RE(re);
86      should_skip_ = true;      should_skip_ = true;
87        skip_repeat_ = false;
88      ConsumeSkip();      ConsumeSkip();
89    } else {    } else {
90      skip_ = NULL;      skip_ = NULL;
91      should_skip_ = false;      should_skip_ = false;
92        skip_repeat_ = false;
93    }    }
94  }  }
95    
# Line 90  void Scanner::EnableSkip() { Line 107  void Scanner::EnableSkip() {
107  int Scanner::LineNumber() const {  int Scanner::LineNumber() const {
108    // TODO: Make it more efficient by keeping track of the last point    // TODO: Make it more efficient by keeping track of the last point
109    // where we computed line numbers and counting newlines since then.    // where we computed line numbers and counting newlines since then.
110    return 1 + std::count(data_.data(), input_.data(), '\n');    // We could use std:count, but not all systems have it. :-(
111      int count = 1;
112      for (const char* p = data_.data(); p < input_.data(); ++p)
113        if (*p == '\n')
114          ++count;
115      return count;
116  }  }
117    
118  int Scanner::Offset() const {  int Scanner::Offset() const {
# Line 114  bool Scanner::Consume(const RE& re, Line 136  bool Scanner::Consume(const RE& re,
136    
137  // helper function to consume *skip_ and honour save_comments_  // helper function to consume *skip_ and honour save_comments_
138  void Scanner::ConsumeSkip() {  void Scanner::ConsumeSkip() {
139      const char* start_data = input_.data();
140      while (skip_->Consume(&input_)) {
141        if (!skip_repeat_) {
142          // Only one skip allowed.
143          break;
144        }
145      }
146    if (save_comments_) {    if (save_comments_) {
147      if (NULL == comments_) {      if (comments_ == NULL) {
148        comments_ = new vector<StringPiece>;        comments_ = new vector<StringPiece>;
149      }      }
     const char *start_data = input_.data();  
     skip_->Consume(&input_);  
150      // already pointing one past end, so no need to +1      // already pointing one past end, so no need to +1
151      int length = input_.data() - start_data;      int length = input_.data() - start_data;
152      if (length > 0) {      if (length > 0) {
153        comments_->push_back(StringPiece(start_data, length));        comments_->push_back(StringPiece(start_data, length));
154      }      }
   } else {  
     skip_->Consume(&input_);  
155    }    }
156  }  }
157    

Legend:
Removed from v.77  
changed lines
  Added in v.96

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12