bool
Parser::Tokenizer::prefix(SBuf &returnedToken, const CharacterSet &tokenChars, const SBuf::size_type limit)
{
- const SBuf::size_type prefixLen = buf_.substr(0,limit).findFirstNotOf(tokenChars);
+ SBuf::size_type prefixLen = buf_.substr(0,limit).findFirstNotOf(tokenChars);
if (prefixLen == 0)
return false;
if (prefixLen == SBuf::npos && (atEnd() || limit == 0))
return false;
+ if (prefixLen == SBuf::npos && limit > 0)
+ prefixLen = limit;
returnedToken = consume(prefixLen); // cannot be empty after the npos check
return true;
}
/* reworked from compat/strtoll.c */
bool
-Parser::Tokenizer::int64(int64_t & result, int base)
+Parser::Tokenizer::int64(int64_t & result, int base, bool allowSign, const SBuf::size_type limit)
{
- if (buf_.isEmpty())
+ if (atEnd() || limit == 0)
return false;
+ const SBuf range(buf_.substr(0,limit));
+
//fixme: account for buf_.size()
bool neg = false;
- const char *s = buf_.rawContent();
- const char *end = buf_.rawContent() + buf_.length();
-
- if (*s == '-') {
- neg = true;
- ++s;
- } else if (*s == '+') {
- ++s;
+ const char *s = range.rawContent();
+ const char *end = range.rawContent() + range.length();
+
+ if (allowSign) {
+ if (*s == '-') {
+ neg = true;
+ ++s;
+ } else if (*s == '+') {
+ ++s;
+ }
+ if (s >= end) return false;
}
- if (s >= end) return false;
if (( base == 0 || base == 16) && *s == '0' && (s+1 <= end ) &&
tolower(*(s+1)) == 'x') {
s += 2;
acc = -acc;
result = acc;
- return success(s - buf_.rawContent() - 1);
+ return success(s - range.rawContent() - 1);
}
* \param result Output value. Not touched if parsing is unsuccessful.
* \param base Specify base to do the parsing in, with the same restrictions
* as strtoll. Defaults to 0 (meaning guess)
+ * \param allowSign Whether to accept a '+' or '-' sign prefix.
+ * \param limit Maximum count of characters to convert.
*
* \return whether the parsing was successful
*/
- bool int64(int64_t &result, int base = 0);
+ bool int64(int64_t &result, int base = 0, bool allowSign = true, SBuf::size_type limit = SBuf::npos);
protected:
SBuf consume(const SBuf::size_type n);