]> git.ipfire.org Git - thirdparty/squid.git/blame - src/parser/Tokenizer.h
Maintenance: bump astyle to 2.04 and quieten report
[thirdparty/squid.git] / src / parser / Tokenizer.h
CommitLineData
bbc27441
AJ
1/*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
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.
7 */
8
c9a4e310
FC
9#ifndef SQUID_PARSER_TOKENIZER_H_
10#define SQUID_PARSER_TOKENIZER_H_
11
12#include "base/CharacterSet.h"
13#include "SBuf.h"
14
90bba30a 15/// Generic protocol-agnostic parsing tools
11bd4370
A
16namespace Parser
17{
c9a4e310 18
0351d153
AJ
19/**
20 * Lexical processor to tokenize a buffer.
21 *
5d811da5
AJ
22 * Allows arbitrary delimiters and token character sets to
23 * be provided by callers.
24 *
25 * All methods start from the beginning of the input buffer.
26 * Methods returning true consume bytes from the buffer.
27 * Methods returning false have no side-effects.
0351d153 28 */
11bd4370
A
29class Tokenizer
30{
c9a4e310 31public:
0c67864a 32 explicit Tokenizer(const SBuf &inBuf) : buf_(inBuf), parsed_(0) {}
11bd4370 33
0c67864a
AR
34 /// yet unparsed data
35 SBuf buf() const { return buf_; }
36
37 /// number of parsed bytes, including skipped ones
38 SBuf::size_type parsedSize() const { return parsed_; }
11bd4370
A
39
40 /// whether the end of the buffer has been reached
41 bool atEnd() const { return buf_.isEmpty(); }
42
43 /// the remaining unprocessed section of buffer
44 const SBuf& remaining() const { return buf_; }
45
46 /// reinitialize processing for a new buffer
0c67864a 47 void reset(const SBuf &newBuf) { buf_ = newBuf; parsed_ = 0; }
11bd4370
A
48
49 /** Basic strtok(3):
50 * Skips all leading delimiters (if any),
0c67864a
AR
51 * extracts all characters up to the next delimiter (a token), and
52 * skips all trailing delimiters (at least one must be present).
11bd4370
A
53 *
54 * Want to extract delimiters? Use prefix() instead.
55 *
0c67864a
AR
56 * Note that Tokenizer cannot tell whether the trailing delimiters will
57 * continue when/if more input data becomes available later.
11bd4370 58 *
0c67864a 59 * \return true if found a non-empty token followed by a delimiter
11bd4370
A
60 */
61 bool token(SBuf &returnedToken, const CharacterSet &delimiters);
62
0c67864a
AR
63 /** Extracts all sequential permitted characters up to an optional length limit.
64 *
65 * Note that Tokenizer cannot tell whether the prefix will
66 * continue when/if more input data becomes available later.
11bd4370
A
67 *
68 * \retval true one or more characters were found, the sequence (string) is placed in returnedToken
69 * \retval false no characters from the permitted set were found
70 */
71 bool prefix(SBuf &returnedToken, const CharacterSet &tokenChars, SBuf::size_type limit = SBuf::npos);
72
11bd4370
A
73 /** skips a given character sequence (string)
74 *
75 * \return whether the exact character sequence was found and skipped
76 */
77 bool skip(const SBuf &tokenToSkip);
78
79 /** skips a given single character
80 *
0c67864a 81 * \return whether the character was skipped
11bd4370
A
82 */
83 bool skip(const char tokenChar);
84
0c67864a
AR
85 /** Skips a single character from the set.
86 *
87 * \return whether a character was skipped
88 */
89 bool skipOne(const CharacterSet &discardables);
90
91 /** Skips all sequential characters from the set, in any order.
92 *
93 * \returns the number of skipped characters
94 */
95 SBuf::size_type skipAll(const CharacterSet &discardables);
96
97 /** Extracts an unsigned int64_t at the beginning of the buffer.
11bd4370
A
98 *
99 * strtoll(3)-alike function: tries to parse unsigned 64-bit integer
100 * at the beginning of the parse buffer, in the base specified by the user
101 * or guesstimated; consumes the parsed characters.
102 *
103 * \param result Output value. Not touched if parsing is unsuccessful.
104 * \param base Specify base to do the parsing in, with the same restrictions
105 * as strtoll. Defaults to 0 (meaning guess)
106 *
107 * \return whether the parsing was successful
108 */
109 bool int64(int64_t &result, int base = 0);
957143e6 110
0c67864a
AR
111protected:
112 SBuf consume(const SBuf::size_type n);
113 SBuf::size_type success(const SBuf::size_type n);
114
c9a4e310 115private:
11bd4370 116 SBuf buf_; ///< yet unparsed input
0c67864a 117 SBuf::size_type parsed_; ///< bytes successfully parsed, including skipped
c9a4e310
FC
118};
119
c9a4e310 120} /* namespace Parser */
0351d153 121
c9a4e310 122#endif /* SQUID_PARSER_TOKENIZER_H_ */