MySQL 9.6.0
Source Code Documentation
decode.h
Go to the documentation of this file.
1// Copyright (c) 2024, 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24#ifndef MYSQL_STRCONV_DECODE_DECODE_H
25#define MYSQL_STRCONV_DECODE_DECODE_H
26
27/// @file
28/// Experimental API header
29
30#include <new> // bad_alloc
31#include <string_view> // string_view
32#include "mysql/strconv/decode/parse_options.h" // Is_parse_options_nocheck
33#include "mysql/strconv/decode/parser.h" // Parser
34#include "mysql/strconv/decode/repeat.h" // Repeat
35#include "mysql/strconv/encode/string_target.h" // Is_string_target
36#include "mysql/strconv/formats/format.h" // Is_format
37#include "mysql/utils/concat.h" // concat
38
39/// @addtogroup GroupLibsMysqlStrconv
40/// @{
41
42namespace mysql::strconv {
43
44/// Parse from a string into the given object, according to the parse options.
45///
46/// @param opt Parse options, which should include the format, and may include
47/// a number of repetitions.
48///
49/// @param in_sv Input represented as `string_view`.
50///
51/// @param[out] out Reference to object to read into. In case of error, this may
52/// be in a "half-parsed" state.
53///
54/// @return Parser that can be queried for success and error messages.
55[[nodiscard]] Parser decode(const Is_parse_options_nocheck auto &opt,
56 const std::string_view &in_sv, auto &out) {
57 Parser parser(in_sv);
58 auto ret = parser.read(opt, out);
61 return parser;
62}
63
64/// Test for success when parsing (decoding) a *string* from a string, without
65/// producing output.
66///
67/// If this succeeds, it is guaranteed that a subsequent invocation of
68/// `decode` will not produce a parse_error. But note that it is possible
69/// for it to fail with an out-of-memory error.
70///
71/// This is an API wrapper for objects/formats for which `decode_impl(opt,
72/// pos, Is_string_target)` has been implemented.
73///
74/// @param opt Parse options, which should include the format, and may include a
75/// number of repetitions.
76///
77/// @param in_sv Input represented as `string_view`.
78///
79/// @return Parser that can be queried for success and error messages.
80[[nodiscard]] Parser test_decode(const Is_parse_options_nocheck auto &opt,
81 const std::string_view &in_sv) {
83 return decode(opt, in_sv, string_counter);
84}
85
86/// Compute the output length when parsing a *string* from a string.
87///
88/// If this succeeds, it is guaranteed that a subsequent invocation of
89/// `decode` will not produce a parse_error. But note that it is possible
90/// for it to fail with an out-of-memory error.
91///
92/// This is an API wrapper for objects/formats for which `decode_impl(opt,
93/// pos, Is_string_target)` has been implemented.
94///
95/// @param opt Parse options, which should include the format, and may include a
96/// number of repetitions.
97///
98/// @param in_sv Input represented as `string_view`.
99///
100/// @return The length, or -1 on parse error.
101[[nodiscard]] std::ptrdiff_t compute_decoded_length(
102 const Is_parse_options_nocheck auto &opt, const std::string_view &in_sv) {
104 Parser ret = decode(opt, in_sv, string_counter);
105 if (!ret.is_ok()) return -1;
106 return string_counter.size();
107}
108
109/// Parse from a string into an Is_out_str object.
110///
111/// This is an API wrapper for objects/formats for which `decode_impl(opt,
112/// pos, Is_string_target)` has been implemented.
113///
114/// @param opt Parse options, which should include the format, and may include a
115/// number of repetitions.
116///
117/// @param in_sv Input represented as `string_view`.
118///
119/// @param[out] out_str Output string wrapper to read into. In case of errors,
120/// this is unchanged.
121///
122/// @return Parser that can be queried for success and error messages.
123[[nodiscard]] Parser decode(const Is_parse_options_nocheck auto &opt,
124 const std::string_view &in_sv,
125 const Is_out_str auto &out_str) {
127 Parser parser(in_sv);
128 auto ret = parser.read_to_out_str(opt, out_str);
129 if (ret == Return_status::ok && !parser.is_sentinel())
131 return parser;
132}
133
134// ==== decode(..., std::string): string->string decoding ====
135
136/// Parse from a string into an std::string object.
137///
138/// This is an API wrapper for objects/formats for which `decode_impl(opt,
139/// pos, Is_string_target)` has been implemented.
140///
141/// @param opt Parse options, which should include the format, and may include
142/// a number of repetitions.
143///
144/// @param in_sv Input represented as `string_view`.
145///
146/// @param[out] out Reference to string to read into. In case of errors, this is
147/// unchanged.
148///
149/// @return Parser that can be queried for success and error messages.
150[[nodiscard]] Parser decode(
151 const Is_parse_options_nocheck auto &opt, const std::string_view &in_sv,
153 return decode(opt, in_sv, out_str_growable(out));
154}
155
156} // namespace mysql::strconv
157
158// addtogroup GroupLibsMysqlStrconv
159/// @}
160
161#endif // ifndef MYSQL_STRCONV_DECODE_DECODE_H
Object used to parse strings.
Definition: parser.h:69
Return_status_t read(const Is_parse_options auto &opt, Object_t &obj)
Parse into the given object.
Definition: parser.h:225
Return_status_t read_to_out_str(const Is_parse_options_nocheck auto &opt, const Is_out_str auto &out_str)
Read from this object to the given Output String Wrapper, using a decode_impl function that takes an ...
Definition: parser.h:317
std::size_t size() const
Return the current size.
Definition: string_counter.h:82
String_counter subclass that can be instantiated.
Definition: string_counter.h:97
bool is_sentinel() const
Return true if this iterator is at the end.
Definition: parse_position.h:92
void set_fullmatch_error() noexcept
Store a result representing that an object was successfully parsed from a prefix of the string,...
Definition: parse_result.h:80
bool is_ok() const
Return true if the last operation succeeded, i.e., either a full match was requested and an object wa...
Definition: parse_result.h:97
Concept used to determine at compile time whether a given type Test is a template specialization of t...
Definition: is_specialization.h:68
True if Test is an Output String Wrapper, i.e., derived from Out_str_base.
Definition: out_str.h:219
True for any kind of parse options: Format, Repeat, Checker, or Compound_parse_options.
Definition: parse_options.h:133
Experimental API header.
struct Parser parser
Definition: gtid_binary_format.h:41
std::ptrdiff_t compute_decoded_length(const Is_parse_options_nocheck auto &opt, const std::string_view &in_sv)
Compute the output length when parsing a string from a string.
Definition: decode.h:101
Parser decode(const Is_parse_options_nocheck auto &opt, const std::string_view &in_sv, auto &out)
Parse from a string into the given object, according to the parse options.
Definition: decode.h:55
auto out_str_growable(String_t &str)
Return a wrapper around a growable output buffer, represented as a std::string or similar type.
Definition: out_str.h:985
Parser test_decode(const Is_parse_options_nocheck auto &opt, const std::string_view &in_sv)
Test for success when parsing (decoding) a string from a string, without producing output.
Definition: decode.h:80
Return_status
Simple, strongly-typed enumeration to indicate internal status: ok, error.
Definition: return_status.h:40
@ ok
operation succeeded
Experimental API header.
Experimental API header.
Experimental API header.
Experimental API header.
Convenience function that concatenates arbitrary arguments, by feeding them to an ostringstream.