userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
tskv_parser.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/encoding/tskv_parser.hpp
4
/// @brief @copybrief utils::encoding::TskvParser
5
6
#
include
<
cstddef
>
7
#
include
<
optional
>
8
#
include
<
string
>
9
#
include
<
string_view
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
utils
::
encoding
{
14
15
/// @brief A streaming parser for the TSKV variant that userver emits.
16
///
17
/// **Supported syntax**
18
/// 1. Records are separated by a single non-escaped `\n` character
19
/// 2. A record consists of entries separated by a single `\t` character
20
/// 3. The first entry SHOULD be verbatim `tskv`
21
/// 4. Entries MAY have values, separated by the first non-escaped `=`
22
/// 5. An entry MAY have no key-value split, in which case it all counts as key
23
///
24
/// **Escaping**
25
/// 1. `\n`, `\t`, `\\` in keys or values SHOULD be escaped as
26
/// `\\\n`, `\\\t`, `\\\\` sequences
27
/// 2. `=` SHOULD be escaped as `\\=` in keys, MAY be escaped in values
28
/// 3. `\r` and `\0` MAY be escaped as `\\\r` and `\\\0` sequences
29
///
30
/// **Parsing process**
31
/// Initialize `TskvParser` with a caller-owned string that may contain a single
32
/// or multiple TSKV records. For each record, first call `SkipToRecordBegin`.
33
/// Then call `ReadKey` and `ReadValue` a bunch of times.
34
///
35
/// For a simpler way to read TSKV records, see utils::encoding::TskvReadRecord.
36
class
TskvParser
final
{
37
public
:
38
/// The status is returned once the record is complete. We ignore invalid
39
/// escaping, not reporting it as an error, trying to push through.
40
/// `nullopt` status means we should keep reading the current TSKV record.
41
enum
class
[[nodiscard]]
RecordStatus
{
42
kReachedEnd
,
///< successfully read the whole record
43
kIncomplete
,
///< the record ends abruptly, the service is probably
44
///< still writing the record
45
};
46
47
explicit
TskvParser(std::string_view in)
noexcept
;
48
49
/// @brief Skips the current record or optional invalid junk until it finds
50
/// the start of the a valid record.
51
/// @returns pointer to the start of the next record if there is one,
52
/// `nullptr` otherwise
53
/// @note `tskv\n` records are currently not supported
54
const
char
*
SkipToRecordBegin
()
noexcept
;
55
56
/// @brief Skips to the end of the current record, which might not be the same
57
/// as the start of the next valid record.
58
/// @note RecordStatus::kReachedEnd SHOULD
59
/// not have been returned for the current record, otherwise the behavior is
60
/// unspecified.
61
/// @note `tskv\n` records are currently not supported
62
RecordStatus
SkipToRecordEnd
()
noexcept
;
63
64
/// @brief Parses the key, replacing `result` with it.
65
/// @throws std::bad_alloc only if `std::string` allocation throws
66
/// @note RecordStatus::kReachedEnd is returned specifically
67
/// on a trailing `\t\n` sequence
68
std::optional<
RecordStatus
>
ReadKey
(std::string& result);
69
70
/// @brief Parses the value, replacing `result` with it.
71
/// @throws std::bad_alloc only if `std::string` allocation throws
72
std::optional<
RecordStatus
>
ReadValue
(std::string& result);
73
74
/// @brief Parses a value without copying it when it contains no escapes.
75
/// @param result view of the parsed value; empty on an incomplete value
76
/// @param storage scratch buffer used only when decoding escapes
77
/// @note The view borrows either the parser's input or `storage`. The
78
/// corresponding buffer must remain alive and unchanged while using the
79
/// view. Reusing `storage` for another escaped value invalidates its views.
80
/// Copy the value to a string if it must outlive the buffer.
81
/// @pre `storage` does not overlap the parser's input.
82
/// @note Uses the same escape, EOF and stream-position rules as
83
/// @ref utils::encoding::TskvParser::ReadValue.
84
/// @throws std::bad_alloc only if scratch buffer allocation throws
85
std::optional<
RecordStatus
>
ReadValueView
(std::string_view& result, std::string& storage);
86
87
/// @brief Skips one value with the same escape and EOF handling as
88
/// @ref utils::encoding::TskvParser::ReadValue, without materializing it.
89
/// @note Does not allocate memory.
90
std::optional<
RecordStatus
>
SkipValue
()
noexcept
;
91
92
/// @returns pointer to the data that will be read by the next operation
93
const
char
*
GetStreamPosition
()
const
noexcept
;
94
95
private
:
96
std::optional<
RecordStatus
> ReadValueFrom(std::string& result, std::size_t value_end);
97
98
std::string_view in_;
99
};
100
101
}
// namespace utils::encoding
102
103
USERVER_NAMESPACE_END
userver
utils
encoding
tskv_parser.hpp
Generated on
for userver by
Doxygen
1.17.0