1 // Copyright (c) 2010, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: Sanjay Ghemawat
39 #include <limits.h> /* for SHRT_MIN, USHRT_MAX, etc */
45 #include "pcrecpp_internal.h"
48 #include "pcre_stringpiece.h"
53 // Maximum number of args we can set
54 static const int kMaxArgs = 16;
55 static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace
57 // Special object that stands-in for no argument
58 Arg RE::no_arg((void*)NULL);
60 // This is for ABI compatibility with old versions of pcre (pre-7.6),
61 // which defined a global no_arg variable instead of putting it in the
62 // RE class. This works on GCC >= 3, at least. It definitely works
63 // for ELF, but may not for other object formats (Mach-O, for
64 // instance, does not support aliases.) We could probably have a more
65 // inclusive test if we ever needed it. (Note that not only the
66 // __attribute__ syntax, but also __USER_LABEL_PREFIX__, are
68 #if defined(__GNUC__) && __GNUC__ >= 3 && defined(__ELF__)
69 # define ULP_AS_STRING(x) ULP_AS_STRING_INTERNAL(x)
70 # define ULP_AS_STRING_INTERNAL(x) #x
71 # define USER_LABEL_PREFIX_STR ULP_AS_STRING(__USER_LABEL_PREFIX__)
73 __attribute__((alias(USER_LABEL_PREFIX_STR "_ZN7pcrecpp2RE6no_argE")));
76 // If a regular expression has no error, its error_ field points here
77 static const string empty_string;
79 // If the user doesn't ask for any options, we just use this one
80 static RE_Options default_options;
82 void RE::Init(const string& pat, const RE_Options* options) {
84 if (options == NULL) {
85 options_ = default_options;
89 error_ = &empty_string;
93 re_partial_ = Compile(UNANCHORED);
94 if (re_partial_ != NULL) {
95 re_full_ = Compile(ANCHOR_BOTH);
100 if (re_full_ != NULL) (*pcre_free)(re_full_);
101 if (re_partial_ != NULL) (*pcre_free)(re_partial_);
102 if (error_ != &empty_string) delete error_;
111 pcre* RE::Compile(Anchor anchor) {
112 // First, convert RE_Options into pcre options
113 int pcre_options = 0;
114 pcre_options = options_.all_options();
116 // Special treatment for anchoring. This is needed because at
117 // runtime pcre only provides an option for anchoring at the
118 // beginning of a string (unless you use offset).
120 // There are three types of anchoring we want:
121 // UNANCHORED Compile the original pattern, and use
122 // a pcre unanchored match.
123 // ANCHOR_START Compile the original pattern, and use
124 // a pcre anchored match.
125 // ANCHOR_BOTH Tack a "\z" to the end of the original pattern
126 // and use a pcre anchored match.
128 const char* compile_error;
131 if (anchor != ANCHOR_BOTH) {
132 re = pcre_compile(pattern_.c_str(), pcre_options,
133 &compile_error, &eoffset, NULL);
135 // Tack a '\z' at the end of RE. Parenthesize it first so that
136 // the '\z' applies to all top-level alternatives in the regexp.
137 string wrapped = "(?:"; // A non-counting grouping operator
140 re = pcre_compile(wrapped.c_str(), pcre_options,
141 &compile_error, &eoffset, NULL);
144 if (error_ == &empty_string) error_ = new string(compile_error);
149 /***** Matching interfaces *****/
151 bool RE::FullMatch(const StringPiece& text,
167 const Arg& ptr16) const {
168 const Arg* args[kMaxArgs];
170 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
171 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
172 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
173 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
174 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
175 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
176 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
177 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
178 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
179 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
180 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
181 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
182 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
183 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
184 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
185 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
190 return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
193 bool RE::PartialMatch(const StringPiece& text,
209 const Arg& ptr16) const {
210 const Arg* args[kMaxArgs];
212 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
213 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
214 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
215 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
216 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
217 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
218 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
219 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
220 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
221 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
222 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
223 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
224 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
225 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
226 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
227 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
232 return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
235 bool RE::Consume(StringPiece* input,
251 const Arg& ptr16) const {
252 const Arg* args[kMaxArgs];
254 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
255 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
256 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
257 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
258 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
259 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
260 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
261 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
262 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
263 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
264 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
265 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
266 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
267 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
268 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
269 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
274 if (DoMatchImpl(*input, ANCHOR_START, &consumed,
275 args, n, vec, kVecSize)) {
276 input->remove_prefix(consumed);
283 bool RE::FindAndConsume(StringPiece* input,
299 const Arg& ptr16) const {
300 const Arg* args[kMaxArgs];
302 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
303 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
304 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
305 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
306 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
307 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
308 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
309 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
310 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
311 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
312 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
313 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
314 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
315 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
316 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
317 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
322 if (DoMatchImpl(*input, UNANCHORED, &consumed,
323 args, n, vec, kVecSize)) {
324 input->remove_prefix(consumed);
331 bool RE::Replace(const StringPiece& rewrite,
334 int matches = TryMatch(*str, 0, UNANCHORED, true, vec, kVecSize);
339 if (!Rewrite(&s, rewrite, *str, vec, matches))
344 str->replace(vec[0], vec[1] - vec[0], s);
348 // Returns PCRE_NEWLINE_CRLF, PCRE_NEWLINE_CR, or PCRE_NEWLINE_LF.
349 // Note that PCRE_NEWLINE_CRLF is defined to be P_N_CR | P_N_LF.
350 // Modified by PH to add PCRE_NEWLINE_ANY and PCRE_NEWLINE_ANYCRLF.
352 static int NewlineMode(int pcre_options) {
353 // TODO: if we can make it threadsafe, cache this var
354 int newline_mode = 0;
355 /* if (newline_mode) return newline_mode; */ // do this once it's cached
356 if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
357 PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)) {
358 newline_mode = (pcre_options &
359 (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
360 PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF));
363 pcre_config(PCRE_CONFIG_NEWLINE, &newline);
365 newline_mode = PCRE_NEWLINE_LF;
366 else if (newline == 13)
367 newline_mode = PCRE_NEWLINE_CR;
368 else if (newline == 3338)
369 newline_mode = PCRE_NEWLINE_CRLF;
370 else if (newline == -1)
371 newline_mode = PCRE_NEWLINE_ANY;
372 else if (newline == -2)
373 newline_mode = PCRE_NEWLINE_ANYCRLF;
375 assert(NULL == "Unexpected return value from pcre_config(NEWLINE)");
380 int RE::GlobalReplace(const StringPiece& rewrite,
387 bool last_match_was_empty_string = false;
389 while (start <= static_cast<int>(str->length())) {
390 // If the previous match was for the empty string, we shouldn't
391 // just match again: we'll match in the same way and get an
392 // infinite loop. Instead, we do the match in a special way:
393 // anchored -- to force another try at the same position --
394 // and with a flag saying that this time, ignore empty matches.
395 // If this special match returns, that means there's a non-empty
396 // match at this position as well, and we can continue. If not,
397 // we do what perl does, and just advance by one.
398 // Notice that perl prints '@@@' for this;
399 // perl -le '$_ = "aa"; s/b*|aa/@/g; print'
401 if (last_match_was_empty_string) {
402 matches = TryMatch(*str, start, ANCHOR_START, false, vec, kVecSize);
404 int matchend = start + 1; // advance one character.
405 // If the current char is CR and we're in CRLF mode, skip LF too.
406 // Note it's better to call pcre_fullinfo() than to examine
407 // all_options(), since options_ could have changed bewteen
408 // compile-time and now, but this is simpler and safe enough.
409 // Modified by PH to add ANY and ANYCRLF.
410 if (matchend < static_cast<int>(str->length()) &&
411 (*str)[start] == '\r' && (*str)[matchend] == '\n' &&
412 (NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF ||
413 NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANY ||
414 NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANYCRLF)) {
417 // We also need to advance more than one char if we're in utf8 mode.
419 if (options_.utf8()) {
420 while (matchend < static_cast<int>(str->length()) &&
421 ((*str)[matchend] & 0xc0) == 0x80)
425 if (start < static_cast<int>(str->length()))
426 out.append(*str, start, matchend - start);
428 last_match_was_empty_string = false;
432 matches = TryMatch(*str, start, UNANCHORED, true, vec, kVecSize);
436 int matchstart = vec[0], matchend = vec[1];
437 assert(matchstart >= start);
438 assert(matchend >= matchstart);
439 out.append(*str, start, matchstart - start);
440 Rewrite(&out, rewrite, *str, vec, matches);
444 last_match_was_empty_string = (matchstart == matchend);
450 if (start < static_cast<int>(str->length()))
451 out.append(*str, start, str->length() - start);
456 bool RE::Extract(const StringPiece& rewrite,
457 const StringPiece& text,
460 int matches = TryMatch(text, 0, UNANCHORED, true, vec, kVecSize);
464 return Rewrite(out, rewrite, text, vec, matches);
467 /*static*/ string RE::QuoteMeta(const StringPiece& unquoted) {
470 // Escape any ascii character not in [A-Za-z_0-9].
472 // Note that it's legal to escape a character even if it has no
473 // special meaning in a regular expression -- so this function does
474 // that. (This also makes it identical to the perl function of the
475 // same name; see `perldoc -f quotemeta`.) The one exception is
476 // escaping NUL: rather than doing backslash + NUL, like perl does,
477 // we do '\0', because pcre itself doesn't take embedded NUL chars.
478 for (int ii = 0; ii < unquoted.size(); ++ii) {
479 // Note that using 'isalnum' here raises the benchmark time from
481 if (unquoted[ii] == '\0') {
483 } else if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
484 (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
485 (unquoted[ii] < '0' || unquoted[ii] > '9') &&
486 unquoted[ii] != '_' &&
487 // If this is the part of a UTF8 or Latin1 character, we need
488 // to copy this byte without escaping. Experimentally this is
489 // what works correctly with the regexp library.
490 !(unquoted[ii] & 128)) {
492 result += unquoted[ii];
494 result += unquoted[ii];
501 /***** Actual matching and rewriting code *****/
503 int RE::TryMatch(const StringPiece& text,
509 pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
511 //fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
515 pcre_extra extra = { 0, 0, 0, 0, 0, 0 };
516 if (options_.match_limit() > 0) {
517 extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
518 extra.match_limit = options_.match_limit();
520 if (options_.match_limit_recursion() > 0) {
521 extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
522 extra.match_limit_recursion = options_.match_limit_recursion();
526 if (anchor != UNANCHORED)
527 options |= PCRE_ANCHORED;
529 options |= PCRE_NOTEMPTY;
531 int rc = pcre_exec(re, // The regular expression object
533 (text.data() == NULL) ? "" : text.data(),
541 if (rc == PCRE_ERROR_NOMATCH) {
544 //fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
545 // re, pattern_.c_str());
547 } else if (rc == 0) {
548 // pcre_exec() returns 0 as a special case when the number of
549 // capturing subpatterns exceeds the size of the vector.
550 // When this happens, there is a match and the output vector
551 // is filled, but we miss out on the positions of the extra subpatterns.
558 bool RE::DoMatchImpl(const StringPiece& text,
561 const Arg* const* args,
565 assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
566 int matches = TryMatch(text, 0, anchor, true, vec, vecsize);
567 assert(matches >= 0); // TryMatch never returns negatives
573 if (n == 0 || args == NULL) {
574 // We are not interested in results
578 if (NumberOfCapturingGroups() < n) {
579 // RE has fewer capturing groups than number of arg pointers passed in
583 // If we got here, we must have matched the whole pattern.
584 // We do not need (can not do) any more checks on the value of 'matches' here
585 // -- see the comment for TryMatch.
586 for (int i = 0; i < n; i++) {
587 const int start = vec[2*(i+1)];
588 const int limit = vec[2*(i+1)+1];
589 if (!args[i]->Parse(text.data() + start, limit-start)) {
590 // TODO: Should we indicate what the error was?
598 bool RE::DoMatch(const StringPiece& text,
601 const Arg* const args[],
604 size_t const vecsize = (1 + n) * 3; // results + PCRE workspace
606 int space[21]; // use stack allocation for small vecsize (common case)
607 int* vec = vecsize <= 21 ? space : new int[vecsize];
608 bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, (int)vecsize);
609 if (vec != space) delete [] vec;
613 bool RE::Rewrite(string *out, const StringPiece &rewrite,
614 const StringPiece &text, int *vec, int veclen) const {
615 for (const char *s = rewrite.data(), *end = s + rewrite.size();
623 //fprintf(stderr, requested group %d in regexp %.*s\n",
624 // n, rewrite.size(), rewrite.data());
627 int start = vec[2 * n];
629 out->append(text.data() + start, vec[2 * n + 1] - start);
630 } else if (c == '\\') {
633 //fprintf(stderr, "invalid rewrite pattern: %.*s\n",
634 // rewrite.size(), rewrite.data());
644 // Return the number of capturing subpatterns, or -1 if the
645 // regexp wasn't valid on construction.
646 int RE::NumberOfCapturingGroups() const {
647 if (re_partial_ == NULL) return -1;
650 int pcre_retval = pcre_fullinfo(re_partial_, // The regular expression object
651 NULL, // We did not study the pattern
652 PCRE_INFO_CAPTURECOUNT,
654 assert(pcre_retval == 0);
658 /***** Parsers for various types *****/
660 bool Arg::parse_null(const char* str, int n, void* dest) {
661 // We fail if somebody asked us to store into a non-NULL void* pointer
662 return (dest == NULL);
665 bool Arg::parse_string(const char* str, int n, void* dest) {
666 if (dest == NULL) return true;
667 reinterpret_cast<string*>(dest)->assign(str, n);
671 bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
672 if (dest == NULL) return true;
673 reinterpret_cast<StringPiece*>(dest)->set(str, n);
677 bool Arg::parse_char(const char* str, int n, void* dest) {
678 if (n != 1) return false;
679 if (dest == NULL) return true;
680 *(reinterpret_cast<char*>(dest)) = str[0];
684 bool Arg::parse_uchar(const char* str, int n, void* dest) {
685 if (n != 1) return false;
686 if (dest == NULL) return true;
687 *(reinterpret_cast<unsigned char*>(dest)) = str[0];
691 // Largest number spec that we are willing to parse
692 static const int kMaxNumberLength = 32;
694 // REQUIRES "buf" must have length at least kMaxNumberLength+1
696 // Copies "str" into "buf" and null-terminates if necessary.
698 // a. "str" if no termination is needed
699 // b. "buf" if the string was copied and null-terminated
700 // c. "" if the input was invalid and has no hope of being parsed
701 static const char* TerminateNumber(char* buf, const char* str, int n) {
702 if ((n > 0) && isspace(*str)) {
703 // We are less forgiving than the strtoxxx() routines and do not
704 // allow leading spaces.
708 // See if the character right after the input text may potentially
709 // look like a digit.
710 if (isdigit(str[n]) ||
711 ((str[n] >= 'a') && (str[n] <= 'f')) ||
712 ((str[n] >= 'A') && (str[n] <= 'F'))) {
713 if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
718 // We can parse right out of the supplied string, so return it.
723 bool Arg::parse_long_radix(const char* str,
727 if (n == 0) return false;
728 char buf[kMaxNumberLength+1];
729 str = TerminateNumber(buf, str, n);
732 long r = strtol(str, &end, radix);
733 if (end != str + n) return false; // Leftover junk
734 if (errno) return false;
735 if (dest == NULL) return true;
736 *(reinterpret_cast<long*>(dest)) = r;
740 bool Arg::parse_ulong_radix(const char* str,
744 if (n == 0) return false;
745 char buf[kMaxNumberLength+1];
746 str = TerminateNumber(buf, str, n);
747 if (str[0] == '-') return false; // strtoul() on a negative number?!
750 unsigned long r = strtoul(str, &end, radix);
751 if (end != str + n) return false; // Leftover junk
752 if (errno) return false;
753 if (dest == NULL) return true;
754 *(reinterpret_cast<unsigned long*>(dest)) = r;
758 bool Arg::parse_short_radix(const char* str,
763 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
764 if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range
765 if (dest == NULL) return true;
766 *(reinterpret_cast<short*>(dest)) = static_cast<short>(r);
770 bool Arg::parse_ushort_radix(const char* str,
775 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
776 if (r > USHRT_MAX) return false; // Out of range
777 if (dest == NULL) return true;
778 *(reinterpret_cast<unsigned short*>(dest)) = static_cast<unsigned short>(r);
782 bool Arg::parse_int_radix(const char* str,
787 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
788 if (r < INT_MIN || r > INT_MAX) return false; // Out of range
789 if (dest == NULL) return true;
790 *(reinterpret_cast<int*>(dest)) = r;
794 bool Arg::parse_uint_radix(const char* str,
799 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
800 if (r > UINT_MAX) return false; // Out of range
801 if (dest == NULL) return true;
802 *(reinterpret_cast<unsigned int*>(dest)) = r;
806 bool Arg::parse_longlong_radix(const char* str,
810 #ifndef HAVE_LONG_LONG
813 if (n == 0) return false;
814 char buf[kMaxNumberLength+1];
815 str = TerminateNumber(buf, str, n);
818 #if defined HAVE_STRTOQ
819 long long r = strtoq(str, &end, radix);
820 #elif defined HAVE_STRTOLL
821 long long r = strtoll(str, &end, radix);
822 #elif defined HAVE__STRTOI64
823 long long r = _strtoi64(str, &end, radix);
824 #elif defined HAVE_STRTOIMAX
825 long long r = strtoimax(str, &end, radix);
827 #error parse_longlong_radix: cannot convert input to a long-long
829 if (end != str + n) return false; // Leftover junk
830 if (errno) return false;
831 if (dest == NULL) return true;
832 *(reinterpret_cast<long long*>(dest)) = r;
834 #endif /* HAVE_LONG_LONG */
837 bool Arg::parse_ulonglong_radix(const char* str,
841 #ifndef HAVE_UNSIGNED_LONG_LONG
844 if (n == 0) return false;
845 char buf[kMaxNumberLength+1];
846 str = TerminateNumber(buf, str, n);
847 if (str[0] == '-') return false; // strtoull() on a negative number?!
850 #if defined HAVE_STRTOQ
851 unsigned long long r = strtouq(str, &end, radix);
852 #elif defined HAVE_STRTOLL
853 unsigned long long r = strtoull(str, &end, radix);
854 #elif defined HAVE__STRTOI64
855 unsigned long long r = _strtoui64(str, &end, radix);
856 #elif defined HAVE_STRTOIMAX
857 unsigned long long r = strtoumax(str, &end, radix);
859 #error parse_ulonglong_radix: cannot convert input to a long-long
861 if (end != str + n) return false; // Leftover junk
862 if (errno) return false;
863 if (dest == NULL) return true;
864 *(reinterpret_cast<unsigned long long*>(dest)) = r;
866 #endif /* HAVE_UNSIGNED_LONG_LONG */
869 bool Arg::parse_double(const char* str, int n, void* dest) {
870 if (n == 0) return false;
871 static const int kMaxLength = 200;
872 char buf[kMaxLength];
873 if (n >= kMaxLength) return false;
878 double r = strtod(buf, &end);
879 if (end != buf + n) return false; // Leftover junk
880 if (errno) return false;
881 if (dest == NULL) return true;
882 *(reinterpret_cast<double*>(dest)) = r;
886 bool Arg::parse_float(const char* str, int n, void* dest) {
888 if (!parse_double(str, n, &r)) return false;
889 if (dest == NULL) return true;
890 *(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
895 #define DEFINE_INTEGER_PARSERS(name) \
896 bool Arg::parse_##name(const char* str, int n, void* dest) { \
897 return parse_##name##_radix(str, n, dest, 10); \
899 bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
900 return parse_##name##_radix(str, n, dest, 16); \
902 bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
903 return parse_##name##_radix(str, n, dest, 8); \
905 bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
906 return parse_##name##_radix(str, n, dest, 0); \
909 DEFINE_INTEGER_PARSERS(short) /* */
910 DEFINE_INTEGER_PARSERS(ushort) /* */
911 DEFINE_INTEGER_PARSERS(int) /* Don't use semicolons after these */
912 DEFINE_INTEGER_PARSERS(uint) /* statements because they can cause */
913 DEFINE_INTEGER_PARSERS(long) /* compiler warnings if the checking */
914 DEFINE_INTEGER_PARSERS(ulong) /* level is turned up high enough. */
915 DEFINE_INTEGER_PARSERS(longlong) /* */
916 DEFINE_INTEGER_PARSERS(ulonglong) /* */
918 #undef DEFINE_INTEGER_PARSERS
920 } // namespace pcrecpp