| 29 |
// |
// |
| 30 |
// Author: Sanjay Ghemawat |
// Author: Sanjay Ghemawat |
| 31 |
|
|
| 32 |
|
#ifdef HAVE_CONFIG_H |
| 33 |
|
#include "config.h" |
| 34 |
|
#endif |
| 35 |
|
|
| 36 |
#include <stdlib.h> |
#include <stdlib.h> |
| 37 |
#include <stdio.h> |
#include <stdio.h> |
| 38 |
#include <ctype.h> |
#include <ctype.h> |
| 40 |
#include <assert.h> |
#include <assert.h> |
| 41 |
#include <errno.h> |
#include <errno.h> |
| 42 |
#include <string> |
#include <string> |
| 43 |
#include "config.h" |
#include <algorithm> |
| 44 |
// We need this to compile the proper dll on windows/msys. This is copied |
|
| 45 |
// from pcre_internal.h. It would probably be better just to include that. |
#include "pcrecpp_internal.h" |
|
#define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */ |
|
| 46 |
#include "pcre.h" |
#include "pcre.h" |
|
#include "pcre_stringpiece.h" |
|
| 47 |
#include "pcrecpp.h" |
#include "pcrecpp.h" |
| 48 |
|
#include "pcre_stringpiece.h" |
| 49 |
|
|
| 50 |
|
|
| 51 |
namespace pcrecpp { |
namespace pcrecpp { |
| 55 |
static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace |
static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace |
| 56 |
|
|
| 57 |
// Special object that stands-in for no argument |
// Special object that stands-in for no argument |
| 58 |
Arg no_arg((void*)NULL); |
Arg RE::no_arg((void*)NULL); |
| 59 |
|
|
| 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. We could probably |
| 63 |
|
// have a more inclusive test if we ever needed it. (Note that not |
| 64 |
|
// only the __attribute__ syntax, but also __USER_LABEL_PREFIX__, are |
| 65 |
|
// gnu-specific.) |
| 66 |
|
#if defined(__GNUC__) && __GNUC__ >= 3 |
| 67 |
|
#if defined(__ELF__) |
| 68 |
|
extern Arg no_arg |
| 69 |
|
__attribute__((alias(__USER_LABEL_PREFIX__ "_ZN7pcrecpp2RE6no_argE"))); |
| 70 |
|
#else |
| 71 |
|
// While we know elf supports strong aliases, not all formats do (Mach |
| 72 |
|
// doesn't, for instance). So make aliases weak by default. This is |
| 73 |
|
// a smidge less safe in theory (conceivably, someone could override |
| 74 |
|
// this symbol in their own binary), but perfectly ok in practice. |
| 75 |
|
extern Arg no_arg |
| 76 |
|
__attribute__((weak, alias(__USER_LABEL_PREFIX__ "_ZN7pcrecpp2RE6no_argE"))); |
| 77 |
|
#endif |
| 78 |
|
#endif |
| 79 |
|
|
| 80 |
// If a regular expression has no error, its error_ field points here |
// If a regular expression has no error, its error_ field points here |
| 81 |
static const string empty_string; |
static const string empty_string; |
| 83 |
// If the user doesn't ask for any options, we just use this one |
// If the user doesn't ask for any options, we just use this one |
| 84 |
static RE_Options default_options; |
static RE_Options default_options; |
| 85 |
|
|
| 86 |
void RE::Init(const char* pat, const RE_Options* options) { |
void RE::Init(const string& pat, const RE_Options* options) { |
| 87 |
pattern_ = pat; |
pattern_ = pat; |
| 88 |
if (options == NULL) { |
if (options == NULL) { |
| 89 |
options_ = default_options; |
options_ = default_options; |
| 96 |
|
|
| 97 |
re_partial_ = Compile(UNANCHORED); |
re_partial_ = Compile(UNANCHORED); |
| 98 |
if (re_partial_ != NULL) { |
if (re_partial_ != NULL) { |
| 99 |
// Check for complicated patterns. The following change is |
re_full_ = Compile(ANCHOR_BOTH); |
|
// conservative in that it may treat some "simple" patterns |
|
|
// as "complex" (e.g., if the vertical bar is in a character |
|
|
// class or is escaped). But it seems good enough. |
|
|
if (strchr(pat, '|') == NULL) { |
|
|
// Simple pattern: we can use position-based checks to perform |
|
|
// fully anchored matches |
|
|
re_full_ = re_partial_; |
|
|
} else { |
|
|
// We need a special pattern for anchored matches |
|
|
re_full_ = Compile(ANCHOR_BOTH); |
|
|
} |
|
| 100 |
} |
} |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
|
void RE::Cleanup() { |
| 104 |
|
if (re_full_ != NULL) (*pcre_free)(re_full_); |
| 105 |
|
if (re_partial_ != NULL) (*pcre_free)(re_partial_); |
| 106 |
|
if (error_ != &empty_string) delete error_; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
|
| 110 |
RE::~RE() { |
RE::~RE() { |
| 111 |
if (re_full_ != NULL && re_full_ != re_partial_) (*pcre_free)(re_full_); |
Cleanup(); |
|
if (re_partial_ != NULL) (*pcre_free)(re_partial_); |
|
|
if (error_ != &empty_string) delete error_; |
|
| 112 |
} |
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
pcre* RE::Compile(Anchor anchor) { |
pcre* RE::Compile(Anchor anchor) { |
| 116 |
// First, convert RE_Options into pcre options |
// First, convert RE_Options into pcre options |
| 117 |
int pcre_options = 0; |
int pcre_options = 0; |
| 118 |
if (options_.utf8()) |
pcre_options = options_.all_options(); |
|
pcre_options |= PCRE_UTF8; |
|
| 119 |
|
|
| 120 |
// Special treatment for anchoring. This is needed because at |
// Special treatment for anchoring. This is needed because at |
| 121 |
// runtime pcre only provides an option for anchoring at the |
// runtime pcre only provides an option for anchoring at the |
| 349 |
return true; |
return true; |
| 350 |
} |
} |
| 351 |
|
|
| 352 |
|
// Returns PCRE_NEWLINE_CRLF, PCRE_NEWLINE_CR, or PCRE_NEWLINE_LF. |
| 353 |
|
// Note that PCRE_NEWLINE_CRLF is defined to be P_N_CR | P_N_LF. |
| 354 |
|
// Modified by PH to add PCRE_NEWLINE_ANY and PCRE_NEWLINE_ANYCRLF. |
| 355 |
|
|
| 356 |
|
static int NewlineMode(int pcre_options) { |
| 357 |
|
// TODO: if we can make it threadsafe, cache this var |
| 358 |
|
int newline_mode = 0; |
| 359 |
|
/* if (newline_mode) return newline_mode; */ // do this once it's cached |
| 360 |
|
if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF| |
| 361 |
|
PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)) { |
| 362 |
|
newline_mode = (pcre_options & |
| 363 |
|
(PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF| |
| 364 |
|
PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)); |
| 365 |
|
} else { |
| 366 |
|
int newline; |
| 367 |
|
pcre_config(PCRE_CONFIG_NEWLINE, &newline); |
| 368 |
|
if (newline == 10) |
| 369 |
|
newline_mode = PCRE_NEWLINE_LF; |
| 370 |
|
else if (newline == 13) |
| 371 |
|
newline_mode = PCRE_NEWLINE_CR; |
| 372 |
|
else if (newline == 3338) |
| 373 |
|
newline_mode = PCRE_NEWLINE_CRLF; |
| 374 |
|
else if (newline == -1) |
| 375 |
|
newline_mode = PCRE_NEWLINE_ANY; |
| 376 |
|
else if (newline == -2) |
| 377 |
|
newline_mode = PCRE_NEWLINE_ANYCRLF; |
| 378 |
|
else |
| 379 |
|
assert("" == "Unexpected return value from pcre_config(NEWLINE)"); |
| 380 |
|
} |
| 381 |
|
return newline_mode; |
| 382 |
|
} |
| 383 |
|
|
| 384 |
int RE::GlobalReplace(const StringPiece& rewrite, |
int RE::GlobalReplace(const StringPiece& rewrite, |
| 385 |
string *str) const { |
string *str) const { |
| 386 |
int count = 0; |
int count = 0; |
| 389 |
int start = 0; |
int start = 0; |
| 390 |
int lastend = -1; |
int lastend = -1; |
| 391 |
|
|
| 392 |
for (; start <= static_cast<int>(str->length()); count++) { |
while (start <= static_cast<int>(str->length())) { |
| 393 |
int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize); |
int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize); |
| 394 |
if (matches <= 0) |
if (matches <= 0) |
| 395 |
break; |
break; |
| 399 |
if (matchstart == matchend && matchstart == lastend) { |
if (matchstart == matchend && matchstart == lastend) { |
| 400 |
// advance one character if we matched an empty string at the same |
// advance one character if we matched an empty string at the same |
| 401 |
// place as the last match occurred |
// place as the last match occurred |
| 402 |
if (start < static_cast<int>(str->length())) |
matchend = start + 1; |
| 403 |
out.push_back((*str)[start]); |
// If the current char is CR and we're in CRLF mode, skip LF too. |
| 404 |
start++; |
// Note it's better to call pcre_fullinfo() than to examine |
| 405 |
|
// all_options(), since options_ could have changed bewteen |
| 406 |
|
// compile-time and now, but this is simpler and safe enough. |
| 407 |
|
// Modified by PH to add ANY and ANYCRLF. |
| 408 |
|
if (start+1 < static_cast<int>(str->length()) && |
| 409 |
|
(*str)[start] == '\r' && (*str)[start+1] == '\n' && |
| 410 |
|
(NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF || |
| 411 |
|
NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANY || |
| 412 |
|
NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANYCRLF) |
| 413 |
|
) { |
| 414 |
|
matchend++; |
| 415 |
|
} |
| 416 |
|
// We also need to advance more than one char if we're in utf8 mode. |
| 417 |
|
#ifdef SUPPORT_UTF8 |
| 418 |
|
if (options_.utf8()) { |
| 419 |
|
while (matchend < static_cast<int>(str->length()) && |
| 420 |
|
((*str)[matchend] & 0xc0) == 0x80) |
| 421 |
|
matchend++; |
| 422 |
|
} |
| 423 |
|
#endif |
| 424 |
|
if (matchend <= static_cast<int>(str->length())) |
| 425 |
|
out.append(*str, start, matchend - start); |
| 426 |
|
start = matchend; |
| 427 |
} else { |
} else { |
| 428 |
out.append(*str, start, matchstart - start); |
out.append(*str, start, matchstart - start); |
| 429 |
Rewrite(&out, rewrite, *str, vec, matches); |
Rewrite(&out, rewrite, *str, vec, matches); |
| 449 |
int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize); |
int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize); |
| 450 |
if (matches == 0) |
if (matches == 0) |
| 451 |
return false; |
return false; |
| 452 |
out->clear(); |
out->erase(); |
| 453 |
return Rewrite(out, rewrite, text, vec, matches); |
return Rewrite(out, rewrite, text, vec, matches); |
| 454 |
} |
} |
| 455 |
|
|
| 456 |
|
/*static*/ string RE::QuoteMeta(const StringPiece& unquoted) { |
| 457 |
|
string result; |
| 458 |
|
|
| 459 |
|
// Escape any ascii character not in [A-Za-z_0-9]. |
| 460 |
|
// |
| 461 |
|
// Note that it's legal to escape a character even if it has no |
| 462 |
|
// special meaning in a regular expression -- so this function does |
| 463 |
|
// that. (This also makes it identical to the perl function of the |
| 464 |
|
// same name; see `perldoc -f quotemeta`.) The one exception is |
| 465 |
|
// escaping NUL: rather than doing backslash + NUL, like perl does, |
| 466 |
|
// we do '\0', because pcre itself doesn't take embedded NUL chars. |
| 467 |
|
for (int ii = 0; ii < unquoted.size(); ++ii) { |
| 468 |
|
// Note that using 'isalnum' here raises the benchmark time from |
| 469 |
|
// 32ns to 58ns: |
| 470 |
|
if (unquoted[ii] == '\0') { |
| 471 |
|
result += "\\0"; |
| 472 |
|
} else if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') && |
| 473 |
|
(unquoted[ii] < 'A' || unquoted[ii] > 'Z') && |
| 474 |
|
(unquoted[ii] < '0' || unquoted[ii] > '9') && |
| 475 |
|
unquoted[ii] != '_' && |
| 476 |
|
// If this is the part of a UTF8 or Latin1 character, we need |
| 477 |
|
// to copy this byte without escaping. Experimentally this is |
| 478 |
|
// what works correctly with the regexp library. |
| 479 |
|
!(unquoted[ii] & 128)) { |
| 480 |
|
result += '\\'; |
| 481 |
|
result += unquoted[ii]; |
| 482 |
|
} else { |
| 483 |
|
result += unquoted[ii]; |
| 484 |
|
} |
| 485 |
|
} |
| 486 |
|
|
| 487 |
|
return result; |
| 488 |
|
} |
| 489 |
|
|
| 490 |
/***** Actual matching and rewriting code *****/ |
/***** Actual matching and rewriting code *****/ |
| 491 |
|
|
| 492 |
int RE::TryMatch(const StringPiece& text, |
int RE::TryMatch(const StringPiece& text, |
| 500 |
return 0; |
return 0; |
| 501 |
} |
} |
| 502 |
|
|
| 503 |
pcre_extra extra = { 0 }; |
pcre_extra extra = { 0, 0, 0, 0, 0, 0 }; |
| 504 |
if (options_.match_limit() > 0) { |
if (options_.match_limit() > 0) { |
| 505 |
extra.flags = PCRE_EXTRA_MATCH_LIMIT; |
extra.flags |= PCRE_EXTRA_MATCH_LIMIT; |
| 506 |
extra.match_limit = options_.match_limit(); |
extra.match_limit = options_.match_limit(); |
| 507 |
} |
} |
| 508 |
|
if (options_.match_limit_recursion() > 0) { |
| 509 |
|
extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION; |
| 510 |
|
extra.match_limit_recursion = options_.match_limit_recursion(); |
| 511 |
|
} |
| 512 |
int rc = pcre_exec(re, // The regular expression object |
int rc = pcre_exec(re, // The regular expression object |
| 513 |
&extra, |
&extra, |
| 514 |
text.data(), |
(text.data() == NULL) ? "" : text.data(), |
| 515 |
text.size(), |
text.size(), |
| 516 |
startpos, |
startpos, |
| 517 |
(anchor == UNANCHORED) ? 0 : PCRE_ANCHORED, |
(anchor == UNANCHORED) ? 0 : PCRE_ANCHORED, |
| 533 |
rc = vecsize / 2; |
rc = vecsize / 2; |
| 534 |
} |
} |
| 535 |
|
|
|
if ((anchor == ANCHOR_BOTH) && (re_full_ == re_partial_)) { |
|
|
// We need an extra check to make sure that the match extended |
|
|
// to the end of the input string |
|
|
assert(vec[0] == 0); // PCRE_ANCHORED forces starting match |
|
|
if (vec[1] != text.size()) return 0; // Did not get ending match |
|
|
} |
|
|
|
|
| 536 |
return rc; |
return rc; |
| 537 |
} |
} |
| 538 |
|
|
| 551 |
|
|
| 552 |
*consumed = vec[1]; |
*consumed = vec[1]; |
| 553 |
|
|
| 554 |
if (args == NULL) { |
if (n == 0 || args == NULL) { |
| 555 |
// We are not interested in results |
// We are not interested in results |
| 556 |
return true; |
return true; |
| 557 |
} |
} |
| 558 |
|
|
| 559 |
|
if (NumberOfCapturingGroups() < n) { |
| 560 |
|
// RE has fewer capturing groups than number of arg pointers passed in |
| 561 |
|
return false; |
| 562 |
|
} |
| 563 |
|
|
| 564 |
// If we got here, we must have matched the whole pattern. |
// If we got here, we must have matched the whole pattern. |
| 565 |
// 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 |
| 566 |
// -- see the comment for TryMatch. |
// -- see the comment for TryMatch. |
| 624 |
|
|
| 625 |
// Return the number of capturing subpatterns, or -1 if the |
// Return the number of capturing subpatterns, or -1 if the |
| 626 |
// regexp wasn't valid on construction. |
// regexp wasn't valid on construction. |
| 627 |
int RE::NumberOfCapturingGroups() { |
int RE::NumberOfCapturingGroups() const { |
| 628 |
if (re_partial_ == NULL) return -1; |
if (re_partial_ == NULL) return -1; |
| 629 |
|
|
| 630 |
int result; |
int result; |
| 644 |
} |
} |
| 645 |
|
|
| 646 |
bool Arg::parse_string(const char* str, int n, void* dest) { |
bool Arg::parse_string(const char* str, int n, void* dest) { |
| 647 |
|
if (dest == NULL) return true; |
| 648 |
reinterpret_cast<string*>(dest)->assign(str, n); |
reinterpret_cast<string*>(dest)->assign(str, n); |
| 649 |
return true; |
return true; |
| 650 |
} |
} |
| 651 |
|
|
| 652 |
bool Arg::parse_stringpiece(const char* str, int n, void* dest) { |
bool Arg::parse_stringpiece(const char* str, int n, void* dest) { |
| 653 |
|
if (dest == NULL) return true; |
| 654 |
reinterpret_cast<StringPiece*>(dest)->set(str, n); |
reinterpret_cast<StringPiece*>(dest)->set(str, n); |
| 655 |
return true; |
return true; |
| 656 |
} |
} |
| 657 |
|
|
| 658 |
bool Arg::parse_char(const char* str, int n, void* dest) { |
bool Arg::parse_char(const char* str, int n, void* dest) { |
| 659 |
if (n != 1) return false; |
if (n != 1) return false; |
| 660 |
|
if (dest == NULL) return true; |
| 661 |
*(reinterpret_cast<char*>(dest)) = str[0]; |
*(reinterpret_cast<char*>(dest)) = str[0]; |
| 662 |
return true; |
return true; |
| 663 |
} |
} |
| 664 |
|
|
| 665 |
bool Arg::parse_uchar(const char* str, int n, void* dest) { |
bool Arg::parse_uchar(const char* str, int n, void* dest) { |
| 666 |
if (n != 1) return false; |
if (n != 1) return false; |
| 667 |
|
if (dest == NULL) return true; |
| 668 |
*(reinterpret_cast<unsigned char*>(dest)) = str[0]; |
*(reinterpret_cast<unsigned char*>(dest)) = str[0]; |
| 669 |
return true; |
return true; |
| 670 |
} |
} |
| 713 |
long r = strtol(str, &end, radix); |
long r = strtol(str, &end, radix); |
| 714 |
if (end != str + n) return false; // Leftover junk |
if (end != str + n) return false; // Leftover junk |
| 715 |
if (errno) return false; |
if (errno) return false; |
| 716 |
|
if (dest == NULL) return true; |
| 717 |
*(reinterpret_cast<long*>(dest)) = r; |
*(reinterpret_cast<long*>(dest)) = r; |
| 718 |
return true; |
return true; |
| 719 |
} |
} |
| 725 |
if (n == 0) return false; |
if (n == 0) return false; |
| 726 |
char buf[kMaxNumberLength+1]; |
char buf[kMaxNumberLength+1]; |
| 727 |
str = TerminateNumber(buf, str, n); |
str = TerminateNumber(buf, str, n); |
| 728 |
|
if (str[0] == '-') return false; // strtoul() on a negative number?! |
| 729 |
char* end; |
char* end; |
| 730 |
errno = 0; |
errno = 0; |
| 731 |
unsigned long r = strtoul(str, &end, radix); |
unsigned long r = strtoul(str, &end, radix); |
| 732 |
if (end != str + n) return false; // Leftover junk |
if (end != str + n) return false; // Leftover junk |
| 733 |
if (errno) return false; |
if (errno) return false; |
| 734 |
|
if (dest == NULL) return true; |
| 735 |
*(reinterpret_cast<unsigned long*>(dest)) = r; |
*(reinterpret_cast<unsigned long*>(dest)) = r; |
| 736 |
return true; |
return true; |
| 737 |
} |
} |
| 743 |
long r; |
long r; |
| 744 |
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse |
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse |
| 745 |
if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range |
if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range |
| 746 |
*(reinterpret_cast<short*>(dest)) = r; |
if (dest == NULL) return true; |
| 747 |
|
*(reinterpret_cast<short*>(dest)) = static_cast<short>(r); |
| 748 |
return true; |
return true; |
| 749 |
} |
} |
| 750 |
|
|
| 755 |
unsigned long r; |
unsigned long r; |
| 756 |
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse |
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse |
| 757 |
if (r > USHRT_MAX) return false; // Out of range |
if (r > USHRT_MAX) return false; // Out of range |
| 758 |
*(reinterpret_cast<unsigned short*>(dest)) = r; |
if (dest == NULL) return true; |
| 759 |
|
*(reinterpret_cast<unsigned short*>(dest)) = static_cast<unsigned short>(r); |
| 760 |
return true; |
return true; |
| 761 |
} |
} |
| 762 |
|
|
| 767 |
long r; |
long r; |
| 768 |
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse |
if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse |
| 769 |
if (r < INT_MIN || r > INT_MAX) return false; // Out of range |
if (r < INT_MIN || r > INT_MAX) return false; // Out of range |
| 770 |
|
if (dest == NULL) return true; |
| 771 |
*(reinterpret_cast<int*>(dest)) = r; |
*(reinterpret_cast<int*>(dest)) = r; |
| 772 |
return true; |
return true; |
| 773 |
} |
} |
| 779 |
unsigned long r; |
unsigned long r; |
| 780 |
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse |
if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse |
| 781 |
if (r > UINT_MAX) return false; // Out of range |
if (r > UINT_MAX) return false; // Out of range |
| 782 |
|
if (dest == NULL) return true; |
| 783 |
*(reinterpret_cast<unsigned int*>(dest)) = r; |
*(reinterpret_cast<unsigned int*>(dest)) = r; |
| 784 |
return true; |
return true; |
| 785 |
} |
} |
| 800 |
long long r = strtoq(str, &end, radix); |
long long r = strtoq(str, &end, radix); |
| 801 |
#elif defined HAVE_STRTOLL |
#elif defined HAVE_STRTOLL |
| 802 |
long long r = strtoll(str, &end, radix); |
long long r = strtoll(str, &end, radix); |
| 803 |
|
#elif defined HAVE__STRTOI64 |
| 804 |
|
long long r = _strtoi64(str, &end, radix); |
| 805 |
#else |
#else |
| 806 |
#error parse_longlong_radix: cannot convert input to a long-long |
#error parse_longlong_radix: cannot convert input to a long-long |
| 807 |
#endif |
#endif |
| 808 |
if (end != str + n) return false; // Leftover junk |
if (end != str + n) return false; // Leftover junk |
| 809 |
if (errno) return false; |
if (errno) return false; |
| 810 |
|
if (dest == NULL) return true; |
| 811 |
*(reinterpret_cast<long long*>(dest)) = r; |
*(reinterpret_cast<long long*>(dest)) = r; |
| 812 |
return true; |
return true; |
| 813 |
#endif /* HAVE_LONG_LONG */ |
#endif /* HAVE_LONG_LONG */ |
| 823 |
if (n == 0) return false; |
if (n == 0) return false; |
| 824 |
char buf[kMaxNumberLength+1]; |
char buf[kMaxNumberLength+1]; |
| 825 |
str = TerminateNumber(buf, str, n); |
str = TerminateNumber(buf, str, n); |
| 826 |
|
if (str[0] == '-') return false; // strtoull() on a negative number?! |
| 827 |
char* end; |
char* end; |
| 828 |
errno = 0; |
errno = 0; |
| 829 |
#if defined HAVE_STRTOQ |
#if defined HAVE_STRTOQ |
| 830 |
unsigned long long r = strtouq(str, &end, radix); |
unsigned long long r = strtouq(str, &end, radix); |
| 831 |
#elif defined HAVE_STRTOLL |
#elif defined HAVE_STRTOLL |
| 832 |
unsigned long long r = strtoull(str, &end, radix); |
unsigned long long r = strtoull(str, &end, radix); |
| 833 |
|
#elif defined HAVE__STRTOI64 |
| 834 |
|
unsigned long long r = _strtoui64(str, &end, radix); |
| 835 |
#else |
#else |
| 836 |
#error parse_ulonglong_radix: cannot convert input to a long-long |
#error parse_ulonglong_radix: cannot convert input to a long-long |
| 837 |
#endif |
#endif |
| 838 |
if (end != str + n) return false; // Leftover junk |
if (end != str + n) return false; // Leftover junk |
| 839 |
if (errno) return false; |
if (errno) return false; |
| 840 |
|
if (dest == NULL) return true; |
| 841 |
*(reinterpret_cast<unsigned long long*>(dest)) = r; |
*(reinterpret_cast<unsigned long long*>(dest)) = r; |
| 842 |
return true; |
return true; |
| 843 |
#endif /* HAVE_UNSIGNED_LONG_LONG */ |
#endif /* HAVE_UNSIGNED_LONG_LONG */ |
| 855 |
double r = strtod(buf, &end); |
double r = strtod(buf, &end); |
| 856 |
if (end != buf + n) return false; // Leftover junk |
if (end != buf + n) return false; // Leftover junk |
| 857 |
if (errno) return false; |
if (errno) return false; |
| 858 |
|
if (dest == NULL) return true; |
| 859 |
*(reinterpret_cast<double*>(dest)) = r; |
*(reinterpret_cast<double*>(dest)) = r; |
| 860 |
return true; |
return true; |
| 861 |
} |
} |
| 863 |
bool Arg::parse_float(const char* str, int n, void* dest) { |
bool Arg::parse_float(const char* str, int n, void* dest) { |
| 864 |
double r; |
double r; |
| 865 |
if (!parse_double(str, n, &r)) return false; |
if (!parse_double(str, n, &r)) return false; |
| 866 |
|
if (dest == NULL) return true; |
| 867 |
*(reinterpret_cast<float*>(dest)) = static_cast<float>(r); |
*(reinterpret_cast<float*>(dest)) = static_cast<float>(r); |
| 868 |
return true; |
return true; |
| 869 |
} |
} |
| 883 |
return parse_##name##_radix(str, n, dest, 0); \ |
return parse_##name##_radix(str, n, dest, 0); \ |
| 884 |
} |
} |
| 885 |
|
|
| 886 |
DEFINE_INTEGER_PARSERS(short); |
DEFINE_INTEGER_PARSERS(short) /* */ |
| 887 |
DEFINE_INTEGER_PARSERS(ushort); |
DEFINE_INTEGER_PARSERS(ushort) /* */ |
| 888 |
DEFINE_INTEGER_PARSERS(int); |
DEFINE_INTEGER_PARSERS(int) /* Don't use semicolons after these */ |
| 889 |
DEFINE_INTEGER_PARSERS(uint); |
DEFINE_INTEGER_PARSERS(uint) /* statements because they can cause */ |
| 890 |
DEFINE_INTEGER_PARSERS(long); |
DEFINE_INTEGER_PARSERS(long) /* compiler warnings if the checking */ |
| 891 |
DEFINE_INTEGER_PARSERS(ulong); |
DEFINE_INTEGER_PARSERS(ulong) /* level is turned up high enough. */ |
| 892 |
DEFINE_INTEGER_PARSERS(longlong); |
DEFINE_INTEGER_PARSERS(longlong) /* */ |
| 893 |
DEFINE_INTEGER_PARSERS(ulonglong); |
DEFINE_INTEGER_PARSERS(ulonglong) /* */ |
| 894 |
|
|
| 895 |
#undef DEFINE_INTEGER_PARSERS |
#undef DEFINE_INTEGER_PARSERS |
| 896 |
|
|