Blame pcrecpp.cc

Packit 78a954
// Copyright (c) 2010, Google Inc.
Packit 78a954
// All rights reserved.
Packit 78a954
//
Packit 78a954
// Redistribution and use in source and binary forms, with or without
Packit 78a954
// modification, are permitted provided that the following conditions are
Packit 78a954
// met:
Packit 78a954
//
Packit 78a954
//     * Redistributions of source code must retain the above copyright
Packit 78a954
// notice, this list of conditions and the following disclaimer.
Packit 78a954
//     * Redistributions in binary form must reproduce the above
Packit 78a954
// copyright notice, this list of conditions and the following disclaimer
Packit 78a954
// in the documentation and/or other materials provided with the
Packit 78a954
// distribution.
Packit 78a954
//     * Neither the name of Google Inc. nor the names of its
Packit 78a954
// contributors may be used to endorse or promote products derived from
Packit 78a954
// this software without specific prior written permission.
Packit 78a954
//
Packit 78a954
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 78a954
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 78a954
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 78a954
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 78a954
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 78a954
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 78a954
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 78a954
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 78a954
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 78a954
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 78a954
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 78a954
//
Packit 78a954
// Author: Sanjay Ghemawat
Packit 78a954
Packit 78a954
#ifdef HAVE_CONFIG_H
Packit 78a954
#include "config.h"
Packit 78a954
#endif
Packit 78a954
Packit 78a954
#include <stdlib.h>
Packit 78a954
#include <stdio.h>
Packit 78a954
#include <ctype.h>
Packit 78a954
#include <limits.h>      /* for SHRT_MIN, USHRT_MAX, etc */
Packit 78a954
#include <string.h>      /* for memcpy */
Packit 78a954
#include <assert.h>
Packit 78a954
#include <errno.h>
Packit 78a954
#include <string>
Packit 78a954
#include <algorithm>
Packit 78a954
Packit 78a954
#include "pcrecpp_internal.h"
Packit 78a954
#include "pcre.h"
Packit 78a954
#include "pcrecpp.h"
Packit 78a954
#include "pcre_stringpiece.h"
Packit 78a954
Packit 78a954
Packit 78a954
namespace pcrecpp {
Packit 78a954
Packit 78a954
// Maximum number of args we can set
Packit 78a954
static const int kMaxArgs = 16;
Packit 78a954
static const int kVecSize = (1 + kMaxArgs) * 3;  // results + PCRE workspace
Packit 78a954
Packit 78a954
// Special object that stands-in for no argument
Packit 78a954
Arg RE::no_arg((void*)NULL);
Packit 78a954
Packit 78a954
// This is for ABI compatibility with old versions of pcre (pre-7.6),
Packit 78a954
// which defined a global no_arg variable instead of putting it in the
Packit 78a954
// RE class.  This works on GCC >= 3, at least.  It definitely works
Packit 78a954
// for ELF, but may not for other object formats (Mach-O, for
Packit 78a954
// instance, does not support aliases.)  We could probably have a more
Packit 78a954
// inclusive test if we ever needed it.  (Note that not only the
Packit 78a954
// __attribute__ syntax, but also __USER_LABEL_PREFIX__, are
Packit 78a954
// gnu-specific.)
Packit 78a954
#if defined(__GNUC__) && __GNUC__ >= 3 && defined(__ELF__)
Packit 78a954
# define ULP_AS_STRING(x)            ULP_AS_STRING_INTERNAL(x)
Packit 78a954
# define ULP_AS_STRING_INTERNAL(x)   #x
Packit 78a954
# define USER_LABEL_PREFIX_STR       ULP_AS_STRING(__USER_LABEL_PREFIX__)
Packit 78a954
extern Arg no_arg
Packit 78a954
  __attribute__((alias(USER_LABEL_PREFIX_STR "_ZN7pcrecpp2RE6no_argE")));
Packit 78a954
#endif
Packit 78a954
Packit 78a954
// If a regular expression has no error, its error_ field points here
Packit 78a954
static const string empty_string;
Packit 78a954
Packit 78a954
// If the user doesn't ask for any options, we just use this one
Packit 78a954
static RE_Options default_options;
Packit 78a954
Packit 78a954
void RE::Init(const string& pat, const RE_Options* options) {
Packit 78a954
  pattern_ = pat;
Packit 78a954
  if (options == NULL) {
Packit 78a954
    options_ = default_options;
Packit 78a954
  } else {
Packit 78a954
    options_ = *options;
Packit 78a954
  }
Packit 78a954
  error_ = &empty_string;
Packit 78a954
  re_full_ = NULL;
Packit 78a954
  re_partial_ = NULL;
Packit 78a954
Packit 78a954
  re_partial_ = Compile(UNANCHORED);
Packit 78a954
  if (re_partial_ != NULL) {
Packit 78a954
    re_full_ = Compile(ANCHOR_BOTH);
Packit 78a954
  }
Packit 78a954
}
Packit 78a954
Packit 78a954
void RE::Cleanup() {
Packit 78a954
  if (re_full_ != NULL)         (*pcre_free)(re_full_);
Packit 78a954
  if (re_partial_ != NULL)      (*pcre_free)(re_partial_);
Packit 78a954
  if (error_ != &empty_string)  delete error_;
Packit 78a954
}
Packit 78a954
Packit 78a954
Packit 78a954
RE::~RE() {
Packit 78a954
  Cleanup();
Packit 78a954
}
Packit 78a954
Packit 78a954
Packit 78a954
pcre* RE::Compile(Anchor anchor) {
Packit 78a954
  // First, convert RE_Options into pcre options
Packit 78a954
  int pcre_options = 0;
Packit 78a954
  pcre_options = options_.all_options();
Packit 78a954
Packit 78a954
  // Special treatment for anchoring.  This is needed because at
Packit 78a954
  // runtime pcre only provides an option for anchoring at the
Packit 78a954
  // beginning of a string (unless you use offset).
Packit 78a954
  //
Packit 78a954
  // There are three types of anchoring we want:
Packit 78a954
  //    UNANCHORED      Compile the original pattern, and use
Packit 78a954
  //                    a pcre unanchored match.
Packit 78a954
  //    ANCHOR_START    Compile the original pattern, and use
Packit 78a954
  //                    a pcre anchored match.
Packit 78a954
  //    ANCHOR_BOTH     Tack a "\z" to the end of the original pattern
Packit 78a954
  //                    and use a pcre anchored match.
Packit 78a954
Packit 78a954
  const char* compile_error;
Packit 78a954
  int eoffset;
Packit 78a954
  pcre* re;
Packit 78a954
  if (anchor != ANCHOR_BOTH) {
Packit 78a954
    re = pcre_compile(pattern_.c_str(), pcre_options,
Packit 78a954
                      &compile_error, &eoffset, NULL);
Packit 78a954
  } else {
Packit 78a954
    // Tack a '\z' at the end of RE.  Parenthesize it first so that
Packit 78a954
    // the '\z' applies to all top-level alternatives in the regexp.
Packit 78a954
    string wrapped = "(?:";  // A non-counting grouping operator
Packit 78a954
    wrapped += pattern_;
Packit 78a954
    wrapped += ")\\z";
Packit 78a954
    re = pcre_compile(wrapped.c_str(), pcre_options,
Packit 78a954
                      &compile_error, &eoffset, NULL);
Packit 78a954
  }
Packit 78a954
  if (re == NULL) {
Packit 78a954
    if (error_ == &empty_string) error_ = new string(compile_error);
Packit 78a954
  }
Packit 78a954
  return re;
Packit 78a954
}
Packit 78a954
Packit 78a954
/***** Matching interfaces *****/
Packit 78a954
Packit 78a954
bool RE::FullMatch(const StringPiece& text,
Packit 78a954
                   const Arg& ptr1,
Packit 78a954
                   const Arg& ptr2,
Packit 78a954
                   const Arg& ptr3,
Packit 78a954
                   const Arg& ptr4,
Packit 78a954
                   const Arg& ptr5,
Packit 78a954
                   const Arg& ptr6,
Packit 78a954
                   const Arg& ptr7,
Packit 78a954
                   const Arg& ptr8,
Packit 78a954
                   const Arg& ptr9,
Packit 78a954
                   const Arg& ptr10,
Packit 78a954
                   const Arg& ptr11,
Packit 78a954
                   const Arg& ptr12,
Packit 78a954
                   const Arg& ptr13,
Packit 78a954
                   const Arg& ptr14,
Packit 78a954
                   const Arg& ptr15,
Packit 78a954
                   const Arg& ptr16) const {
Packit 78a954
  const Arg* args[kMaxArgs];
Packit 78a954
  int n = 0;
Packit 78a954
  if (&ptr1  == &no_arg) goto done; args[n++] = &ptr1;
Packit 78a954
  if (&ptr2  == &no_arg) goto done; args[n++] = &ptr2;
Packit 78a954
  if (&ptr3  == &no_arg) goto done; args[n++] = &ptr3;
Packit 78a954
  if (&ptr4  == &no_arg) goto done; args[n++] = &ptr4;
Packit 78a954
  if (&ptr5  == &no_arg) goto done; args[n++] = &ptr5;
Packit 78a954
  if (&ptr6  == &no_arg) goto done; args[n++] = &ptr6;
Packit 78a954
  if (&ptr7  == &no_arg) goto done; args[n++] = &ptr7;
Packit 78a954
  if (&ptr8  == &no_arg) goto done; args[n++] = &ptr8;
Packit 78a954
  if (&ptr9  == &no_arg) goto done; args[n++] = &ptr9;
Packit 78a954
  if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
Packit 78a954
  if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
Packit 78a954
  if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
Packit 78a954
  if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
Packit 78a954
  if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
Packit 78a954
  if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
Packit 78a954
  if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
Packit 78a954
 done:
Packit 78a954
Packit 78a954
  int consumed;
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::PartialMatch(const StringPiece& text,
Packit 78a954
                      const Arg& ptr1,
Packit 78a954
                      const Arg& ptr2,
Packit 78a954
                      const Arg& ptr3,
Packit 78a954
                      const Arg& ptr4,
Packit 78a954
                      const Arg& ptr5,
Packit 78a954
                      const Arg& ptr6,
Packit 78a954
                      const Arg& ptr7,
Packit 78a954
                      const Arg& ptr8,
Packit 78a954
                      const Arg& ptr9,
Packit 78a954
                      const Arg& ptr10,
Packit 78a954
                      const Arg& ptr11,
Packit 78a954
                      const Arg& ptr12,
Packit 78a954
                      const Arg& ptr13,
Packit 78a954
                      const Arg& ptr14,
Packit 78a954
                      const Arg& ptr15,
Packit 78a954
                      const Arg& ptr16) const {
Packit 78a954
  const Arg* args[kMaxArgs];
Packit 78a954
  int n = 0;
Packit 78a954
  if (&ptr1  == &no_arg) goto done; args[n++] = &ptr1;
Packit 78a954
  if (&ptr2  == &no_arg) goto done; args[n++] = &ptr2;
Packit 78a954
  if (&ptr3  == &no_arg) goto done; args[n++] = &ptr3;
Packit 78a954
  if (&ptr4  == &no_arg) goto done; args[n++] = &ptr4;
Packit 78a954
  if (&ptr5  == &no_arg) goto done; args[n++] = &ptr5;
Packit 78a954
  if (&ptr6  == &no_arg) goto done; args[n++] = &ptr6;
Packit 78a954
  if (&ptr7  == &no_arg) goto done; args[n++] = &ptr7;
Packit 78a954
  if (&ptr8  == &no_arg) goto done; args[n++] = &ptr8;
Packit 78a954
  if (&ptr9  == &no_arg) goto done; args[n++] = &ptr9;
Packit 78a954
  if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
Packit 78a954
  if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
Packit 78a954
  if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
Packit 78a954
  if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
Packit 78a954
  if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
Packit 78a954
  if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
Packit 78a954
  if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
Packit 78a954
 done:
Packit 78a954
Packit 78a954
  int consumed;
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::Consume(StringPiece* input,
Packit 78a954
                 const Arg& ptr1,
Packit 78a954
                 const Arg& ptr2,
Packit 78a954
                 const Arg& ptr3,
Packit 78a954
                 const Arg& ptr4,
Packit 78a954
                 const Arg& ptr5,
Packit 78a954
                 const Arg& ptr6,
Packit 78a954
                 const Arg& ptr7,
Packit 78a954
                 const Arg& ptr8,
Packit 78a954
                 const Arg& ptr9,
Packit 78a954
                 const Arg& ptr10,
Packit 78a954
                 const Arg& ptr11,
Packit 78a954
                 const Arg& ptr12,
Packit 78a954
                 const Arg& ptr13,
Packit 78a954
                 const Arg& ptr14,
Packit 78a954
                 const Arg& ptr15,
Packit 78a954
                 const Arg& ptr16) const {
Packit 78a954
  const Arg* args[kMaxArgs];
Packit 78a954
  int n = 0;
Packit 78a954
  if (&ptr1  == &no_arg) goto done; args[n++] = &ptr1;
Packit 78a954
  if (&ptr2  == &no_arg) goto done; args[n++] = &ptr2;
Packit 78a954
  if (&ptr3  == &no_arg) goto done; args[n++] = &ptr3;
Packit 78a954
  if (&ptr4  == &no_arg) goto done; args[n++] = &ptr4;
Packit 78a954
  if (&ptr5  == &no_arg) goto done; args[n++] = &ptr5;
Packit 78a954
  if (&ptr6  == &no_arg) goto done; args[n++] = &ptr6;
Packit 78a954
  if (&ptr7  == &no_arg) goto done; args[n++] = &ptr7;
Packit 78a954
  if (&ptr8  == &no_arg) goto done; args[n++] = &ptr8;
Packit 78a954
  if (&ptr9  == &no_arg) goto done; args[n++] = &ptr9;
Packit 78a954
  if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
Packit 78a954
  if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
Packit 78a954
  if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
Packit 78a954
  if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
Packit 78a954
  if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
Packit 78a954
  if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
Packit 78a954
  if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
Packit 78a954
 done:
Packit 78a954
Packit 78a954
  int consumed;
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  if (DoMatchImpl(*input, ANCHOR_START, &consumed,
Packit 78a954
                  args, n, vec, kVecSize)) {
Packit 78a954
    input->remove_prefix(consumed);
Packit 78a954
    return true;
Packit 78a954
  } else {
Packit 78a954
    return false;
Packit 78a954
  }
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::FindAndConsume(StringPiece* input,
Packit 78a954
                        const Arg& ptr1,
Packit 78a954
                        const Arg& ptr2,
Packit 78a954
                        const Arg& ptr3,
Packit 78a954
                        const Arg& ptr4,
Packit 78a954
                        const Arg& ptr5,
Packit 78a954
                        const Arg& ptr6,
Packit 78a954
                        const Arg& ptr7,
Packit 78a954
                        const Arg& ptr8,
Packit 78a954
                        const Arg& ptr9,
Packit 78a954
                        const Arg& ptr10,
Packit 78a954
                        const Arg& ptr11,
Packit 78a954
                        const Arg& ptr12,
Packit 78a954
                        const Arg& ptr13,
Packit 78a954
                        const Arg& ptr14,
Packit 78a954
                        const Arg& ptr15,
Packit 78a954
                        const Arg& ptr16) const {
Packit 78a954
  const Arg* args[kMaxArgs];
Packit 78a954
  int n = 0;
Packit 78a954
  if (&ptr1  == &no_arg) goto done; args[n++] = &ptr1;
Packit 78a954
  if (&ptr2  == &no_arg) goto done; args[n++] = &ptr2;
Packit 78a954
  if (&ptr3  == &no_arg) goto done; args[n++] = &ptr3;
Packit 78a954
  if (&ptr4  == &no_arg) goto done; args[n++] = &ptr4;
Packit 78a954
  if (&ptr5  == &no_arg) goto done; args[n++] = &ptr5;
Packit 78a954
  if (&ptr6  == &no_arg) goto done; args[n++] = &ptr6;
Packit 78a954
  if (&ptr7  == &no_arg) goto done; args[n++] = &ptr7;
Packit 78a954
  if (&ptr8  == &no_arg) goto done; args[n++] = &ptr8;
Packit 78a954
  if (&ptr9  == &no_arg) goto done; args[n++] = &ptr9;
Packit 78a954
  if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
Packit 78a954
  if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
Packit 78a954
  if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
Packit 78a954
  if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
Packit 78a954
  if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
Packit 78a954
  if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
Packit 78a954
  if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
Packit 78a954
 done:
Packit 78a954
Packit 78a954
  int consumed;
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  if (DoMatchImpl(*input, UNANCHORED, &consumed,
Packit 78a954
                  args, n, vec, kVecSize)) {
Packit 78a954
    input->remove_prefix(consumed);
Packit 78a954
    return true;
Packit 78a954
  } else {
Packit 78a954
    return false;
Packit 78a954
  }
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::Replace(const StringPiece& rewrite,
Packit 78a954
                 string *str) const {
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  int matches = TryMatch(*str, 0, UNANCHORED, true, vec, kVecSize);
Packit 78a954
  if (matches == 0)
Packit 78a954
    return false;
Packit 78a954
Packit 78a954
  string s;
Packit 78a954
  if (!Rewrite(&s, rewrite, *str, vec, matches))
Packit 78a954
    return false;
Packit 78a954
Packit 78a954
  assert(vec[0] >= 0);
Packit 78a954
  assert(vec[1] >= 0);
Packit 78a954
  str->replace(vec[0], vec[1] - vec[0], s);
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
// Returns PCRE_NEWLINE_CRLF, PCRE_NEWLINE_CR, or PCRE_NEWLINE_LF.
Packit 78a954
// Note that PCRE_NEWLINE_CRLF is defined to be P_N_CR | P_N_LF.
Packit 78a954
// Modified by PH to add PCRE_NEWLINE_ANY and PCRE_NEWLINE_ANYCRLF.
Packit 78a954
Packit 78a954
static int NewlineMode(int pcre_options) {
Packit 78a954
  // TODO: if we can make it threadsafe, cache this var
Packit 78a954
  int newline_mode = 0;
Packit 78a954
  /* if (newline_mode) return newline_mode; */  // do this once it's cached
Packit 78a954
  if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
Packit 78a954
                      PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)) {
Packit 78a954
    newline_mode = (pcre_options &
Packit 78a954
                    (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
Packit 78a954
                     PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF));
Packit 78a954
  } else {
Packit 78a954
    int newline;
Packit 78a954
    pcre_config(PCRE_CONFIG_NEWLINE, &newline);
Packit 78a954
    if (newline == 10)
Packit 78a954
      newline_mode = PCRE_NEWLINE_LF;
Packit 78a954
    else if (newline == 13)
Packit 78a954
      newline_mode = PCRE_NEWLINE_CR;
Packit 78a954
    else if (newline == 3338)
Packit 78a954
      newline_mode = PCRE_NEWLINE_CRLF;
Packit 78a954
    else if (newline == -1)
Packit 78a954
      newline_mode = PCRE_NEWLINE_ANY;
Packit 78a954
    else if (newline == -2)
Packit 78a954
      newline_mode = PCRE_NEWLINE_ANYCRLF;
Packit 78a954
    else
Packit 78a954
      assert(NULL == "Unexpected return value from pcre_config(NEWLINE)");
Packit 78a954
  }
Packit 78a954
  return newline_mode;
Packit 78a954
}
Packit 78a954
Packit 78a954
int RE::GlobalReplace(const StringPiece& rewrite,
Packit 78a954
                      string *str) const {
Packit 78a954
  int count = 0;
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  string out;
Packit 78a954
  int start = 0;
Packit 78a954
  bool last_match_was_empty_string = false;
Packit 78a954
Packit 78a954
  while (start <= static_cast<int>(str->length())) {
Packit 78a954
    // If the previous match was for the empty string, we shouldn't
Packit 78a954
    // just match again: we'll match in the same way and get an
Packit 78a954
    // infinite loop.  Instead, we do the match in a special way:
Packit 78a954
    // anchored -- to force another try at the same position --
Packit 78a954
    // and with a flag saying that this time, ignore empty matches.
Packit 78a954
    // If this special match returns, that means there's a non-empty
Packit 78a954
    // match at this position as well, and we can continue.  If not,
Packit 78a954
    // we do what perl does, and just advance by one.
Packit 78a954
    // Notice that perl prints '@@@' for this;
Packit 78a954
    //    perl -le '$_ = "aa"; s/b*|aa/@/g; print'
Packit 78a954
    int matches;
Packit 78a954
    if (last_match_was_empty_string) {
Packit 78a954
      matches = TryMatch(*str, start, ANCHOR_START, false, vec, kVecSize);
Packit 78a954
      if (matches <= 0) {
Packit 78a954
        int matchend = start + 1;     // advance one character.
Packit 78a954
        // If the current char is CR and we're in CRLF mode, skip LF too.
Packit 78a954
        // Note it's better to call pcre_fullinfo() than to examine
Packit 78a954
        // all_options(), since options_ could have changed bewteen
Packit 78a954
        // compile-time and now, but this is simpler and safe enough.
Packit 78a954
        // Modified by PH to add ANY and ANYCRLF.
Packit 78a954
        if (matchend < static_cast<int>(str->length()) &&
Packit 78a954
            (*str)[start] == '\r' && (*str)[matchend] == '\n' &&
Packit 78a954
            (NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF ||
Packit 78a954
             NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANY ||
Packit 78a954
             NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANYCRLF)) {
Packit 78a954
          matchend++;
Packit 78a954
        }
Packit 78a954
        // We also need to advance more than one char if we're in utf8 mode.
Packit 78a954
#ifdef SUPPORT_UTF8
Packit 78a954
        if (options_.utf8()) {
Packit 78a954
          while (matchend < static_cast<int>(str->length()) &&
Packit 78a954
                 ((*str)[matchend] & 0xc0) == 0x80)
Packit 78a954
            matchend++;
Packit 78a954
        }
Packit 78a954
#endif
Packit 78a954
        if (start < static_cast<int>(str->length()))
Packit 78a954
          out.append(*str, start, matchend - start);
Packit 78a954
        start = matchend;
Packit 78a954
        last_match_was_empty_string = false;
Packit 78a954
        continue;
Packit 78a954
      }
Packit 78a954
    } else {
Packit 78a954
      matches = TryMatch(*str, start, UNANCHORED, true, vec, kVecSize);
Packit 78a954
      if (matches <= 0)
Packit 78a954
        break;
Packit 78a954
    }
Packit 78a954
    int matchstart = vec[0], matchend = vec[1];
Packit 78a954
    assert(matchstart >= start);
Packit 78a954
    assert(matchend >= matchstart);
Packit 78a954
    out.append(*str, start, matchstart - start);
Packit 78a954
    Rewrite(&out, rewrite, *str, vec, matches);
Packit 78a954
    start = matchend;
Packit 78a954
    count++;
Packit 78a954
    last_match_was_empty_string = (matchstart == matchend);
Packit 78a954
  }
Packit 78a954
Packit 78a954
  if (count == 0)
Packit 78a954
    return 0;
Packit 78a954
Packit 78a954
  if (start < static_cast<int>(str->length()))
Packit 78a954
    out.append(*str, start, str->length() - start);
Packit 78a954
  swap(out, *str);
Packit 78a954
  return count;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::Extract(const StringPiece& rewrite,
Packit 78a954
                 const StringPiece& text,
Packit 78a954
                 string *out) const {
Packit 78a954
  int vec[kVecSize];
Packit 78a954
  int matches = TryMatch(text, 0, UNANCHORED, true, vec, kVecSize);
Packit 78a954
  if (matches == 0)
Packit 78a954
    return false;
Packit 78a954
  out->erase();
Packit 78a954
  return Rewrite(out, rewrite, text, vec, matches);
Packit 78a954
}
Packit 78a954
Packit 78a954
/*static*/ string RE::QuoteMeta(const StringPiece& unquoted) {
Packit 78a954
  string result;
Packit 78a954
Packit 78a954
  // Escape any ascii character not in [A-Za-z_0-9].
Packit 78a954
  //
Packit 78a954
  // Note that it's legal to escape a character even if it has no
Packit 78a954
  // special meaning in a regular expression -- so this function does
Packit 78a954
  // that.  (This also makes it identical to the perl function of the
Packit 78a954
  // same name; see `perldoc -f quotemeta`.)  The one exception is
Packit 78a954
  // escaping NUL: rather than doing backslash + NUL, like perl does,
Packit 78a954
  // we do '\0', because pcre itself doesn't take embedded NUL chars.
Packit 78a954
  for (int ii = 0; ii < unquoted.size(); ++ii) {
Packit 78a954
    // Note that using 'isalnum' here raises the benchmark time from
Packit 78a954
    // 32ns to 58ns:
Packit 78a954
    if (unquoted[ii] == '\0') {
Packit 78a954
      result += "\\0";
Packit 78a954
    } else if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
Packit 78a954
               (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
Packit 78a954
               (unquoted[ii] < '0' || unquoted[ii] > '9') &&
Packit 78a954
               unquoted[ii] != '_' &&
Packit 78a954
               // If this is the part of a UTF8 or Latin1 character, we need
Packit 78a954
               // to copy this byte without escaping.  Experimentally this is
Packit 78a954
               // what works correctly with the regexp library.
Packit 78a954
               !(unquoted[ii] & 128)) {
Packit 78a954
      result += '\\';
Packit 78a954
      result += unquoted[ii];
Packit 78a954
    } else {
Packit 78a954
      result += unquoted[ii];
Packit 78a954
    }
Packit 78a954
  }
Packit 78a954
Packit 78a954
  return result;
Packit 78a954
}
Packit 78a954
Packit 78a954
/***** Actual matching and rewriting code *****/
Packit 78a954
Packit 78a954
int RE::TryMatch(const StringPiece& text,
Packit 78a954
                 int startpos,
Packit 78a954
                 Anchor anchor,
Packit 78a954
                 bool empty_ok,
Packit 78a954
                 int *vec,
Packit 78a954
                 int vecsize) const {
Packit 78a954
  pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
Packit 78a954
  if (re == NULL) {
Packit 78a954
    //fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
Packit 78a954
    return 0;
Packit 78a954
  }
Packit 78a954
Packit 78a954
  pcre_extra extra = { 0, 0, 0, 0, 0, 0, 0, 0 };
Packit 78a954
  if (options_.match_limit() > 0) {
Packit 78a954
    extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
Packit 78a954
    extra.match_limit = options_.match_limit();
Packit 78a954
  }
Packit 78a954
  if (options_.match_limit_recursion() > 0) {
Packit 78a954
    extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
Packit 78a954
    extra.match_limit_recursion = options_.match_limit_recursion();
Packit 78a954
  }
Packit 78a954
Packit 78a954
  // int options = 0;
Packit 78a954
  // Changed by PH as a result of bugzilla #1288
Packit 78a954
  int options = (options_.all_options() & PCRE_NO_UTF8_CHECK);
Packit 78a954
Packit 78a954
  if (anchor != UNANCHORED)
Packit 78a954
    options |= PCRE_ANCHORED;
Packit 78a954
  if (!empty_ok)
Packit 78a954
    options |= PCRE_NOTEMPTY;
Packit 78a954
Packit 78a954
  int rc = pcre_exec(re,              // The regular expression object
Packit 78a954
                     &extra,
Packit 78a954
                     (text.data() == NULL) ? "" : text.data(),
Packit 78a954
                     text.size(),
Packit 78a954
                     startpos,
Packit 78a954
                     options,
Packit 78a954
                     vec,
Packit 78a954
                     vecsize);
Packit 78a954
Packit 78a954
  // Handle errors
Packit 78a954
  if (rc == PCRE_ERROR_NOMATCH) {
Packit 78a954
    return 0;
Packit 78a954
  } else if (rc < 0) {
Packit 78a954
    //fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
Packit 78a954
    //        re, pattern_.c_str());
Packit 78a954
    return 0;
Packit 78a954
  } else if (rc == 0) {
Packit 78a954
    // pcre_exec() returns 0 as a special case when the number of
Packit 78a954
    // capturing subpatterns exceeds the size of the vector.
Packit 78a954
    // When this happens, there is a match and the output vector
Packit 78a954
    // is filled, but we miss out on the positions of the extra subpatterns.
Packit 78a954
    rc = vecsize / 2;
Packit 78a954
  }
Packit 78a954
Packit 78a954
  return rc;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::DoMatchImpl(const StringPiece& text,
Packit 78a954
                     Anchor anchor,
Packit 78a954
                     int* consumed,
Packit 78a954
                     const Arg* const* args,
Packit 78a954
                     int n,
Packit 78a954
                     int* vec,
Packit 78a954
                     int vecsize) const {
Packit 78a954
  assert((1 + n) * 3 <= vecsize);  // results + PCRE workspace
Packit 78a954
  int matches = TryMatch(text, 0, anchor, true, vec, vecsize);
Packit 78a954
  assert(matches >= 0);  // TryMatch never returns negatives
Packit 78a954
  if (matches == 0)
Packit 78a954
    return false;
Packit 78a954
Packit 78a954
  *consumed = vec[1];
Packit 78a954
Packit 78a954
  if (n == 0 || args == NULL) {
Packit 78a954
    // We are not interested in results
Packit 78a954
    return true;
Packit 78a954
  }
Packit 78a954
Packit 78a954
  if (NumberOfCapturingGroups() < n) {
Packit 78a954
    // RE has fewer capturing groups than number of arg pointers passed in
Packit 78a954
    return false;
Packit 78a954
  }
Packit 78a954
Packit 78a954
  // If we got here, we must have matched the whole pattern.
Packit 78a954
  // We do not need (can not do) any more checks on the value of 'matches' here
Packit 78a954
  // -- see the comment for TryMatch.
Packit 78a954
  for (int i = 0; i < n; i++) {
Packit 78a954
    const int start = vec[2*(i+1)];
Packit 78a954
    const int limit = vec[2*(i+1)+1];
Packit 78a954
    if (!args[i]->Parse(text.data() + start, limit-start)) {
Packit 78a954
      // TODO: Should we indicate what the error was?
Packit 78a954
      return false;
Packit 78a954
    }
Packit 78a954
  }
Packit 78a954
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::DoMatch(const StringPiece& text,
Packit 78a954
                 Anchor anchor,
Packit 78a954
                 int* consumed,
Packit 78a954
                 const Arg* const args[],
Packit 78a954
                 int n) const {
Packit 78a954
  assert(n >= 0);
Packit 78a954
  size_t const vecsize = (1 + n) * 3;  // results + PCRE workspace
Packit 78a954
                                       // (as for kVecSize)
Packit 78a954
  int space[21];   // use stack allocation for small vecsize (common case)
Packit 78a954
  int* vec = vecsize <= 21 ? space : new int[vecsize];
Packit 78a954
  bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, (int)vecsize);
Packit 78a954
  if (vec != space) delete [] vec;
Packit 78a954
  return retval;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool RE::Rewrite(string *out, const StringPiece &rewrite,
Packit 78a954
                 const StringPiece &text, int *vec, int veclen) const {
Packit 78a954
  for (const char *s = rewrite.data(), *end = s + rewrite.size();
Packit 78a954
       s < end; s++) {
Packit 78a954
    int c = *s;
Packit 78a954
    if (c == '\\') {
Packit 78a954
      c = *++s;
Packit 78a954
      if (isdigit(c)) {
Packit 78a954
        int n = (c - '0');
Packit 78a954
        if (n >= veclen) {
Packit 78a954
          //fprintf(stderr, requested group %d in regexp %.*s\n",
Packit 78a954
          //        n, rewrite.size(), rewrite.data());
Packit 78a954
          return false;
Packit 78a954
        }
Packit 78a954
        int start = vec[2 * n];
Packit 78a954
        if (start >= 0)
Packit 78a954
          out->append(text.data() + start, vec[2 * n + 1] - start);
Packit 78a954
      } else if (c == '\\') {
Packit 78a954
        *out += '\\';
Packit 78a954
      } else {
Packit 78a954
        //fprintf(stderr, "invalid rewrite pattern: %.*s\n",
Packit 78a954
        //        rewrite.size(), rewrite.data());
Packit 78a954
        return false;
Packit 78a954
      }
Packit 78a954
    } else {
Packit 78a954
      *out += c;
Packit 78a954
    }
Packit 78a954
  }
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
// Return the number of capturing subpatterns, or -1 if the
Packit 78a954
// regexp wasn't valid on construction.
Packit 78a954
int RE::NumberOfCapturingGroups() const {
Packit 78a954
  if (re_partial_ == NULL) return -1;
Packit 78a954
Packit 78a954
  int result;
Packit 78a954
  int pcre_retval = pcre_fullinfo(re_partial_,  // The regular expression object
Packit 78a954
                                  NULL,         // We did not study the pattern
Packit 78a954
                                  PCRE_INFO_CAPTURECOUNT,
Packit 78a954
                                  &result);
Packit 78a954
  assert(pcre_retval == 0);
Packit 78a954
  return result;
Packit 78a954
}
Packit 78a954
Packit 78a954
/***** Parsers for various types *****/
Packit 78a954
Packit 78a954
bool Arg::parse_null(const char* str, int n, void* dest) {
Packit 78a954
  (void)str;
Packit 78a954
  (void)n;
Packit 78a954
  // We fail if somebody asked us to store into a non-NULL void* pointer
Packit 78a954
  return (dest == NULL);
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_string(const char* str, int n, void* dest) {
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  reinterpret_cast<string*>(dest)->assign(str, n);
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  reinterpret_cast<StringPiece*>(dest)->set(str, n);
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_char(const char* str, int n, void* dest) {
Packit 78a954
  if (n != 1) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<char*>(dest)) = str[0];
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_uchar(const char* str, int n, void* dest) {
Packit 78a954
  if (n != 1) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<unsigned char*>(dest)) = str[0];
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
// Largest number spec that we are willing to parse
Packit 78a954
static const int kMaxNumberLength = 32;
Packit 78a954
Packit 78a954
// REQUIRES "buf" must have length at least kMaxNumberLength+1
Packit 78a954
// REQUIRES "n > 0"
Packit 78a954
// Copies "str" into "buf" and null-terminates if necessary.
Packit 78a954
// Returns one of:
Packit 78a954
//      a. "str" if no termination is needed
Packit 78a954
//      b. "buf" if the string was copied and null-terminated
Packit 78a954
//      c. "" if the input was invalid and has no hope of being parsed
Packit 78a954
static const char* TerminateNumber(char* buf, const char* str, int n) {
Packit 78a954
  if ((n > 0) && isspace(*str)) {
Packit 78a954
    // We are less forgiving than the strtoxxx() routines and do not
Packit 78a954
    // allow leading spaces.
Packit 78a954
    return "";
Packit 78a954
  }
Packit 78a954
Packit 78a954
  // See if the character right after the input text may potentially
Packit 78a954
  // look like a digit.
Packit 78a954
  if (isdigit(str[n]) ||
Packit 78a954
      ((str[n] >= 'a') && (str[n] <= 'f')) ||
Packit 78a954
      ((str[n] >= 'A') && (str[n] <= 'F'))) {
Packit 78a954
    if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
Packit 78a954
    memcpy(buf, str, n);
Packit 78a954
    buf[n] = '\0';
Packit 78a954
    return buf;
Packit 78a954
  } else {
Packit 78a954
    // We can parse right out of the supplied string, so return it.
Packit 78a954
    return str;
Packit 78a954
  }
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_long_radix(const char* str,
Packit 78a954
                           int n,
Packit 78a954
                           void* dest,
Packit 78a954
                           int radix) {
Packit 78a954
  if (n == 0) return false;
Packit 78a954
  char buf[kMaxNumberLength+1];
Packit 78a954
  str = TerminateNumber(buf, str, n);
Packit 78a954
  char* end;
Packit 78a954
  errno = 0;
Packit 78a954
  long r = strtol(str, &end, radix);
Packit 78a954
  if (end != str + n) return false;   // Leftover junk
Packit 78a954
  if (errno) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<long*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_ulong_radix(const char* str,
Packit 78a954
                            int n,
Packit 78a954
                            void* dest,
Packit 78a954
                            int radix) {
Packit 78a954
  if (n == 0) return false;
Packit 78a954
  char buf[kMaxNumberLength+1];
Packit 78a954
  str = TerminateNumber(buf, str, n);
Packit 78a954
  if (str[0] == '-') return false;    // strtoul() on a negative number?!
Packit 78a954
  char* end;
Packit 78a954
  errno = 0;
Packit 78a954
  unsigned long r = strtoul(str, &end, radix);
Packit 78a954
  if (end != str + n) return false;   // Leftover junk
Packit 78a954
  if (errno) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<unsigned long*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_short_radix(const char* str,
Packit 78a954
                            int n,
Packit 78a954
                            void* dest,
Packit 78a954
                            int radix) {
Packit 78a954
  long r;
Packit 78a954
  if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
Packit 78a954
  if (r < SHRT_MIN || r > SHRT_MAX) return false;       // Out of range
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<short*>(dest)) = static_cast<short>(r);
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_ushort_radix(const char* str,
Packit 78a954
                             int n,
Packit 78a954
                             void* dest,
Packit 78a954
                             int radix) {
Packit 78a954
  unsigned long r;
Packit 78a954
  if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
Packit 78a954
  if (r > USHRT_MAX) return false;                      // Out of range
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<unsigned short*>(dest)) = static_cast<unsigned short>(r);
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_int_radix(const char* str,
Packit 78a954
                          int n,
Packit 78a954
                          void* dest,
Packit 78a954
                          int radix) {
Packit 78a954
  long r;
Packit 78a954
  if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
Packit 78a954
  if (r < INT_MIN || r > INT_MAX) return false;         // Out of range
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<int*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_uint_radix(const char* str,
Packit 78a954
                           int n,
Packit 78a954
                           void* dest,
Packit 78a954
                           int radix) {
Packit 78a954
  unsigned long r;
Packit 78a954
  if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
Packit 78a954
  if (r > UINT_MAX) return false;                       // Out of range
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<unsigned int*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_longlong_radix(const char* str,
Packit 78a954
                               int n,
Packit 78a954
                               void* dest,
Packit 78a954
                               int radix) {
Packit 78a954
#ifndef HAVE_LONG_LONG
Packit 78a954
  return false;
Packit 78a954
#else
Packit 78a954
  if (n == 0) return false;
Packit 78a954
  char buf[kMaxNumberLength+1];
Packit 78a954
  str = TerminateNumber(buf, str, n);
Packit 78a954
  char* end;
Packit 78a954
  errno = 0;
Packit 78a954
#if defined HAVE_STRTOQ
Packit 78a954
  long long r = strtoq(str, &end, radix);
Packit 78a954
#elif defined HAVE_STRTOLL
Packit 78a954
  long long r = strtoll(str, &end, radix);
Packit 78a954
#elif defined HAVE__STRTOI64
Packit 78a954
  long long r = _strtoi64(str, &end, radix);
Packit 78a954
#elif defined HAVE_STRTOIMAX
Packit 78a954
  long long r = strtoimax(str, &end, radix);
Packit 78a954
#else
Packit 78a954
#error parse_longlong_radix: cannot convert input to a long-long
Packit 78a954
#endif
Packit 78a954
  if (end != str + n) return false;   // Leftover junk
Packit 78a954
  if (errno) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<long long*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
#endif   /* HAVE_LONG_LONG */
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_ulonglong_radix(const char* str,
Packit 78a954
                                int n,
Packit 78a954
                                void* dest,
Packit 78a954
                                int radix) {
Packit 78a954
#ifndef HAVE_UNSIGNED_LONG_LONG
Packit 78a954
  return false;
Packit 78a954
#else
Packit 78a954
  if (n == 0) return false;
Packit 78a954
  char buf[kMaxNumberLength+1];
Packit 78a954
  str = TerminateNumber(buf, str, n);
Packit 78a954
  if (str[0] == '-') return false;    // strtoull() on a negative number?!
Packit 78a954
  char* end;
Packit 78a954
  errno = 0;
Packit 78a954
#if defined HAVE_STRTOQ
Packit 78a954
  unsigned long long r = strtouq(str, &end, radix);
Packit 78a954
#elif defined HAVE_STRTOLL
Packit 78a954
  unsigned long long r = strtoull(str, &end, radix);
Packit 78a954
#elif defined HAVE__STRTOI64
Packit 78a954
  unsigned long long r = _strtoui64(str, &end, radix);
Packit 78a954
#elif defined HAVE_STRTOIMAX
Packit 78a954
  unsigned long long r = strtoumax(str, &end, radix);
Packit 78a954
#else
Packit 78a954
#error parse_ulonglong_radix: cannot convert input to a long-long
Packit 78a954
#endif
Packit 78a954
  if (end != str + n) return false;   // Leftover junk
Packit 78a954
  if (errno) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<unsigned long long*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
#endif   /* HAVE_UNSIGNED_LONG_LONG */
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_double(const char* str, int n, void* dest) {
Packit 78a954
  if (n == 0) return false;
Packit 78a954
  static const int kMaxLength = 200;
Packit 78a954
  char buf[kMaxLength];
Packit 78a954
  if (n >= kMaxLength) return false;
Packit 78a954
  memcpy(buf, str, n);
Packit 78a954
  buf[n] = '\0';
Packit 78a954
  errno = 0;
Packit 78a954
  char* end;
Packit 78a954
  double r = strtod(buf, &end;;
Packit 78a954
  if (end != buf + n) return false;   // Leftover junk
Packit 78a954
  if (errno) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<double*>(dest)) = r;
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
bool Arg::parse_float(const char* str, int n, void* dest) {
Packit 78a954
  double r;
Packit 78a954
  if (!parse_double(str, n, &r)) return false;
Packit 78a954
  if (dest == NULL) return true;
Packit 78a954
  *(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
Packit 78a954
  return true;
Packit 78a954
}
Packit 78a954
Packit 78a954
Packit 78a954
#define DEFINE_INTEGER_PARSERS(name)                                    \
Packit 78a954
  bool Arg::parse_##name(const char* str, int n, void* dest) {          \
Packit 78a954
    return parse_##name##_radix(str, n, dest, 10);                      \
Packit 78a954
  }                                                                     \
Packit 78a954
  bool Arg::parse_##name##_hex(const char* str, int n, void* dest) {    \
Packit 78a954
    return parse_##name##_radix(str, n, dest, 16);                      \
Packit 78a954
  }                                                                     \
Packit 78a954
  bool Arg::parse_##name##_octal(const char* str, int n, void* dest) {  \
Packit 78a954
    return parse_##name##_radix(str, n, dest, 8);                       \
Packit 78a954
  }                                                                     \
Packit 78a954
  bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
Packit 78a954
    return parse_##name##_radix(str, n, dest, 0);                       \
Packit 78a954
  }
Packit 78a954
Packit 78a954
DEFINE_INTEGER_PARSERS(short)      /*                                   */
Packit 78a954
DEFINE_INTEGER_PARSERS(ushort)     /*                                   */
Packit 78a954
DEFINE_INTEGER_PARSERS(int)        /* Don't use semicolons after these  */
Packit 78a954
DEFINE_INTEGER_PARSERS(uint)       /* statements because they can cause */
Packit 78a954
DEFINE_INTEGER_PARSERS(long)       /* compiler warnings if the checking */
Packit 78a954
DEFINE_INTEGER_PARSERS(ulong)      /* level is turned up high enough.   */
Packit 78a954
DEFINE_INTEGER_PARSERS(longlong)   /*                                   */
Packit 78a954
DEFINE_INTEGER_PARSERS(ulonglong)  /*                                   */
Packit 78a954
Packit 78a954
#undef DEFINE_INTEGER_PARSERS
Packit 78a954
Packit 78a954
}   // namespace pcrecpp