123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <limits.h> /* for SHRT_MIN, USHRT_MAX, etc */
- #include <string.h> /* for memcpy */
- #include <assert.h>
- #include <errno.h>
- #include <string>
- #include <algorithm>
- #include "pcrecpp_internal.h"
- #include "pcre.h"
- #include "pcrecpp.h"
- #include "pcre_stringpiece.h"
- namespace pcrecpp {
- static const int kMaxArgs = 16;
- static const int kVecSize = (1 + kMaxArgs) * 3;
- Arg RE::no_arg((void*)NULL);
- #if defined(__GNUC__) && __GNUC__ >= 3 && defined(__ELF__) && !defined(__INTEL_COMPILER)
- # define ULP_AS_STRING(x) ULP_AS_STRING_INTERNAL(x)
- # define ULP_AS_STRING_INTERNAL(x) #x
- # define USER_LABEL_PREFIX_STR ULP_AS_STRING(__USER_LABEL_PREFIX__)
- extern Arg no_arg
- __attribute__((alias(USER_LABEL_PREFIX_STR "_ZN7pcrecpp2RE6no_argE")));
- #endif
- static const string empty_string;
- static RE_Options default_options;
- static const char *start_options[] = {
- "(*UTF8)",
- "(*UTF)",
- "(*UCP)",
- "(*NO_START_OPT)",
- "(*NO_AUTO_POSSESS)",
- "(*LIMIT_RECURSION=",
- "(*LIMIT_MATCH=",
- "(*CRLF)",
- "(*CR)",
- "(*BSR_UNICODE)",
- "(*BSR_ANYCRLF)",
- "(*ANYCRLF)",
- "(*ANY)",
- "" };
- void RE::Init(const string& pat, const RE_Options* options) {
- pattern_ = pat;
- if (options == NULL) {
- options_ = default_options;
- } else {
- options_ = *options;
- }
- error_ = &empty_string;
- re_full_ = NULL;
- re_partial_ = NULL;
- re_partial_ = Compile(UNANCHORED);
- if (re_partial_ != NULL) {
- re_full_ = Compile(ANCHOR_BOTH);
- }
- }
- void RE::Cleanup() {
- if (re_full_ != NULL) (*pcre_free)(re_full_);
- if (re_partial_ != NULL) (*pcre_free)(re_partial_);
- if (error_ != &empty_string) delete error_;
- }
- RE::~RE() {
- Cleanup();
- }
- pcre* RE::Compile(Anchor anchor) {
-
- int pcre_options = 0;
- pcre_options = options_.all_options();
-
-
-
-
-
-
-
-
-
-
-
- const char* compile_error;
- int eoffset;
- pcre* re;
- if (anchor != ANCHOR_BOTH) {
- re = pcre_compile(pattern_.c_str(), pcre_options,
- &compile_error, &eoffset, NULL);
- } else {
-
-
-
- string wrapped = "";
- if (pattern_.c_str()[0] == '(' && pattern_.c_str()[1] == '*') {
- int kk, klen, kmat;
- for (;;) {
- for (kk = 0; start_options[kk][0] != 0; kk++) {
- klen = strlen(start_options[kk]);
- kmat = strncmp(pattern_.c_str(), start_options[kk], klen);
- if (kmat >= 0) break;
- }
- if (kmat != 0) break;
-
- if (start_options[kk][klen-1] == '=') {
- while (isdigit(pattern_.c_str()[klen])) klen++;
- if (pattern_.c_str()[klen] != ')') break;
- klen++;
- }
-
- wrapped += pattern_.substr(0, klen);
- pattern_.erase(0, klen);
- }
- }
-
- wrapped += "(?:";
- wrapped += pattern_;
- wrapped += ")\\z";
- re = pcre_compile(wrapped.c_str(), pcre_options,
- &compile_error, &eoffset, NULL);
- }
- if (re == NULL) {
- if (error_ == &empty_string) error_ = new string(compile_error);
- }
- return re;
- }
- bool RE::FullMatch(const StringPiece& text,
- const Arg& ptr1,
- const Arg& ptr2,
- const Arg& ptr3,
- const Arg& ptr4,
- const Arg& ptr5,
- const Arg& ptr6,
- const Arg& ptr7,
- const Arg& ptr8,
- const Arg& ptr9,
- const Arg& ptr10,
- const Arg& ptr11,
- const Arg& ptr12,
- const Arg& ptr13,
- const Arg& ptr14,
- const Arg& ptr15,
- const Arg& ptr16) const {
- const Arg* args[kMaxArgs];
- int n = 0;
- if (&ptr1 == &no_arg) { goto done; } args[n++] = &ptr1;
- if (&ptr2 == &no_arg) { goto done; } args[n++] = &ptr2;
- if (&ptr3 == &no_arg) { goto done; } args[n++] = &ptr3;
- if (&ptr4 == &no_arg) { goto done; } args[n++] = &ptr4;
- if (&ptr5 == &no_arg) { goto done; } args[n++] = &ptr5;
- if (&ptr6 == &no_arg) { goto done; } args[n++] = &ptr6;
- if (&ptr7 == &no_arg) { goto done; } args[n++] = &ptr7;
- if (&ptr8 == &no_arg) { goto done; } args[n++] = &ptr8;
- if (&ptr9 == &no_arg) { goto done; } args[n++] = &ptr9;
- if (&ptr10 == &no_arg) { goto done; } args[n++] = &ptr10;
- if (&ptr11 == &no_arg) { goto done; } args[n++] = &ptr11;
- if (&ptr12 == &no_arg) { goto done; } args[n++] = &ptr12;
- if (&ptr13 == &no_arg) { goto done; } args[n++] = &ptr13;
- if (&ptr14 == &no_arg) { goto done; } args[n++] = &ptr14;
- if (&ptr15 == &no_arg) { goto done; } args[n++] = &ptr15;
- if (&ptr16 == &no_arg) { goto done; } args[n++] = &ptr16;
- done:
- int consumed;
- int vec[kVecSize];
- return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
- }
- bool RE::PartialMatch(const StringPiece& text,
- const Arg& ptr1,
- const Arg& ptr2,
- const Arg& ptr3,
- const Arg& ptr4,
- const Arg& ptr5,
- const Arg& ptr6,
- const Arg& ptr7,
- const Arg& ptr8,
- const Arg& ptr9,
- const Arg& ptr10,
- const Arg& ptr11,
- const Arg& ptr12,
- const Arg& ptr13,
- const Arg& ptr14,
- const Arg& ptr15,
- const Arg& ptr16) const {
- const Arg* args[kMaxArgs];
- int n = 0;
- if (&ptr1 == &no_arg) { goto done; } args[n++] = &ptr1;
- if (&ptr2 == &no_arg) { goto done; } args[n++] = &ptr2;
- if (&ptr3 == &no_arg) { goto done; } args[n++] = &ptr3;
- if (&ptr4 == &no_arg) { goto done; } args[n++] = &ptr4;
- if (&ptr5 == &no_arg) { goto done; } args[n++] = &ptr5;
- if (&ptr6 == &no_arg) { goto done; } args[n++] = &ptr6;
- if (&ptr7 == &no_arg) { goto done; } args[n++] = &ptr7;
- if (&ptr8 == &no_arg) { goto done; } args[n++] = &ptr8;
- if (&ptr9 == &no_arg) { goto done; } args[n++] = &ptr9;
- if (&ptr10 == &no_arg) { goto done; } args[n++] = &ptr10;
- if (&ptr11 == &no_arg) { goto done; } args[n++] = &ptr11;
- if (&ptr12 == &no_arg) { goto done; } args[n++] = &ptr12;
- if (&ptr13 == &no_arg) { goto done; } args[n++] = &ptr13;
- if (&ptr14 == &no_arg) { goto done; } args[n++] = &ptr14;
- if (&ptr15 == &no_arg) { goto done; } args[n++] = &ptr15;
- if (&ptr16 == &no_arg) { goto done; } args[n++] = &ptr16;
- done:
- int consumed;
- int vec[kVecSize];
- return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
- }
- bool RE::Consume(StringPiece* input,
- const Arg& ptr1,
- const Arg& ptr2,
- const Arg& ptr3,
- const Arg& ptr4,
- const Arg& ptr5,
- const Arg& ptr6,
- const Arg& ptr7,
- const Arg& ptr8,
- const Arg& ptr9,
- const Arg& ptr10,
- const Arg& ptr11,
- const Arg& ptr12,
- const Arg& ptr13,
- const Arg& ptr14,
- const Arg& ptr15,
- const Arg& ptr16) const {
- const Arg* args[kMaxArgs];
- int n = 0;
- if (&ptr1 == &no_arg) { goto done; } args[n++] = &ptr1;
- if (&ptr2 == &no_arg) { goto done; } args[n++] = &ptr2;
- if (&ptr3 == &no_arg) { goto done; } args[n++] = &ptr3;
- if (&ptr4 == &no_arg) { goto done; } args[n++] = &ptr4;
- if (&ptr5 == &no_arg) { goto done; } args[n++] = &ptr5;
- if (&ptr6 == &no_arg) { goto done; } args[n++] = &ptr6;
- if (&ptr7 == &no_arg) { goto done; } args[n++] = &ptr7;
- if (&ptr8 == &no_arg) { goto done; } args[n++] = &ptr8;
- if (&ptr9 == &no_arg) { goto done; } args[n++] = &ptr9;
- if (&ptr10 == &no_arg) { goto done; } args[n++] = &ptr10;
- if (&ptr11 == &no_arg) { goto done; } args[n++] = &ptr11;
- if (&ptr12 == &no_arg) { goto done; } args[n++] = &ptr12;
- if (&ptr13 == &no_arg) { goto done; } args[n++] = &ptr13;
- if (&ptr14 == &no_arg) { goto done; } args[n++] = &ptr14;
- if (&ptr15 == &no_arg) { goto done; } args[n++] = &ptr15;
- if (&ptr16 == &no_arg) { goto done; } args[n++] = &ptr16;
- done:
- int consumed;
- int vec[kVecSize];
- if (DoMatchImpl(*input, ANCHOR_START, &consumed,
- args, n, vec, kVecSize)) {
- input->remove_prefix(consumed);
- return true;
- } else {
- return false;
- }
- }
- bool RE::FindAndConsume(StringPiece* input,
- const Arg& ptr1,
- const Arg& ptr2,
- const Arg& ptr3,
- const Arg& ptr4,
- const Arg& ptr5,
- const Arg& ptr6,
- const Arg& ptr7,
- const Arg& ptr8,
- const Arg& ptr9,
- const Arg& ptr10,
- const Arg& ptr11,
- const Arg& ptr12,
- const Arg& ptr13,
- const Arg& ptr14,
- const Arg& ptr15,
- const Arg& ptr16) const {
- const Arg* args[kMaxArgs];
- int n = 0;
- if (&ptr1 == &no_arg) { goto done; } args[n++] = &ptr1;
- if (&ptr2 == &no_arg) { goto done; } args[n++] = &ptr2;
- if (&ptr3 == &no_arg) { goto done; } args[n++] = &ptr3;
- if (&ptr4 == &no_arg) { goto done; } args[n++] = &ptr4;
- if (&ptr5 == &no_arg) { goto done; } args[n++] = &ptr5;
- if (&ptr6 == &no_arg) { goto done; } args[n++] = &ptr6;
- if (&ptr7 == &no_arg) { goto done; } args[n++] = &ptr7;
- if (&ptr8 == &no_arg) { goto done; } args[n++] = &ptr8;
- if (&ptr9 == &no_arg) { goto done; } args[n++] = &ptr9;
- if (&ptr10 == &no_arg) { goto done; } args[n++] = &ptr10;
- if (&ptr11 == &no_arg) { goto done; } args[n++] = &ptr11;
- if (&ptr12 == &no_arg) { goto done; } args[n++] = &ptr12;
- if (&ptr13 == &no_arg) { goto done; } args[n++] = &ptr13;
- if (&ptr14 == &no_arg) { goto done; } args[n++] = &ptr14;
- if (&ptr15 == &no_arg) { goto done; } args[n++] = &ptr15;
- if (&ptr16 == &no_arg) { goto done; } args[n++] = &ptr16;
- done:
- int consumed;
- int vec[kVecSize];
- if (DoMatchImpl(*input, UNANCHORED, &consumed,
- args, n, vec, kVecSize)) {
- input->remove_prefix(consumed);
- return true;
- } else {
- return false;
- }
- }
- bool RE::Replace(const StringPiece& rewrite,
- string *str) const {
- int vec[kVecSize];
- int matches = TryMatch(*str, 0, UNANCHORED, true, vec, kVecSize);
- if (matches == 0)
- return false;
- string s;
- if (!Rewrite(&s, rewrite, *str, vec, matches))
- return false;
- assert(vec[0] >= 0);
- assert(vec[1] >= 0);
- str->replace(vec[0], vec[1] - vec[0], s);
- return true;
- }
- static int NewlineMode(int pcre_options) {
-
- int newline_mode = 0;
-
- if (pcre_options & (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
- PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF)) {
- newline_mode = (pcre_options &
- (PCRE_NEWLINE_CRLF|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|
- PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF));
- } else {
- int newline;
- pcre_config(PCRE_CONFIG_NEWLINE, &newline);
- if (newline == 10)
- newline_mode = PCRE_NEWLINE_LF;
- else if (newline == 13)
- newline_mode = PCRE_NEWLINE_CR;
- else if (newline == 3338)
- newline_mode = PCRE_NEWLINE_CRLF;
- else if (newline == -1)
- newline_mode = PCRE_NEWLINE_ANY;
- else if (newline == -2)
- newline_mode = PCRE_NEWLINE_ANYCRLF;
- else
- assert(NULL == "Unexpected return value from pcre_config(NEWLINE)");
- }
- return newline_mode;
- }
- int RE::GlobalReplace(const StringPiece& rewrite,
- string *str) const {
- int count = 0;
- int vec[kVecSize];
- string out;
- int start = 0;
- bool last_match_was_empty_string = false;
- while (start <= static_cast<int>(str->length())) {
-
-
-
-
-
-
-
-
-
-
- int matches;
- if (last_match_was_empty_string) {
- matches = TryMatch(*str, start, ANCHOR_START, false, vec, kVecSize);
- if (matches <= 0) {
- int matchend = start + 1;
-
-
-
-
-
- if (matchend < static_cast<int>(str->length()) &&
- (*str)[start] == '\r' && (*str)[matchend] == '\n' &&
- (NewlineMode(options_.all_options()) == PCRE_NEWLINE_CRLF ||
- NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANY ||
- NewlineMode(options_.all_options()) == PCRE_NEWLINE_ANYCRLF)) {
- matchend++;
- }
-
- #ifdef SUPPORT_UTF
- if (options_.utf8()) {
- while (matchend < static_cast<int>(str->length()) &&
- ((*str)[matchend] & 0xc0) == 0x80)
- matchend++;
- }
- #endif
- if (start < static_cast<int>(str->length()))
- out.append(*str, start, matchend - start);
- start = matchend;
- last_match_was_empty_string = false;
- continue;
- }
- } else {
- matches = TryMatch(*str, start, UNANCHORED, true, vec, kVecSize);
- if (matches <= 0)
- break;
- }
- int matchstart = vec[0], matchend = vec[1];
- assert(matchstart >= start);
- assert(matchend >= matchstart);
- out.append(*str, start, matchstart - start);
- Rewrite(&out, rewrite, *str, vec, matches);
- start = matchend;
- count++;
- last_match_was_empty_string = (matchstart == matchend);
- }
- if (count == 0)
- return 0;
- if (start < static_cast<int>(str->length()))
- out.append(*str, start, str->length() - start);
- swap(out, *str);
- return count;
- }
- bool RE::Extract(const StringPiece& rewrite,
- const StringPiece& text,
- string *out) const {
- int vec[kVecSize];
- int matches = TryMatch(text, 0, UNANCHORED, true, vec, kVecSize);
- if (matches == 0)
- return false;
- out->erase();
- return Rewrite(out, rewrite, text, vec, matches);
- }
- string RE::QuoteMeta(const StringPiece& unquoted) {
- string result;
-
-
-
-
-
-
-
-
- for (int ii = 0; ii < unquoted.size(); ++ii) {
-
-
- if (unquoted[ii] == '\0') {
- result += "\\0";
- } else if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
- (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
- (unquoted[ii] < '0' || unquoted[ii] > '9') &&
- unquoted[ii] != '_' &&
-
-
-
- !(unquoted[ii] & 128)) {
- result += '\\';
- result += unquoted[ii];
- } else {
- result += unquoted[ii];
- }
- }
- return result;
- }
- int RE::TryMatch(const StringPiece& text,
- int startpos,
- Anchor anchor,
- bool empty_ok,
- int *vec,
- int vecsize) const {
- pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
- if (re == NULL) {
-
- return 0;
- }
- pcre_extra extra = { 0, 0, 0, 0, 0, 0, 0, 0 };
- if (options_.match_limit() > 0) {
- extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
- extra.match_limit = options_.match_limit();
- }
- if (options_.match_limit_recursion() > 0) {
- extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
- extra.match_limit_recursion = options_.match_limit_recursion();
- }
-
-
- int options = (options_.all_options() & PCRE_NO_UTF8_CHECK);
- if (anchor != UNANCHORED)
- options |= PCRE_ANCHORED;
- if (!empty_ok)
- options |= PCRE_NOTEMPTY;
- int rc = pcre_exec(re,
- &extra,
- (text.data() == NULL) ? "" : text.data(),
- text.size(),
- startpos,
- options,
- vec,
- vecsize);
-
- if (rc == PCRE_ERROR_NOMATCH) {
- return 0;
- } else if (rc < 0) {
-
-
- return 0;
- } else if (rc == 0) {
-
-
-
-
- rc = vecsize / 2;
- }
- return rc;
- }
- bool RE::DoMatchImpl(const StringPiece& text,
- Anchor anchor,
- int* consumed,
- const Arg* const* args,
- int n,
- int* vec,
- int vecsize) const {
- assert((1 + n) * 3 <= vecsize);
- int matches = TryMatch(text, 0, anchor, true, vec, vecsize);
- assert(matches >= 0);
- if (matches == 0)
- return false;
- *consumed = vec[1];
- if (n == 0 || args == NULL) {
-
- return true;
- }
- if (NumberOfCapturingGroups() < n) {
-
- return false;
- }
-
-
-
- for (int i = 0; i < n; i++) {
- const int start = vec[2*(i+1)];
- const int limit = vec[2*(i+1)+1];
- if (!args[i]->Parse(text.data() + start, limit-start)) {
-
- return false;
- }
- }
- return true;
- }
- bool RE::DoMatch(const StringPiece& text,
- Anchor anchor,
- int* consumed,
- const Arg* const args[],
- int n) const {
- assert(n >= 0);
- size_t const vecsize = (1 + n) * 3;
-
- int space[21];
- int* vec = vecsize <= 21 ? space : new int[vecsize];
- bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, (int)vecsize);
- if (vec != space) delete [] vec;
- return retval;
- }
- bool RE::Rewrite(string *out, const StringPiece &rewrite,
- const StringPiece &text, int *vec, int veclen) const {
- for (const char *s = rewrite.data(), *end = s + rewrite.size();
- s < end; s++) {
- int c = *s;
- if (c == '\\') {
- c = *++s;
- if (isdigit(c)) {
- int n = (c - '0');
- if (n >= veclen) {
-
-
- return false;
- }
- int start = vec[2 * n];
- if (start >= 0)
- out->append(text.data() + start, vec[2 * n + 1] - start);
- } else if (c == '\\') {
- *out += '\\';
- } else {
-
-
- return false;
- }
- } else {
- *out += c;
- }
- }
- return true;
- }
- int RE::NumberOfCapturingGroups() const {
- if (re_partial_ == NULL) return -1;
- int result;
- int pcre_retval = pcre_fullinfo(re_partial_,
- NULL,
- PCRE_INFO_CAPTURECOUNT,
- &result);
- assert(pcre_retval == 0);
- return result;
- }
- bool Arg::parse_null(const char* str, int n, void* dest) {
- (void)str;
- (void)n;
-
- return (dest == NULL);
- }
- bool Arg::parse_string(const char* str, int n, void* dest) {
- if (dest == NULL) return true;
- reinterpret_cast<string*>(dest)->assign(str, n);
- return true;
- }
- bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
- if (dest == NULL) return true;
- reinterpret_cast<StringPiece*>(dest)->set(str, n);
- return true;
- }
- bool Arg::parse_char(const char* str, int n, void* dest) {
- if (n != 1) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<char*>(dest)) = str[0];
- return true;
- }
- bool Arg::parse_uchar(const char* str, int n, void* dest) {
- if (n != 1) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<unsigned char*>(dest)) = str[0];
- return true;
- }
- static const int kMaxNumberLength = 32;
- static const char* TerminateNumber(char* buf, const char* str, int n) {
- if ((n > 0) && isspace(*str)) {
-
-
- return "";
- }
-
-
- if (isdigit(str[n]) ||
- ((str[n] >= 'a') && (str[n] <= 'f')) ||
- ((str[n] >= 'A') && (str[n] <= 'F'))) {
- if (n > kMaxNumberLength) return "";
- memcpy(buf, str, n);
- buf[n] = '\0';
- return buf;
- } else {
-
- return str;
- }
- }
- bool Arg::parse_long_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- if (n == 0) return false;
- char buf[kMaxNumberLength+1];
- str = TerminateNumber(buf, str, n);
- char* end;
- errno = 0;
- long r = strtol(str, &end, radix);
- if (end != str + n) return false;
- if (errno) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<long*>(dest)) = r;
- return true;
- }
- bool Arg::parse_ulong_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- if (n == 0) return false;
- char buf[kMaxNumberLength+1];
- str = TerminateNumber(buf, str, n);
- if (str[0] == '-') return false;
- char* end;
- errno = 0;
- unsigned long r = strtoul(str, &end, radix);
- if (end != str + n) return false;
- if (errno) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<unsigned long*>(dest)) = r;
- return true;
- }
- bool Arg::parse_short_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- long r;
- if (!parse_long_radix(str, n, &r, radix)) return false;
- if (r < SHRT_MIN || r > SHRT_MAX) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<short*>(dest)) = static_cast<short>(r);
- return true;
- }
- bool Arg::parse_ushort_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- unsigned long r;
- if (!parse_ulong_radix(str, n, &r, radix)) return false;
- if (r > USHRT_MAX) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<unsigned short*>(dest)) = static_cast<unsigned short>(r);
- return true;
- }
- bool Arg::parse_int_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- long r;
- if (!parse_long_radix(str, n, &r, radix)) return false;
- if (r < INT_MIN || r > INT_MAX) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<int*>(dest)) = r;
- return true;
- }
- bool Arg::parse_uint_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- unsigned long r;
- if (!parse_ulong_radix(str, n, &r, radix)) return false;
- if (r > UINT_MAX) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<unsigned int*>(dest)) = r;
- return true;
- }
- bool Arg::parse_longlong_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- #ifndef HAVE_LONG_LONG
- return false;
- #else
- if (n == 0) return false;
- char buf[kMaxNumberLength+1];
- str = TerminateNumber(buf, str, n);
- char* end;
- errno = 0;
- #if defined HAVE_STRTOQ
- long long r = strtoq(str, &end, radix);
- #elif defined HAVE_STRTOLL
- long long r = strtoll(str, &end, radix);
- #elif defined HAVE__STRTOI64
- long long r = _strtoi64(str, &end, radix);
- #elif defined HAVE_STRTOIMAX
- long long r = strtoimax(str, &end, radix);
- #else
- #error parse_longlong_radix: cannot convert input to a long-long
- #endif
- if (end != str + n) return false;
- if (errno) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<long long*>(dest)) = r;
- return true;
- #endif
- }
- bool Arg::parse_ulonglong_radix(const char* str,
- int n,
- void* dest,
- int radix) {
- #ifndef HAVE_UNSIGNED_LONG_LONG
- return false;
- #else
- if (n == 0) return false;
- char buf[kMaxNumberLength+1];
- str = TerminateNumber(buf, str, n);
- if (str[0] == '-') return false;
- char* end;
- errno = 0;
- #if defined HAVE_STRTOQ
- unsigned long long r = strtouq(str, &end, radix);
- #elif defined HAVE_STRTOLL
- unsigned long long r = strtoull(str, &end, radix);
- #elif defined HAVE__STRTOI64
- unsigned long long r = _strtoui64(str, &end, radix);
- #elif defined HAVE_STRTOIMAX
- unsigned long long r = strtoumax(str, &end, radix);
- #else
- #error parse_ulonglong_radix: cannot convert input to a long-long
- #endif
- if (end != str + n) return false;
- if (errno) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<unsigned long long*>(dest)) = r;
- return true;
- #endif
- }
- bool Arg::parse_double(const char* str, int n, void* dest) {
- if (n == 0) return false;
- static const int kMaxLength = 200;
- char buf[kMaxLength];
- if (n >= kMaxLength) return false;
- memcpy(buf, str, n);
- buf[n] = '\0';
- errno = 0;
- char* end;
- double r = strtod(buf, &end);
- if (end != buf + n) return false;
- if (errno) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<double*>(dest)) = r;
- return true;
- }
- bool Arg::parse_float(const char* str, int n, void* dest) {
- double r;
- if (!parse_double(str, n, &r)) return false;
- if (dest == NULL) return true;
- *(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
- return true;
- }
- #define DEFINE_INTEGER_PARSERS(name) \
- bool Arg::parse_##name(const char* str, int n, void* dest) { \
- return parse_##name##_radix(str, n, dest, 10); \
- } \
- bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
- return parse_##name##_radix(str, n, dest, 16); \
- } \
- bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
- return parse_##name##_radix(str, n, dest, 8); \
- } \
- bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
- return parse_##name##_radix(str, n, dest, 0); \
- }
- DEFINE_INTEGER_PARSERS(short)
- DEFINE_INTEGER_PARSERS(ushort)
- DEFINE_INTEGER_PARSERS(int)
- DEFINE_INTEGER_PARSERS(uint)
- DEFINE_INTEGER_PARSERS(long)
- DEFINE_INTEGER_PARSERS(ulong)
- DEFINE_INTEGER_PARSERS(longlong)
- DEFINE_INTEGER_PARSERS(ulonglong)
- #undef DEFINE_INTEGER_PARSERS
- }
|