MySQL 9.6.0
Source Code Documentation
parse_position.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_PARSE_POSITION_H
25#define MYSQL_STRCONV_DECODE_PARSE_POSITION_H
26
27/// @file
28/// Experimental API header
29
30#include <cassert> // assert
31#include <cstring> // strlen
32#include <iterator> // contiguous_iterator_tag
33#include <string_view> // string_view
34#include "mysql/iterators/iterator_interface.h" // Iterator_interface
35#include "mysql/utils/char_cast.h" // uchar_cast
36
37/// @addtogroup GroupLibsMysqlStrconv
38/// @{
39
40namespace mysql::strconv::detail {
41
42/// Base class for the current position of a string parser, holding both the
43/// parsed string and the position within the parsed string. Subclasses are
44/// contiguous iterators over the characters in the parsed string.
45///
46/// @tparam Self_tp The subclass inheriting from this class.
47template <class Self_tp>
49 public:
50 /// Construct a new Parse_position from the given range.
51 ///
52 /// @param source Source string.
53 ///
54 /// @param int_pos Current position. Defaults to 0, i.e., the beginning.
55 explicit Parse_position(const std::string_view &source,
56 std::size_t int_pos = 0)
58 assert(int_pos <= source.size());
59 }
60
61 // ==== Iterator implementation ====
62 //
63 // Functions required by Iterator_interface to implement a contiguous iterator
64 // with a sentinel type.
65
66 using Iterator_category_t = std::contiguous_iterator_tag;
67
68 /// Construct a new object. The object cannot be used for anything besides
69 /// assigning another object to it.
70 Parse_position() = default;
71
72 /// Dereference the iterator and return the value.
73 [[nodiscard]] const char *get_pointer() const {
74 assert(int_pos() < m_source.size());
75 return pos();
76 }
77
78 /// Move the iterator delta steps.
79 void advance(std::ptrdiff_t delta) {
80 m_int_pos += delta;
81 assert(int_pos() <= m_source.size());
82 }
83
84 /// Return the distance from iterator other to this.
85 [[nodiscard]] std::ptrdiff_t distance_from(
86 const Parse_position &other) const {
87 assert(other.m_source == other.m_source);
88 return m_int_pos - other.m_int_pos;
89 }
90
91 /// Return true if this iterator is at the end.
92 [[nodiscard]] bool is_sentinel() const {
93 return m_int_pos == m_source.size();
94 }
95
96 // ==== Absolute position ====
97 //
98 // Access to the iterator, relative to the beginning of the string.
99
100 /// Set the position to the given one.
101 void set_int_pos(std::size_t int_pos_arg) {
102 assert(int_pos_arg <= str_size());
103 m_int_pos = int_pos_arg;
104 }
105
106 /// Return the current position as an integer.
107 [[nodiscard]] std::size_t int_pos() const { return m_int_pos; }
108
109 /// Return the current position as a char pointer.
110 [[nodiscard]] const char *pos() const { return begin() + int_pos(); }
111
112 /// Return the current position as an unsigned char pointer.
113 [[nodiscard]] const unsigned char *upos() const {
115 }
116
117 /// Return the current position as an std::byte pointer.
118 [[nodiscard]] const std::byte *bpos() const {
120 }
121
122 // ==== View over parsed string ====
123
124 /// Return pointer to the beginning of the underlying string.
125 [[nodiscard]] const char *begin() const { return m_source.data(); }
126
127 /// Return pointer to the beginning of the underlying string.
128 [[nodiscard]] const unsigned char *ubegin() const {
129 return mysql::utils::uchar_cast(m_source.data());
130 }
131
132 /// Return pointer to the beginning of the underlying string.
133 [[nodiscard]] const std::byte *bbegin() const {
134 return mysql::utils::byte_cast(m_source.data());
135 }
136
137 /// Return pointer to the end of the underlying string.
138 [[nodiscard]] const char *end() const {
139 return m_source.data() + m_source.size();
140 }
141
142 /// Return pointer to the end of the underlying string.
143 [[nodiscard]] const unsigned char *uend() const {
144 return ubegin() + m_source.size();
145 }
146
147 /// Return pointer to the end of the underlying string.
148 [[nodiscard]] const std::byte *bend() const {
149 return bbegin() + m_source.size();
150 }
151
152 /// Return the remaining size.
153 [[nodiscard]] std::size_t remaining_size() const { return end() - pos(); }
154
155 /// Return the length of the underlying string.
156 [[nodiscard]] std::size_t str_size() const { return m_source.size(); }
157
158 /// Return a string_view over the left part of the string, up to the position.
159 [[nodiscard]] std::string_view parsed_str() const { return {begin(), pos()}; }
160
161 /// Return a string_view over the remaining string.
162 [[nodiscard]] std::string_view remaining_str() const {
163 return {pos(), end()};
164 }
165
166 /// Return a string_view over the underlying string.
167 [[nodiscard]] std::string_view str() const { return m_source; }
168
169 private:
170 /// The beginning of the range.
171 std::string_view m_source{};
172
173 /// The current position.
174 std::size_t m_int_pos{};
175}; // class Parse_position
176
177} // namespace mysql::strconv::detail
178
179// addtogroup GroupLibsMysqlStrconv
180/// @}
181
182#endif // ifndef MYSQL_STRCONV_DECODE_PARSE_POSITION_H
Experimental API header.
CRTP base class (mixin) that makes your class a standard-compliant iterator, given only a minimal set...
Definition: iterator_interface.h:370
Base class for the current position of a string parser, holding both the parsed string and the positi...
Definition: parse_position.h:48
void advance(std::ptrdiff_t delta)
Move the iterator delta steps.
Definition: parse_position.h:79
const char * end() const
Return pointer to the end of the underlying string.
Definition: parse_position.h:138
std::size_t remaining_size() const
Return the remaining size.
Definition: parse_position.h:153
const unsigned char * ubegin() const
Return pointer to the beginning of the underlying string.
Definition: parse_position.h:128
const std::byte * bbegin() const
Return pointer to the beginning of the underlying string.
Definition: parse_position.h:133
const unsigned char * upos() const
Return the current position as an unsigned char pointer.
Definition: parse_position.h:113
Parse_position()=default
Construct a new object.
std::size_t str_size() const
Return the length of the underlying string.
Definition: parse_position.h:156
std::string_view parsed_str() const
Return a string_view over the left part of the string, up to the position.
Definition: parse_position.h:159
const char * pos() const
Return the current position as a char pointer.
Definition: parse_position.h:110
const char * get_pointer() const
Dereference the iterator and return the value.
Definition: parse_position.h:73
std::string_view str() const
Return a string_view over the underlying string.
Definition: parse_position.h:167
Parse_position(const std::string_view &source, std::size_t int_pos=0)
Construct a new Parse_position from the given range.
Definition: parse_position.h:55
const std::byte * bend() const
Return pointer to the end of the underlying string.
Definition: parse_position.h:148
std::ptrdiff_t distance_from(const Parse_position &other) const
Return the distance from iterator other to this.
Definition: parse_position.h:85
std::size_t int_pos() const
Return the current position as an integer.
Definition: parse_position.h:107
bool is_sentinel() const
Return true if this iterator is at the end.
Definition: parse_position.h:92
const char * begin() const
Return pointer to the beginning of the underlying string.
Definition: parse_position.h:125
std::contiguous_iterator_tag Iterator_category_t
Definition: parse_position.h:66
const unsigned char * uend() const
Return pointer to the end of the underlying string.
Definition: parse_position.h:143
std::size_t m_int_pos
The current position.
Definition: parse_position.h:174
void set_int_pos(std::size_t int_pos_arg)
Set the position to the given one.
Definition: parse_position.h:101
const std::byte * bpos() const
Return the current position as an std::byte pointer.
Definition: parse_position.h:118
std::string_view m_source
The beginning of the range.
Definition: parse_position.h:171
std::string_view remaining_str() const
Return a string_view over the remaining string.
Definition: parse_position.h:162
Experimental API header.
unsigned char byte
Blob class.
Definition: common.h:151
Definition: gtid_binary_format_conv.h:252
decltype(auto) uchar_cast(Type_t &&value)
Shorthand for char_cast<unsigned char>.
Definition: char_cast.h:65
decltype(auto) byte_cast(Type_t &&value)
Shorthand for char_cast<std::byte>.
Definition: char_cast.h:71
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42