]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/parser/Tokenizer.cc
2 #include "parser/Tokenizer.h"
12 /* Native 64 bit system without strtoll() */
13 #if defined(LONG_MIN) && (SIZEOF_LONG == 8)
14 #define INT64_MIN LONG_MIN
17 #define INT64_MIN (-9223372036854775807LL-1LL)
22 /* Native 64 bit system without strtoll() */
23 #if defined(LONG_MAX) && (SIZEOF_LONG == 8)
24 #define INT64_MAX LONG_MAX
27 #define INT64_MAX 9223372036854775807LL
32 Parser::Tokenizer::token(SBuf
&returnedToken
, const CharacterSet
&delimiters
)
34 const SBuf
savebuf(buf_
);
36 const SBuf::size_type tokenLen
= buf_
.findFirstOf(delimiters
); // not found = npos => consume to end
37 if (tokenLen
== SBuf::npos
&& !delimiters
['\0']) {
38 // no delimiter found, nor is NUL/EOS/npos acceptible as one
42 const SBuf retval
= buf_
.consume(tokenLen
);
44 returnedToken
= retval
;
49 Parser::Tokenizer::prefix(SBuf
&returnedToken
, const CharacterSet
&tokenChars
, const SBuf::size_type limit
)
51 const SBuf::size_type prefixLen
= buf_
.substr(0,limit
).findFirstNotOf(tokenChars
);
54 returnedToken
= buf_
.consume(prefixLen
);
59 Parser::Tokenizer::skip(const CharacterSet
&tokenChars
)
61 const SBuf::size_type prefixLen
= buf_
.findFirstNotOf(tokenChars
);
64 buf_
.consume(prefixLen
);
69 Parser::Tokenizer::skip(const SBuf
&tokenToSkip
)
71 if (buf_
.startsWith(tokenToSkip
)) {
72 buf_
.consume(tokenToSkip
.length());
79 Parser::Tokenizer::skip(const char tokenChar
)
81 if (buf_
[0] == tokenChar
) {
88 /* reworked from compat/strtoll.c */
90 Parser::Tokenizer::int64(int64_t & result
, int base
)
95 //fixme: account for buf_.size()
97 const char *s
= buf_
.rawContent();
98 const char *end
= buf_
.rawContent() + buf_
.length();
103 } else if (*s
== '+') {
106 if (s
>= end
) return false;
107 if (( base
== 0 || base
== 16) && *s
== '0' && (s
+1 <= end
) &&
108 tolower(*(s
+1)) == 'x') {
120 if (s
>= end
) return false;
124 cutoff
= neg
? -static_cast<uint64_t>(INT64_MIN
) : INT64_MAX
;
125 const int cutlim
= cutoff
% static_cast<int64_t>(base
);
126 cutoff
/= static_cast<uint64_t>(base
);
130 for (c
= *s
++; s
<= end
; c
= *s
++) {
133 } else if (xisalpha(c
)) {
134 c
-= xisupper(c
) ? 'A' - 10 : 'a' - 10;
140 if (any
< 0 || static_cast<uint64_t>(acc
) > cutoff
|| (static_cast<uint64_t>(acc
) == cutoff
&& c
> cutlim
))
149 if (any
== 0) // nothing was parsed
152 acc
= neg
? INT64_MIN
: INT64_MAX
;
159 buf_
.consume(s
- buf_
.rawContent() -1);