/[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 81 by nigel, Sat Feb 24 21:40:59 2007 UTC revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC
# Line 332  bool RE::Replace(const StringPiece& rewr Line 332  bool RE::Replace(const StringPiece& rewr
332    return true;    return true;
333  }  }
334    
335    // Returns PCRE_NEWLINE_CRLF, PCRE_NEWLINE_CR, or PCRE_NEWLINE_LF.
336    // Note that PCRE_NEWLINE_CRLF is defined to be P_N_CR | P_N_LF.
337    static int NewlineMode(int pcre_options) {
338      // TODO: if we can make it threadsafe, cache this var
339      int newline_mode = 0;
340      /* if (newline_mode) return newline_mode; */  // do this once it's cached
341      if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF)) {
342        newline_mode = (pcre_options &
343                        (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF));
344      } else {
345        int newline;
346        pcre_config(PCRE_CONFIG_NEWLINE, &newline);
347        if (newline == 10)
348          newline_mode = PCRE_NEWLINE_LF;
349        else if (newline == 13)
350          newline_mode = PCRE_NEWLINE_CR;
351        else if (newline == 3338)
352          newline_mode = PCRE_NEWLINE_CRLF;
353        else
354          assert("" == "Unexpected return value from pcre_config(NEWLINE)");
355      }
356      return newline_mode;
357    }
358    
359  int RE::GlobalReplace(const StringPiece& rewrite,  int RE::GlobalReplace(const StringPiece& rewrite,
360                        string *str) const {                        string *str) const {
361    int count = 0;    int count = 0;
# Line 350  int RE::GlobalReplace(const StringPiece& Line 374  int RE::GlobalReplace(const StringPiece&
374      if (matchstart == matchend && matchstart == lastend) {      if (matchstart == matchend && matchstart == lastend) {
375        // advance one character if we matched an empty string at the same        // advance one character if we matched an empty string at the same
376        // place as the last match occurred        // place as the last match occurred
377        if (start < static_cast<int>(str->length()))        matchend = start + 1;
378          out.push_back((*str)[start]);        // If the current char is CR and we're in CRLF mode, skip LF too.
379        start++;        // Note it's better to call pcre_fullinfo() than to examine
380          // all_options(), since options_ could have changed bewteen
381          // compile-time and now, but this is simpler and safe enough.
382          if (start+1 < static_cast<int>(str->length()) &&
383              (*str)[start] == '\r' && (*str)[start+1] == '\n' &&
384              NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF) {
385            matchend++;
386          }
387          // We also need to advance more than one char if we're in utf8 mode.
388    #ifdef SUPPORT_UTF8
389          if (options_.utf8()) {
390            while (matchend < static_cast<int>(str->length()) &&
391                   ((*str)[matchend] & 0xc0) == 0x80)
392              matchend++;
393          }
394    #endif
395          if (matchend <= static_cast<int>(str->length()))
396            out.append(*str, start, matchend - start);
397          start = matchend;
398      } else {      } else {
399        out.append(*str, start, matchstart - start);        out.append(*str, start, matchstart - start);
400        Rewrite(&out, rewrite, *str, vec, matches);        Rewrite(&out, rewrite, *str, vec, matches);
# Line 397  int RE::TryMatch(const StringPiece& text Line 439  int RE::TryMatch(const StringPiece& text
439    
440    pcre_extra extra = { 0 };    pcre_extra extra = { 0 };
441    if (options_.match_limit() > 0) {    if (options_.match_limit() > 0) {
442      extra.flags = PCRE_EXTRA_MATCH_LIMIT;      extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
443      extra.match_limit = options_.match_limit();      extra.match_limit = options_.match_limit();
444    }    }
445      if (options_.match_limit_recursion() > 0) {
446        extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
447        extra.match_limit_recursion = options_.match_limit_recursion();
448      }
449    int rc = pcre_exec(re,              // The regular expression object    int rc = pcre_exec(re,              // The regular expression object
450                       &extra,                       &extra,
451                       text.data(),                       (text.data() == NULL) ? "" : text.data(),
452                       text.size(),                       text.size(),
453                       startpos,                       startpos,
454                       (anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,                       (anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
# Line 449  bool RE::DoMatchImpl(const StringPiece& Line 495  bool RE::DoMatchImpl(const StringPiece&
495    
496    *consumed = vec[1];    *consumed = vec[1];
497    
498    if (args == NULL) {    if (n == 0 || args == NULL) {
499      // We are not interested in results      // We are not interested in results
500      return true;      return true;
501    }    }
502    
503      if (NumberOfCapturingGroups() < n) {
504        // RE has fewer capturing groups than number of arg pointers passed in
505        return false;
506      }
507    
508    // If we got here, we must have matched the whole pattern.    // If we got here, we must have matched the whole pattern.
509    // We do not need (can not do) any more checks on the value of 'matches' here    // We do not need (can not do) any more checks on the value of 'matches' here
510    // -- see the comment for TryMatch.    // -- see the comment for TryMatch.
# Line 517  bool RE::Rewrite(string *out, const Stri Line 568  bool RE::Rewrite(string *out, const Stri
568    
569  // Return the number of capturing subpatterns, or -1 if the  // Return the number of capturing subpatterns, or -1 if the
570  // regexp wasn't valid on construction.  // regexp wasn't valid on construction.
571  int RE::NumberOfCapturingGroups() {  int RE::NumberOfCapturingGroups() const {
572    if (re_partial_ == NULL) return -1;    if (re_partial_ == NULL) return -1;
573    
574    int result;    int result;
# Line 613  bool Arg::parse_ulong_radix(const char* Line 664  bool Arg::parse_ulong_radix(const char*
664    if (n == 0) return false;    if (n == 0) return false;
665    char buf[kMaxNumberLength+1];    char buf[kMaxNumberLength+1];
666    str = TerminateNumber(buf, str, n);    str = TerminateNumber(buf, str, n);
667      if (str[0] == '-') return false;    // strtoul() on a negative number?!
668    char* end;    char* end;
669    errno = 0;    errno = 0;
670    unsigned long r = strtoul(str, &end, radix);    unsigned long r = strtoul(str, &end, radix);
# Line 702  bool Arg::parse_ulonglong_radix(const ch Line 754  bool Arg::parse_ulonglong_radix(const ch
754    if (n == 0) return false;    if (n == 0) return false;
755    char buf[kMaxNumberLength+1];    char buf[kMaxNumberLength+1];
756    str = TerminateNumber(buf, str, n);    str = TerminateNumber(buf, str, n);
757      if (str[0] == '-') return false;    // strtoull() on a negative number?!
758    char* end;    char* end;
759    errno = 0;    errno = 0;
760  #if defined HAVE_STRTOQ  #if defined HAVE_STRTOQ

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12