MySQL 9.6.0
Source Code Documentation
encode_text.h
Go to the documentation of this file.
1// Copyright (c) 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_ENCODE_ENCODE_TEXT_H
25#define MYSQL_STRCONV_ENCODE_ENCODE_TEXT_H
26
27/// @file
28/// Experimental API header
29
30#include "mysql/strconv/encode/concat.h" // concat
31#include "mysql/strconv/encode/encode.h" // compute_encoded_length
32#include "mysql/strconv/formats/text_format.h" // Text_format
33
34/// @addtogroup GroupLibsMysqlStrconv
35/// @{
36
37namespace mysql::strconv {
38
39/// Return the string length of the object, using Text_format.
40///
41/// @note If you plan to allocate memory for a null-terminated string, remember
42/// to add 1 byte for the trailing '\0'.
43template <class Object_t>
44[[nodiscard]] std::size_t compute_encoded_length_text(const Object_t &object) {
45 return compute_encoded_length(Text_format{}, object);
46}
47
48/// Write the string representation of the object to the given string output
49/// wrapper, using Text_format.
50///
51/// This overload is for the case that the output wrapper's resize policy is
52/// `fixed`, i.e., the caller guarantees that the output buffer has enough space
53/// to store the output. Therefore, this function cannot fail and does not have
54/// the `[[nodiscard]]` attribute.
55///
56/// @tparam Out_str_t Type of output string wrapper to write to.
57///
58/// @tparam Object_t Type of object.
59///
60/// param[in,out] out_str Output string wrapper to write to.
61///
62/// param object Object to write.
63///
64/// @return Return_status::ok on success, Return_status::error on allocation
65/// failure.
66// Doxygen complains that the parameters are duplicated (maybe mixing up
67// overloads with different constraints). We work around that by using param
68// instead of @param.
69template <Is_out_str_fixed Out_str_t, class Object_t>
70auto encode_text(Out_str_t out_str, const Object_t &object) {
71 return encode(Text_format{}, out_str, object);
72}
73
74/// Write the string representation of the object to the given string output
75/// wrapper, using Text_format.
76///
77/// This overload is for the case that the output wrapper's resize policy is
78/// `growable`. Therefore, this function can fail to allocate memory and has the
79/// `[[nodiscard]]` attribute.
80///
81/// @tparam Out_str_t Output string wrapper to write to.
82///
83/// @tparam Object_t Type of object.
84///
85/// @param[in,out] out_str Output string wrapper to write to.
86///
87/// @param object Object to write.
88///
89/// @return Return_status::ok on success, Return_status::error on allocation
90/// failure.
91template <Is_out_str_growable Out_str_t, class Object_t>
92[[nodiscard]] auto encode_text(Out_str_t out_str, const Object_t &object) {
93 return encode(Text_format{}, out_str, object);
94}
95
96namespace throwing {
97
98/// Return an std::string object holding the string representation of the
99/// object, using Text_format.
100///
101/// @tparam String_t Output string type. Defaults to std::string.
102///
103/// @tparam Object_t Type of object.
104///
105/// @param object Object to write.
106///
107/// @return String representation of object.
108///
109/// @throws bad_alloc if an out-of-memory condition occurs.
110template <class String_t = std::string, class Object_t>
111[[nodiscard]] String_t encode_text(const Object_t &object) {
112 return mysql::strconv::throwing::encode<String_t>(Text_format{}, object);
113}
114
115/// Return an std::string object holding the concatenated string representations
116/// of the objects, using Text_format.
117///
118/// @tparam String_t Output string type. Defaults to std::string.
119///
120/// @tparam Objects_t Types of objects.
121///
122/// @param objects Objects to write.
123///
124/// @return Concatenated string representations of objects.
125///
126/// @throws bad_alloc if an out-of-memory condition occurs.
127template <class String_t = std::string, class... Objects_t>
128[[nodiscard]] String_t concat_text(const Objects_t &...objects) {
129 return mysql::strconv::throwing::concat<String_t>(Text_format{}, objects...);
130}
131
132} // namespace throwing
133
134/// Return an std::optional<std::string> object holding the string
135/// representation of the object, using Text_format.
136///
137/// @tparam String_t Output string type. Defaults to std::string.
138///
139/// @tparam Object_t Type of object.
140///
141/// @param object Object to write.
142///
143/// @returns std::optional<std::string> object holding the string representation
144/// of @c object, or holding no value if an out-of-memory condition occurred.
145template <class String_t = std::string, class Object_t>
146[[nodiscard]] auto encode_text(const Object_t &object) {
147 return encode<String_t>(Text_format{}, object);
148}
149
150/// Return an std::optional<std::string> object holding the concatenated string
151/// representations of the objects, using Text_format.
152///
153/// @tparam String_t Output string type. Defaults to std::string.
154///
155/// @tparam Objects_t Types of objects.
156///
157/// @param objects Objects to write.
158///
159/// @returns std::optional<std::string> object holding the concatenated string
160/// representations of @c objects, or holding no value if an out-of-memory
161/// condition occurred.
162template <class String_t = std::string, class... Objects_t>
163[[nodiscard]] auto concat_text(const Objects_t &...objects) {
164 return concat<String_t>(Text_format{}, objects...);
165}
166
167} // namespace mysql::strconv
168
169// addtogroup GroupLibsMysqlStrconv
170/// @}
171
172#endif // ifndef MYSQL_STRCONV_ENCODE_ENCODE_TEXT_H
String_t encode_text(const Object_t &object)
Return an std::string object holding the string representation of the object, using Text_format.
Definition: encode_text.h:111
String_t concat_text(const Objects_t &...objects)
Return an std::string object holding the concatenated string representations of the objects,...
Definition: encode_text.h:128
Definition: gtid_binary_format.h:41
std::size_t compute_encoded_length_text(const Object_t &object)
Return the string length of the object, using Text_format.
Definition: encode_text.h:44
auto concat_text(const Objects_t &...objects)
Return an std::optional<std::string> object holding the concatenated string representations of the ob...
Definition: encode_text.h:163
void encode(const Debug_format &format, Is_string_target auto &target, const Repeat &repeat)
Definition: debug_repeat.h:39
auto encode_text(Out_str_t out_str, const Object_t &object)
Write the string representation of the object to the given string output wrapper, using Text_format.
Definition: encode_text.h:70
std::size_t compute_encoded_length(const Is_format auto &format, const auto &object)
Return the string length of the object.
Definition: encode.h:57
Experimental API header.
Experimental API header.
Format tag to identify text format.
Definition: text_format.h:38
Experimental API header.