From: Francesco Chemolli Date: Thu, 22 May 2014 20:37:12 +0000 (+0200) Subject: Interim: start implementing Parser::Tokenizer::int64 X-Git-Tag: SQUID_3_5_0_1~217^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=957143e60bfea4efa17a1e3aa7f5c3659f1e3a41;p=thirdparty%2Fsquid.git Interim: start implementing Parser::Tokenizer::int64 --- diff --git a/src/parser/Tokenizer.cc b/src/parser/Tokenizer.cc index 9be59279cc..469d0b62c6 100644 --- a/src/parser/Tokenizer.cc +++ b/src/parser/Tokenizer.cc @@ -55,3 +55,41 @@ Parser::Tokenizer::skip(const char tokenChar) } return false; } + +// adapted from compatr/strtoll.c +bool +Parser::Tokenizer::int64 (int64_t & result, int base) +{ + //register uint64_t acc; + register uint64_t cutoff; + bool neg = false; + static SBuf zerox("0x"), zero("0"); + + if (buf_.isEmpty()) + return false; + + if (buf_[0] == '-') { + neg = true; + buf_.consume(1); + } + if (buf_[0] == '+') + buf_.consume(1); + if (base == 0) { + if (buf_.startsWith(zerox)) + base = 16; + else if (buf_.startsWith(zero)) + base = 8; + else + base = 10; + } + if (base != 8 && base != 10 && base != 16) + return false; + + // TODO: finish + cutoff = neg ? -(uint64_t) INT64_MIN : INT64_MAX; + + // dummy to keep compiler happy. Remove before continuing + if (neg) result = cutoff; + + return false; +} diff --git a/src/parser/Tokenizer.h b/src/parser/Tokenizer.h index a1dbdefb2d..0296a7221d 100644 --- a/src/parser/Tokenizer.h +++ b/src/parser/Tokenizer.h @@ -64,6 +64,22 @@ public: */ bool skip(const char tokenChar); + /** parse an unsigned int64_t at the beginning of the buffer + * + * strtoull(3)-alike function: tries to parse unsigned 64-bit integer + * at the beginning of the parse buffer, in the base specified by the user + * or guesstimated; consumes the parsed characters. + * + * \param result output value. Not touched if parseing is unsuccessful + * \param base specify base to do the parsing in. Admitted values are + * 8, 10, 16 and 0, the latter specifying to use C syntax for guessing + * the base + * \return true if the parsing was successful + */ + bool uint64 (uint64_t & result, int base); + + bool int64 (int64_t &result, int base); + private: SBuf buf_; ///< yet unparsed input };