]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/parser/Tokenizer.cc
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 24 SBuf */
13 #include "parser/Tokenizer.h"
20 /// convenience method: consumes up to n bytes, counts, and returns them
22 Parser::Tokenizer::consume(const SBuf::size_type n
)
24 // careful: n may be npos!
25 debugs(24, 5, "consuming " << n
<< " bytes");
26 const SBuf result
= buf_
.consume(n
);
27 parsed_
+= result
.length();
31 /// convenience method: consume()s up to n bytes and returns their count
33 Parser::Tokenizer::success(const SBuf::size_type n
)
35 return consume(n
).length();
38 /// convenience method: consumes up to n last bytes and returns them
40 Parser::Tokenizer::consumeTrailing(const SBuf::size_type n
)
42 debugs(24, 5, "consuming " << n
<< " bytes");
44 // If n is npos, we consume everything from buf_ (and nothing from result).
45 const SBuf::size_type parsed
= (n
== SBuf::npos
) ? buf_
.length() : n
;
48 buf_
= result
.consume(buf_
.length() - parsed
);
53 /// convenience method: consumes up to n last bytes and returns their count
55 Parser::Tokenizer::successTrailing(const SBuf::size_type n
)
57 return consumeTrailing(n
).length();
61 Parser::Tokenizer::token(SBuf
&returnedToken
, const CharacterSet
&delimiters
)
63 const Tokenizer
saved(*this);
65 const SBuf::size_type tokenLen
= buf_
.findFirstOf(delimiters
); // not found = npos => consume to end
66 if (tokenLen
== SBuf::npos
) {
67 debugs(24, 8, "no token found for delimiters " << delimiters
.name
);
71 returnedToken
= consume(tokenLen
); // cannot be empty
73 debugs(24, DBG_DATA
, "token found for delimiters " << delimiters
.name
<< ": '" <<
74 returnedToken
<< '\'');
79 Parser::Tokenizer::prefix(SBuf
&returnedToken
, const CharacterSet
&tokenChars
, const SBuf::size_type limit
)
81 SBuf::size_type prefixLen
= buf_
.substr(0,limit
).findFirstNotOf(tokenChars
);
83 debugs(24, 8, "no prefix for set " << tokenChars
.name
);
86 if (prefixLen
== SBuf::npos
&& (atEnd() || limit
== 0)) {
87 debugs(24, 8, "no char in set " << tokenChars
.name
<< " while looking for prefix");
90 if (prefixLen
== SBuf::npos
&& limit
> 0) {
91 debugs(24, 8, "whole haystack matched");
94 debugs(24, 8, "found with length " << prefixLen
);
95 returnedToken
= consume(prefixLen
); // cannot be empty after the npos check
100 Parser::Tokenizer::suffix(SBuf
&returnedToken
, const CharacterSet
&tokenChars
, const SBuf::size_type limit
)
104 if (limit
< buf_
.length())
105 span
.consume(buf_
.length() - limit
); // ignore the N prefix characters
107 auto i
= span
.rbegin();
108 SBuf::size_type found
= 0;
109 while (i
!= span
.rend() && tokenChars
[*i
]) {
115 returnedToken
= consumeTrailing(found
);
120 Parser::Tokenizer::skipAll(const CharacterSet
&tokenChars
)
122 const SBuf::size_type prefixLen
= buf_
.findFirstNotOf(tokenChars
);
123 if (prefixLen
== 0) {
124 debugs(24, 8, "no match when trying to skipAll " << tokenChars
.name
);
127 debugs(24, 8, "skipping all in " << tokenChars
.name
<< " len " << prefixLen
);
128 return success(prefixLen
);
132 Parser::Tokenizer::skipOne(const CharacterSet
&chars
)
134 if (!buf_
.isEmpty() && chars
[buf_
[0]]) {
135 debugs(24, 8, "skipping one-of " << chars
.name
);
138 debugs(24, 8, "no match while skipping one-of " << chars
.name
);
143 Parser::Tokenizer::skipSuffix(const SBuf
&tokenToSkip
)
145 if (buf_
.length() < tokenToSkip
.length())
148 SBuf::size_type offset
= 0;
149 if (tokenToSkip
.length() < buf_
.length())
150 offset
= buf_
.length() - tokenToSkip
.length();
152 if (buf_
.substr(offset
, SBuf::npos
).cmp(tokenToSkip
) == 0) {
153 debugs(24, 8, "skipping " << tokenToSkip
.length());
154 return successTrailing(tokenToSkip
.length());
160 Parser::Tokenizer::skip(const SBuf
&tokenToSkip
)
162 if (buf_
.startsWith(tokenToSkip
)) {
163 debugs(24, 8, "skipping " << tokenToSkip
.length());
164 return success(tokenToSkip
.length());
166 debugs(24, 8, "no match, not skipping '" << tokenToSkip
<< '\'');
171 Parser::Tokenizer::skip(const char tokenChar
)
173 if (!buf_
.isEmpty() && buf_
[0] == tokenChar
) {
174 debugs(24, 8, "skipping char '" << tokenChar
<< '\'');
177 debugs(24, 8, "no match, not skipping char '" << tokenChar
<< '\'');
182 Parser::Tokenizer::skipOneTrailing(const CharacterSet
&skippable
)
184 if (!buf_
.isEmpty() && skippable
[buf_
[buf_
.length()-1]]) {
185 debugs(24, 8, "skipping one-of " << skippable
.name
);
186 return successTrailing(1);
188 debugs(24, 8, "no match while skipping one-of " << skippable
.name
);
193 Parser::Tokenizer::skipAllTrailing(const CharacterSet
&skippable
)
195 const SBuf::size_type prefixEnd
= buf_
.findLastNotOf(skippable
);
196 const SBuf::size_type prefixLen
= prefixEnd
== SBuf::npos
?
198 const SBuf::size_type suffixLen
= buf_
.length() - prefixLen
;
199 if (suffixLen
== 0) {
200 debugs(24, 8, "no match when trying to skip " << skippable
.name
);
203 debugs(24, 8, "skipping in " << skippable
.name
<< " len " << suffixLen
);
204 return successTrailing(suffixLen
);
207 /* reworked from compat/strtoll.c */
209 Parser::Tokenizer::int64(int64_t & result
, int base
, bool allowSign
, const SBuf::size_type limit
)
211 if (atEnd() || limit
== 0)
214 const SBuf
range(buf_
.substr(0,limit
));
216 //fixme: account for buf_.size()
218 const char *s
= range
.rawContent();
219 const char *end
= range
.rawContent() + range
.length();
225 } else if (*s
== '+') {
228 if (s
>= end
) return false;
230 if (( base
== 0 || base
== 16) && *s
== '0' && (s
+1 <= end
) &&
231 tolower(*(s
+1)) == 'x') {
243 if (s
>= end
) return false;
247 cutoff
= neg
? -static_cast<uint64_t>(INT64_MIN
) : INT64_MAX
;
248 const int cutlim
= cutoff
% static_cast<int64_t>(base
);
249 cutoff
/= static_cast<uint64_t>(base
);
253 for (c
= *s
++; s
<= end
; c
= *s
++) {
256 } else if (xisalpha(c
)) {
257 c
-= xisupper(c
) ? 'A' - 10 : 'a' - 10;
263 if (any
< 0 || static_cast<uint64_t>(acc
) > cutoff
|| (static_cast<uint64_t>(acc
) == cutoff
&& c
> cutlim
))
272 if (any
== 0) // nothing was parsed
275 acc
= neg
? INT64_MIN
: INT64_MAX
;
282 return success(s
- range
.rawContent() - 1);