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

Diff of /code/trunk/pcrecpp.cc

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

revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC revision 179 by ph10, Wed Jun 13 08:53:45 2007 UTC
# Line 29  Line 29 
29  //  //
30  // Author: Sanjay Ghemawat  // Author: Sanjay Ghemawat
31    
32    #ifdef HAVE_CONFIG_H
33    #  include <config.h>
34    #endif
35    
36  #include <stdlib.h>  #include <stdlib.h>
37  #include <stdio.h>  #include <stdio.h>
38  #include <ctype.h>  #include <ctype.h>
# Line 37  Line 41 
41  #include <errno.h>  #include <errno.h>
42  #include <string>  #include <string>
43  #include <algorithm>  #include <algorithm>
 #include "config.h"  
44  // We need this to compile the proper dll on windows/msys.  This is copied  // We need this to compile the proper dll on windows/msys.  This is copied
45  // from pcre_internal.h.  It would probably be better just to include that.  // from pcre_internal.h.  It would probably be better just to include that.
46  #define PCRE_DEFINITION  /* Win32 __declspec(export) trigger for .dll */  #define PCRE_DEFINITION  /* Win32 __declspec(export) trigger for .dll */
47  #include "pcre.h"  #include <pcre.h>
48  #include "pcre_stringpiece.h"  #include "pcre_stringpiece.h"
49  #include "pcrecpp.h"  #include "pcrecpp.h"
50    
# Line 61  static const string empty_string; Line 64  static const string empty_string;
64  // If the user doesn't ask for any options, we just use this one  // If the user doesn't ask for any options, we just use this one
65  static RE_Options default_options;  static RE_Options default_options;
66    
67  void RE::Init(const char* pat, const RE_Options* options) {  void RE::Init(const string& pat, const RE_Options* options) {
68    pattern_ = pat;    pattern_ = pat;
69    if (options == NULL) {    if (options == NULL) {
70      options_ = default_options;      options_ = default_options;
# Line 74  void RE::Init(const char* pat, const RE_ Line 77  void RE::Init(const char* pat, const RE_
77    
78    re_partial_ = Compile(UNANCHORED);    re_partial_ = Compile(UNANCHORED);
79    if (re_partial_ != NULL) {    if (re_partial_ != NULL) {
80      // Check for complicated patterns.  The following change is      re_full_ = Compile(ANCHOR_BOTH);
     // conservative in that it may treat some "simple" patterns  
     // as "complex" (e.g., if the vertical bar is in a character  
     // class or is escaped).  But it seems good enough.  
     if (strchr(pat, '|') == NULL) {  
       // Simple pattern: we can use position-based checks to perform  
       // fully anchored matches  
       re_full_ = re_partial_;  
     } else {  
       // We need a special pattern for anchored matches  
       re_full_ = Compile(ANCHOR_BOTH);  
     }  
81    }    }
82  }  }
83    
84    void RE::Cleanup() {
85      if (re_full_ != NULL)         (*pcre_free)(re_full_);
86      if (re_partial_ != NULL)      (*pcre_free)(re_partial_);
87      if (error_ != &empty_string)  delete error_;
88    }
89    
90    
91  RE::~RE() {  RE::~RE() {
92    if (re_full_ != NULL && re_full_ != re_partial_) (*pcre_free)(re_full_);    Cleanup();
   if (re_partial_ != NULL)                         (*pcre_free)(re_partial_);  
   if (error_ != &empty_string)                     delete error_;  
93  }  }
94    
95    
96  pcre* RE::Compile(Anchor anchor) {  pcre* RE::Compile(Anchor anchor) {
97    // First, convert RE_Options into pcre options    // First, convert RE_Options into pcre options
98    int pcre_options = 0;    int pcre_options = 0;
# Line 424  bool RE::Extract(const StringPiece& rewr Line 422  bool RE::Extract(const StringPiece& rewr
422    return Rewrite(out, rewrite, text, vec, matches);    return Rewrite(out, rewrite, text, vec, matches);
423  }  }
424    
425    /*static*/ string RE::QuoteMeta(const StringPiece& unquoted) {
426      string result;
427    
428      // Escape any ascii character not in [A-Za-z_0-9].
429      //
430      // Note that it's legal to escape a character even if it has no
431      // special meaning in a regular expression -- so this function does
432      // that.  (This also makes it identical to the perl function of the
433      // same name; see `perldoc -f quotemeta`.)
434      for (int ii = 0; ii < unquoted.size(); ++ii) {
435        // Note that using 'isalnum' here raises the benchmark time from
436        // 32ns to 58ns:
437        if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
438            (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
439            (unquoted[ii] < '0' || unquoted[ii] > '9') &&
440            unquoted[ii] != '_' &&
441            // If this is the part of a UTF8 or Latin1 character, we need
442            // to copy this byte without escaping.  Experimentally this is
443            // what works correctly with the regexp library.
444            !(unquoted[ii] & 128)) {
445          result += '\\';
446        }
447        result += unquoted[ii];
448      }
449    
450      return result;
451    }
452    
453  /***** Actual matching and rewriting code *****/  /***** Actual matching and rewriting code *****/
454    
455  int RE::TryMatch(const StringPiece& text,  int RE::TryMatch(const StringPiece& text,
# Line 470  int RE::TryMatch(const StringPiece& text Line 496  int RE::TryMatch(const StringPiece& text
496      rc = vecsize / 2;      rc = vecsize / 2;
497    }    }
498    
   if ((anchor == ANCHOR_BOTH) && (re_full_ == re_partial_)) {  
     // We need an extra check to make sure that the match extended  
     // to the end of the input string  
     assert(vec[0] == 0);                 // PCRE_ANCHORED forces starting match  
     if (vec[1] != text.size()) return 0; // Did not get ending match  
   }  
   
499    return rc;    return rc;
500  }  }
501    
# Line 809  bool Arg::parse_float(const char* str, i Line 828  bool Arg::parse_float(const char* str, i
828      return parse_##name##_radix(str, n, dest, 0);                       \      return parse_##name##_radix(str, n, dest, 0);                       \
829    }    }
830    
831  DEFINE_INTEGER_PARSERS(short);  DEFINE_INTEGER_PARSERS(short)      /*                                   */
832  DEFINE_INTEGER_PARSERS(ushort);  DEFINE_INTEGER_PARSERS(ushort)     /*                                   */
833  DEFINE_INTEGER_PARSERS(int);  DEFINE_INTEGER_PARSERS(int)        /* Don't use semicolons after these  */
834  DEFINE_INTEGER_PARSERS(uint);  DEFINE_INTEGER_PARSERS(uint)       /* statements because they can cause */
835  DEFINE_INTEGER_PARSERS(long);  DEFINE_INTEGER_PARSERS(long)       /* compiler warnings if the checking */
836  DEFINE_INTEGER_PARSERS(ulong);  DEFINE_INTEGER_PARSERS(ulong)      /* level is turned up high enough.   */
837  DEFINE_INTEGER_PARSERS(longlong);  DEFINE_INTEGER_PARSERS(longlong)   /*                                   */
838  DEFINE_INTEGER_PARSERS(ulonglong);  DEFINE_INTEGER_PARSERS(ulonglong)  /*                                   */
839    
840  #undef DEFINE_INTEGER_PARSERS  #undef DEFINE_INTEGER_PARSERS
841    

Legend:
Removed from v.91  
changed lines
  Added in v.179

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12