MySQL 9.6.0
Source Code Documentation
escaped_format.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_FORMATS_ESCAPED_FORMAT_H
25#define MYSQL_STRCONV_FORMATS_ESCAPED_FORMAT_H
26
27/// @file
28/// Experimental API header
29
30#include <array> // array
31#include <cstddef> // size_t
32#include <string_view> // string_view
33#include "mysql/strconv/formats/format.h" // Format_base
34
35/// @addtogroup GroupLibsMysqlStrconv
36/// @{
37
38namespace mysql::strconv {
39
40// ==== Helpers types to define format variants ====
41
42// NOLINTBEGIN(performance-enum-size): silence clang-tidy's pointless hint
43
44/// Whether output string should be enclosed in quote characters: "foo" vs foo.
45enum class With_quotes { no, yes };
46
47/// Whether ascii 128..255 should be preserved or escaped: backslash-xff vs
48/// ascii 255.
50
51/// Whether ascii 7..13 should use hex instead of mnemonics: backslash-x0a vs
52/// backslash-n.
54
55// NOLINTEND(performance-enum-size)
56
57// ==== Format class ====
58
59/// Format class to encode ascii strings.
60///
61/// @tparam quote_char_tp The character that surrounds a string, e.g. double
62/// quote.
63///
64/// @tparam escape_char_tp The character that begins an escape sequence, e.g.
65/// backslash.
66///
67/// @tparam preserve_high_characters_tp Whether ascii 128..255 should be
68/// preserved or escaped: backslash-xff vs ascii 255.
69///
70/// @tparam numeric_control_characters_tp Whether ascii 7..13 should use hex
71/// instead of mnemonics: backslash-x0a vs backslash-n.
72template <char quote_char_tp = '"', char escape_char_tp = '\\',
73 Preserve_high_characters preserve_high_characters_tp =
75 Numeric_control_characters numeric_control_characters_tp =
77 requires(quote_char_tp >= 32 && (unsigned)quote_char_tp < 128 &&
78 escape_char_tp >= 32 && (unsigned)escape_char_tp < 128)
80 public:
81 static constexpr char quote_char = quote_char_tp;
82 static constexpr char escape_char = escape_char_tp;
83 static constexpr Preserve_high_characters preserve_high_characters =
84 preserve_high_characters_tp;
85 static constexpr Numeric_control_characters numeric_control_characters =
86 numeric_control_characters_tp;
87
88 using Table_t = std::array<std::string_view, 256>;
89
90 /// Construct a new Format object.
91 ///
92 /// @param with_quotes Indicates whether output strings should be enclosed in
93 /// quote characters.
95 : m_with_quotes(with_quotes) {}
96
97 /// Return the conversion table for this format.
98 ///
99 /// For a character `c` between 0 and 255, element `c` is the possibly-escaped
100 /// form of character `c`, as a std::string_view.
101 static const Table_t &table() {
102 // Build the table on first invocation of this function.
103 const static auto &tbl = build_table();
104 return tbl;
105 }
106
107 /// Indicates whether output strings should be enclosed in quote characters.
109
110 private:
111 /// Construct and return the table.
112 //
113 // Todo: Make this constexpr when we have C++23.
114 static const Table_t &build_table() {
115 char hex[] = "0123456789abcdef";
116 for (int i = 0; i < 256; ++i) {
117 auto &sv = m_table[i];
118 auto &data = m_data_table[i];
119 std::size_t size;
120 if (i < 32 || (preserve_high_characters == Preserve_high_characters::no &&
121 i >= 128)) {
122 data[0] = escape_char;
123 if (numeric_control_characters == Numeric_control_characters::no &&
124 i >= '\a' && i <= '\r') {
125 // the special escapes, backslash followed by a, b, t, n, v, f, or r
126 data[1] = "abtnvfr"[i - '\a'];
127 size = 2;
128 } else {
129 // hex escapes like backslash-x01
130 data[1] = 'x';
131 data[2] = hex[i >> 4];
132 data[3] = hex[i & 0xf];
133 size = 4;
134 }
135 } else {
136 if (i == escape_char || i == quote_char) {
137 // escaped control characters: backslash-quote and backslash-backslash
138 data[0] = escape_char;
139 data[1] = i;
140 size = 2;
141 } else {
142 // un-escaped character
143 data[0] = i;
144 size = 1;
145 }
146 }
147 sv = std::string_view(data.data(), size);
148 }
149 return m_table;
150 }
151
152 /// Element 'c' is a string_view over character 'c' escaped.
153 static inline Table_t m_table;
154
155 /// Element 'c' is a character array containing the string data for
156 /// character 'c' escaped.
157 static inline std::array<std::array<char, 4>, 256> m_data_table;
158}; // class Escaped_format
159
160} // namespace mysql::strconv
161
162// addtogroup GroupLibsMysqlStrconv
163/// @}
164
165#endif // ifndef MYSQL_STRCONV_FORMATS_ESCAPED_FORMAT_H
Format class to encode ascii strings.
Definition: escaped_format.h:79
static Table_t m_table
Element 'c' is a string_view over character 'c' escaped.
Definition: escaped_format.h:153
static const Table_t & table()
Return the conversion table for this format.
Definition: escaped_format.h:101
Escaped_format(With_quotes with_quotes=With_quotes::no)
Construct a new Format object.
Definition: escaped_format.h:94
static const Table_t & build_table()
Construct and return the table.
Definition: escaped_format.h:114
static std::array< std::array< char, 4 >, 256 > m_data_table
Element 'c' is a character array containing the string data for character 'c' escaped.
Definition: escaped_format.h:157
With_quotes m_with_quotes
Indicates whether output strings should be enclosed in quote characters.
Definition: escaped_format.h:108
std::array< std::string_view, 256 > Table_t
Definition: escaped_format.h:88
Experimental API header.
void build_table(const std::string &schema, const std::string &table, const std::string &partition, bool is_tmp, bool convert, std::string &dict_name)
Definition: dict0dd.cc:7567
std::string hex(const Container &c)
Definition: hex.h:61
Definition: gtid_binary_format.h:41
Preserve_high_characters
Whether ascii 128..255 should be preserved or escaped: backslash-xff vs ascii 255.
Definition: escaped_format.h:49
Numeric_control_characters
Whether ascii 7..13 should use hex instead of mnemonics: backslash-x0a vs backslash-n.
Definition: escaped_format.h:53
With_quotes
Whether output string should be enclosed in quote characters: "foo" vs foo.
Definition: escaped_format.h:45
size_t size(const char *const c)
Definition: base64.h:46
Top of the hierarchy.
Definition: format.h:38