1
// Copyright (c) 2005, 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
PCRECPP_EXP_DEFN Arg no_arg((void*)NULL);
60
// If a regular expression has no error, its error_ field points here
61
static const string empty_string;
63
// If the user doesn't ask for any options, we just use this one
64
static RE_Options default_options;
66
void RE::Init(const string& pat, const RE_Options* options) {
68
if (options == NULL) {
69
options_ = default_options;
73
error_ = &empty_string;
77
re_partial_ = Compile(UNANCHORED);
78
if (re_partial_ != NULL) {
79
re_full_ = Compile(ANCHOR_BOTH);
84
if (re_full_ != NULL) (*pcre_free)(re_full_);
85
if (re_partial_ != NULL) (*pcre_free)(re_partial_);
86
if (error_ != &empty_string) delete error_;
95
pcre* RE::Compile(Anchor anchor) {
96
// First, convert RE_Options into pcre options
98
pcre_options = options_.all_options();
100
// Special treatment for anchoring. This is needed because at
101
// runtime pcre only provides an option for anchoring at the
102
// beginning of a string (unless you use offset).
104
// There are three types of anchoring we want:
105
// UNANCHORED Compile the original pattern, and use
106
// a pcre unanchored match.
107
// ANCHOR_START Compile the original pattern, and use
108
// a pcre anchored match.
109
// ANCHOR_BOTH Tack a "\z" to the end of the original pattern
110
// and use a pcre anchored match.
112
const char* compile_error;
115
if (anchor != ANCHOR_BOTH) {
116
re = pcre_compile(pattern_.c_str(), pcre_options,
117
&compile_error, &eoffset, NULL);
119
// Tack a '\z' at the end of RE. Parenthesize it first so that
120
// the '\z' applies to all top-level alternatives in the regexp.
121
string wrapped = "(?:"; // A non-counting grouping operator
124
re = pcre_compile(wrapped.c_str(), pcre_options,
125
&compile_error, &eoffset, NULL);
128
if (error_ == &empty_string) error_ = new string(compile_error);
133
/***** Matching interfaces *****/
135
bool RE::FullMatch(const StringPiece& text,
151
const Arg& ptr16) const {
152
const Arg* args[kMaxArgs];
154
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
155
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
156
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
157
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
158
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
159
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
160
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
161
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
162
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
163
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
164
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
165
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
166
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
167
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
168
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
169
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
174
return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
177
bool RE::PartialMatch(const StringPiece& text,
193
const Arg& ptr16) const {
194
const Arg* args[kMaxArgs];
196
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
197
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
198
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
199
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
200
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
201
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
202
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
203
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
204
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
205
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
206
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
207
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
208
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
209
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
210
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
211
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
216
return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
219
bool RE::Consume(StringPiece* input,
235
const Arg& ptr16) const {
236
const Arg* args[kMaxArgs];
238
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
239
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
240
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
241
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
242
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
243
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
244
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
245
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
246
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
247
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
248
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
249
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
250
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
251
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
252
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
253
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
258
if (DoMatchImpl(*input, ANCHOR_START, &consumed,
259
args, n, vec, kVecSize)) {
260
input->remove_prefix(consumed);
267
bool RE::FindAndConsume(StringPiece* input,
283
const Arg& ptr16) const {
284
const Arg* args[kMaxArgs];
286
if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
287
if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
288
if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
289
if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
290
if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
291
if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
292
if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
293
if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
294
if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
295
if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
296
if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
297
if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
298
if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
299
if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
300
if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
301
if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
306
if (DoMatchImpl(*input, UNANCHORED, &consumed,
307
args, n, vec, kVecSize)) {
308
input->remove_prefix(consumed);
315
bool RE::Replace(const StringPiece& rewrite,
318
int matches = TryMatch(*str, 0, UNANCHORED, vec, kVecSize);
323
if (!Rewrite(&s, rewrite, *str, vec, matches))
328
str->replace(vec[0], vec[1] - vec[0], s);
332
// Returns PCRE_NEWLINE_CRLF, PCRE_NEWLINE_CR, or PCRE_NEWLINE_LF.
333
// Note that PCRE_NEWLINE_CRLF is defined to be P_N_CR | P_N_LF.
334
// Modified by PH to add PCRE_NEWLINE_ANY and PCRE_NEWLINE_ANYCRLF.
336
static int NewlineMode(int pcre_options) {
337
// TODO: if we can make it threadsafe, cache this var
338
int newline_mode = 0;
339
/* if (newline_mode) return newline_mode; */ // do this once it's cached
340
if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
341
PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)) {
342
newline_mode = (pcre_options &
343
(PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
344
PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF));
347
pcre_config(PCRE_CONFIG_NEWLINE, &newline);
349
newline_mode = PCRE_NEWLINE_LF;
350
else if (newline == 13)
351
newline_mode = PCRE_NEWLINE_CR;
352
else if (newline == 3338)
353
newline_mode = PCRE_NEWLINE_CRLF;
354
else if (newline == -1)
355
newline_mode = PCRE_NEWLINE_ANY;
356
else if (newline == -2)
357
newline_mode = PCRE_NEWLINE_ANYCRLF;
359
assert("" == "Unexpected return value from pcre_config(NEWLINE)");
364
int RE::GlobalReplace(const StringPiece& rewrite,
372
for (; start <= static_cast<int>(str->length()); count++) {
373
int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize);
376
int matchstart = vec[0], matchend = vec[1];
377
assert(matchstart >= start);
378
assert(matchend >= matchstart);
379
if (matchstart == matchend && matchstart == lastend) {
380
// advance one character if we matched an empty string at the same
381
// place as the last match occurred
382
matchend = start + 1;
383
// If the current char is CR and we're in CRLF mode, skip LF too.
384
// Note it's better to call pcre_fullinfo() than to examine
385
// all_options(), since options_ could have changed bewteen
386
// compile-time and now, but this is simpler and safe enough.
387
// Modified by PH to add ANY and ANYCRLF.
388
if (start+1 < static_cast<int>(str->length()) &&
389
(*str)[start] == '\r' && (*str)[start+1] == '\n' &&
390
(NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF ||
391
NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANY ||
392
NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANYCRLF)
396
// We also need to advance more than one char if we're in utf8 mode.
398
if (options_.utf8()) {
399
while (matchend < static_cast<int>(str->length()) &&
400
((*str)[matchend] & 0xc0) == 0x80)
404
if (matchend <= static_cast<int>(str->length()))
405
out.append(*str, start, matchend - start);
408
out.append(*str, start, matchstart - start);
409
Rewrite(&out, rewrite, *str, vec, matches);
419
if (start < static_cast<int>(str->length()))
420
out.append(*str, start, str->length() - start);
425
bool RE::Extract(const StringPiece& rewrite,
426
const StringPiece& text,
429
int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize);
433
return Rewrite(out, rewrite, text, vec, matches);
436
/*static*/ string RE::QuoteMeta(const StringPiece& unquoted) {
439
// Escape any ascii character not in [A-Za-z_0-9].
441
// Note that it's legal to escape a character even if it has no
442
// special meaning in a regular expression -- so this function does
443
// that. (This also makes it identical to the perl function of the
444
// same name; see `perldoc -f quotemeta`.)
445
for (int ii = 0; ii < unquoted.size(); ++ii) {
446
// Note that using 'isalnum' here raises the benchmark time from
448
if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
449
(unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
450
(unquoted[ii] < '0' || unquoted[ii] > '9') &&
451
unquoted[ii] != '_' &&
452
// If this is the part of a UTF8 or Latin1 character, we need
453
// to copy this byte without escaping. Experimentally this is
454
// what works correctly with the regexp library.
455
!(unquoted[ii] & 128)) {
458
result += unquoted[ii];
464
/***** Actual matching and rewriting code *****/
466
int RE::TryMatch(const StringPiece& text,
471
pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
473
//fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
477
pcre_extra extra = { 0, 0, 0, 0, 0, 0 };
478
if (options_.match_limit() > 0) {
479
extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
480
extra.match_limit = options_.match_limit();
482
if (options_.match_limit_recursion() > 0) {
483
extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
484
extra.match_limit_recursion = options_.match_limit_recursion();
486
int rc = pcre_exec(re, // The regular expression object
488
(text.data() == NULL) ? "" : text.data(),
491
(anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
496
if (rc == PCRE_ERROR_NOMATCH) {
499
//fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
500
// re, pattern_.c_str());
502
} else if (rc == 0) {
503
// pcre_exec() returns 0 as a special case when the number of
504
// capturing subpatterns exceeds the size of the vector.
505
// When this happens, there is a match and the output vector
506
// is filled, but we miss out on the positions of the extra subpatterns.
513
bool RE::DoMatchImpl(const StringPiece& text,
516
const Arg* const* args,
520
assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
521
int matches = TryMatch(text, 0, anchor, vec, vecsize);
522
assert(matches >= 0); // TryMatch never returns negatives
528
if (n == 0 || args == NULL) {
529
// We are not interested in results
533
if (NumberOfCapturingGroups() < n) {
534
// RE has fewer capturing groups than number of arg pointers passed in
538
// If we got here, we must have matched the whole pattern.
539
// We do not need (can not do) any more checks on the value of 'matches' here
540
// -- see the comment for TryMatch.
541
for (int i = 0; i < n; i++) {
542
const int start = vec[2*(i+1)];
543
const int limit = vec[2*(i+1)+1];
544
if (!args[i]->Parse(text.data() + start, limit-start)) {
545
// TODO: Should we indicate what the error was?
553
bool RE::DoMatch(const StringPiece& text,
556
const Arg* const args[],
559
size_t const vecsize = (1 + n) * 3; // results + PCRE workspace
561
int space[21]; // use stack allocation for small vecsize (common case)
562
int* vec = vecsize <= 21 ? space : new int[vecsize];
563
bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
564
if (vec != space) delete [] vec;
568
bool RE::Rewrite(string *out, const StringPiece &rewrite,
569
const StringPiece &text, int *vec, int veclen) const {
570
for (const char *s = rewrite.data(), *end = s + rewrite.size();
578
//fprintf(stderr, requested group %d in regexp %.*s\n",
579
// n, rewrite.size(), rewrite.data());
582
int start = vec[2 * n];
584
out->append(text.data() + start, vec[2 * n + 1] - start);
585
} else if (c == '\\') {
586
out->push_back('\\');
588
//fprintf(stderr, "invalid rewrite pattern: %.*s\n",
589
// rewrite.size(), rewrite.data());
599
// Return the number of capturing subpatterns, or -1 if the
600
// regexp wasn't valid on construction.
601
int RE::NumberOfCapturingGroups() const {
602
if (re_partial_ == NULL) return -1;
605
int pcre_retval = pcre_fullinfo(re_partial_, // The regular expression object
606
NULL, // We did not study the pattern
607
PCRE_INFO_CAPTURECOUNT,
609
assert(pcre_retval == 0);
613
/***** Parsers for various types *****/
615
bool Arg::parse_null(const char* str, int n, void* dest) {
616
// We fail if somebody asked us to store into a non-NULL void* pointer
617
return (dest == NULL);
620
bool Arg::parse_string(const char* str, int n, void* dest) {
621
reinterpret_cast<string*>(dest)->assign(str, n);
625
bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
626
reinterpret_cast<StringPiece*>(dest)->set(str, n);
630
bool Arg::parse_char(const char* str, int n, void* dest) {
631
if (n != 1) return false;
632
*(reinterpret_cast<char*>(dest)) = str[0];
636
bool Arg::parse_uchar(const char* str, int n, void* dest) {
637
if (n != 1) return false;
638
*(reinterpret_cast<unsigned char*>(dest)) = str[0];
642
// Largest number spec that we are willing to parse
643
static const int kMaxNumberLength = 32;
645
// REQUIRES "buf" must have length at least kMaxNumberLength+1
647
// Copies "str" into "buf" and null-terminates if necessary.
649
// a. "str" if no termination is needed
650
// b. "buf" if the string was copied and null-terminated
651
// c. "" if the input was invalid and has no hope of being parsed
652
static const char* TerminateNumber(char* buf, const char* str, int n) {
653
if ((n > 0) && isspace(*str)) {
654
// We are less forgiving than the strtoxxx() routines and do not
655
// allow leading spaces.
659
// See if the character right after the input text may potentially
660
// look like a digit.
661
if (isdigit(str[n]) ||
662
((str[n] >= 'a') && (str[n] <= 'f')) ||
663
((str[n] >= 'A') && (str[n] <= 'F'))) {
664
if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
669
// We can parse right out of the supplied string, so return it.
674
bool Arg::parse_long_radix(const char* str,
678
if (n == 0) return false;
679
char buf[kMaxNumberLength+1];
680
str = TerminateNumber(buf, str, n);
683
long r = strtol(str, &end, radix);
684
if (end != str + n) return false; // Leftover junk
685
if (errno) return false;
686
*(reinterpret_cast<long*>(dest)) = r;
690
bool Arg::parse_ulong_radix(const char* str,
694
if (n == 0) return false;
695
char buf[kMaxNumberLength+1];
696
str = TerminateNumber(buf, str, n);
697
if (str[0] == '-') return false; // strtoul() on a negative number?!
700
unsigned long r = strtoul(str, &end, radix);
701
if (end != str + n) return false; // Leftover junk
702
if (errno) return false;
703
*(reinterpret_cast<unsigned long*>(dest)) = r;
707
bool Arg::parse_short_radix(const char* str,
712
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
713
if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range
714
*(reinterpret_cast<short*>(dest)) = static_cast<short>(r);
718
bool Arg::parse_ushort_radix(const char* str,
723
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
724
if (r > USHRT_MAX) return false; // Out of range
725
*(reinterpret_cast<unsigned short*>(dest)) = static_cast<unsigned short>(r);
729
bool Arg::parse_int_radix(const char* str,
734
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
735
if (r < INT_MIN || r > INT_MAX) return false; // Out of range
736
*(reinterpret_cast<int*>(dest)) = r;
740
bool Arg::parse_uint_radix(const char* str,
745
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
746
if (r > UINT_MAX) return false; // Out of range
747
*(reinterpret_cast<unsigned int*>(dest)) = r;
751
bool Arg::parse_longlong_radix(const char* str,
755
#ifndef HAVE_LONG_LONG
758
if (n == 0) return false;
759
char buf[kMaxNumberLength+1];
760
str = TerminateNumber(buf, str, n);
763
#if defined HAVE_STRTOQ
764
long long r = strtoq(str, &end, radix);
765
#elif defined HAVE_STRTOLL
766
long long r = strtoll(str, &end, radix);
767
#elif defined HAVE__STRTOI64
768
long long r = _strtoi64(str, &end, radix);
770
#error parse_longlong_radix: cannot convert input to a long-long
772
if (end != str + n) return false; // Leftover junk
773
if (errno) return false;
774
*(reinterpret_cast<long long*>(dest)) = r;
776
#endif /* HAVE_LONG_LONG */
779
bool Arg::parse_ulonglong_radix(const char* str,
783
#ifndef HAVE_UNSIGNED_LONG_LONG
786
if (n == 0) return false;
787
char buf[kMaxNumberLength+1];
788
str = TerminateNumber(buf, str, n);
789
if (str[0] == '-') return false; // strtoull() on a negative number?!
792
#if defined HAVE_STRTOQ
793
unsigned long long r = strtouq(str, &end, radix);
794
#elif defined HAVE_STRTOLL
795
unsigned long long r = strtoull(str, &end, radix);
796
#elif defined HAVE__STRTOI64
797
unsigned long long r = _strtoui64(str, &end, radix);
799
#error parse_ulonglong_radix: cannot convert input to a long-long
801
if (end != str + n) return false; // Leftover junk
802
if (errno) return false;
803
*(reinterpret_cast<unsigned long long*>(dest)) = r;
805
#endif /* HAVE_UNSIGNED_LONG_LONG */
808
bool Arg::parse_double(const char* str, int n, void* dest) {
809
if (n == 0) return false;
810
static const int kMaxLength = 200;
811
char buf[kMaxLength];
812
if (n >= kMaxLength) return false;
817
double r = strtod(buf, &end);
818
if (end != buf + n) return false; // Leftover junk
819
if (errno) return false;
820
*(reinterpret_cast<double*>(dest)) = r;
824
bool Arg::parse_float(const char* str, int n, void* dest) {
826
if (!parse_double(str, n, &r)) return false;
827
*(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
832
#define DEFINE_INTEGER_PARSERS(name) \
833
bool Arg::parse_##name(const char* str, int n, void* dest) { \
834
return parse_##name##_radix(str, n, dest, 10); \
836
bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
837
return parse_##name##_radix(str, n, dest, 16); \
839
bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
840
return parse_##name##_radix(str, n, dest, 8); \
842
bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
843
return parse_##name##_radix(str, n, dest, 0); \
846
DEFINE_INTEGER_PARSERS(short) /* */
847
DEFINE_INTEGER_PARSERS(ushort) /* */
848
DEFINE_INTEGER_PARSERS(int) /* Don't use semicolons after these */
849
DEFINE_INTEGER_PARSERS(uint) /* statements because they can cause */
850
DEFINE_INTEGER_PARSERS(long) /* compiler warnings if the checking */
851
DEFINE_INTEGER_PARSERS(ulong) /* level is turned up high enough. */
852
DEFINE_INTEGER_PARSERS(longlong) /* */
853
DEFINE_INTEGER_PARSERS(ulonglong) /* */
855
#undef DEFINE_INTEGER_PARSERS
857
} // namespace pcrecpp