MySQL 8.4.0
Source Code Documentation
mb_wc.h
Go to the documentation of this file.
1#ifndef MB_WC_INCLUDED
2#define MB_WC_INCLUDED
3
4/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License, version 2.0, as published by the Free Software Foundation.
9
10 This library is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the library and your derivative works with the
15 separately licensed software that they have either included with
16 the library or referenced in the documentation.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Library General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU Library General Public
24 License along with this library; if not, write to the Free
25 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
26 MA 02110-1301 USA */
27
28/**
29 @file mb_wc.h
30
31 Definitions of mb_wc (multibyte to wide character, ie., effectively
32 “parse a UTF-8 character”) functions for UTF-8 (both three- and four-byte).
33 These are available both as inline functions, as C-style thunks so that they
34 can fit into MY_CHARSET_HANDLER, and as functors.
35
36 The functors exist so that you can specialize a class on them and get them
37 inlined instead of having to call them through the function pointer in
38 MY_CHARSET_HANDLER; mb_wc is in itself so cheap (the most common case is
39 just a single byte load and a predictable compare) that the call overhead
40 in a tight loop is significant, and these routines tend to take up a lot
41 of CPU time when sorting. Typically, at the outermost level, you'd simply
42 compare cs->cset->mb_wc with my_mb_wc_{utf8mb3,utf8mb4}_thunk, and if so,
43 instantiate your function with the given class. If it doesn't match,
44 you can use Mb_wc_through_function_pointer, which calls through the
45 function pointer as usual. (It will cache the function pointer for you,
46 which is typically faster than looking it up all the time -- the compiler
47 cannot always figure out on its own that it doesn't change.)
48
49 The Mb_wc_* classes should be sent by _value_, not by reference, since
50 they are never larger than two pointers (and usually simply zero).
51*/
52#include <cstdint>
53
54#include <string.h>
55
56#include "my_compiler.h"
57#include "my_config.h"
59
60template <bool RANGE_CHECK, bool SUPPORT_MB4>
61static int my_mb_wc_utf8_prototype(my_wc_t *pwc, const uint8_t *s,
62 const uint8_t *e);
63
64static int my_mb_wc_utf8mb3(my_wc_t *pwc, const uint8_t *s, const uint8_t *e);
65static int my_mb_wc_utf8mb4(my_wc_t *pwc, const uint8_t *s, const uint8_t *e);
66
67/**
68 Functor that converts a UTF-8 multibyte sequence (up to three bytes)
69 to a wide character.
70*/
72 Mb_wc_utf8mb3() = default;
73
75 int operator()(my_wc_t *pwc, const uint8_t *s, const uint8_t *e) const {
76 return my_mb_wc_utf8mb3(pwc, s, e);
77 }
78};
79
80/**
81 Functor that converts a UTF-8 multibyte sequence (up to four bytes)
82 to a wide character.
83*/
85 Mb_wc_utf8mb4() = default;
86
88 int operator()(my_wc_t *pwc, const uint8_t *s, const uint8_t *e) const {
89 return my_mb_wc_utf8mb4(pwc, s, e);
90 }
91};
92
93/**
94 Functor that uses a function pointer to convert a multibyte sequence
95 to a wide character.
96*/
98 public:
100 : m_funcptr(cs->cset->mb_wc), m_cs(cs) {}
101
102 int operator()(my_wc_t *pwc, const uint8_t *s, const uint8_t *e) const {
103 return m_funcptr(m_cs, pwc, s, e);
104 }
105
106 private:
107 typedef int (*mbwc_func_t)(const CHARSET_INFO *, my_wc_t *, const uint8_t *,
108 const uint8_t *);
109
111 const CHARSET_INFO *const m_cs;
112};
113
114template <bool RANGE_CHECK, bool SUPPORT_MB4>
115static ALWAYS_INLINE int my_mb_wc_utf8_prototype(my_wc_t *pwc, const uint8_t *s,
116 const uint8_t *e) {
117 if (RANGE_CHECK && s >= e) return MY_CS_TOOSMALL;
118
119 uint8_t c = s[0];
120 if (c < 0x80) {
121 *pwc = c;
122 return 1;
123 }
124
125 if (c < 0xe0) {
126 if (c < 0xc2) // Resulting code point would be less than 0x80.
127 return MY_CS_ILSEQ;
128
129 if (RANGE_CHECK && s + 2 > e) return MY_CS_TOOSMALL2;
130
131 if ((s[1] & 0xc0) != 0x80) // Next byte must be a continuation byte.
132 return MY_CS_ILSEQ;
133
134 *pwc = ((my_wc_t)(c & 0x1f) << 6) + (my_wc_t)(s[1] & 0x3f);
135 return 2;
136 }
137
138 if (c < 0xf0) {
139 if (RANGE_CHECK && s + 3 > e) return MY_CS_TOOSMALL3;
140
141 // Next two bytes must be continuation bytes.
142 uint16_t two_bytes = 0;
143 memcpy(&two_bytes, s + 1, sizeof(two_bytes));
144 if ((two_bytes & 0xc0c0) != 0x8080) // Endianness does not matter.
145 return MY_CS_ILSEQ;
146
147 *pwc = ((my_wc_t)(c & 0x0f) << 12) + ((my_wc_t)(s[1] & 0x3f) << 6) +
148 (my_wc_t)(s[2] & 0x3f);
149 if (*pwc < 0x800) return MY_CS_ILSEQ;
150 /*
151 According to RFC 3629, UTF-8 should prohibit characters between
152 U+D800 and U+DFFF, which are reserved for surrogate pairs and do
153 not directly represent characters.
154 */
155 if (*pwc >= 0xd800 && *pwc <= 0xdfff) return MY_CS_ILSEQ;
156 return 3;
157 }
158
159 if (SUPPORT_MB4) {
160 if (RANGE_CHECK && s + 4 > e) /* We need 4 characters */
161 return MY_CS_TOOSMALL4;
162
163 /*
164 This byte must be of the form 11110xxx, and the next three bytes
165 must be continuation bytes.
166 */
167 uint32_t four_bytes = 0;
168 memcpy(&four_bytes, s, sizeof(four_bytes));
169#ifdef WORDS_BIGENDIAN
170 if ((four_bytes & 0xf8c0c0c0) != 0xf0808080)
171#else
172 if ((four_bytes & 0xc0c0c0f8) != 0x808080f0)
173#endif
174 return MY_CS_ILSEQ;
175
176 *pwc = ((my_wc_t)(c & 0x07) << 18) + ((my_wc_t)(s[1] & 0x3f) << 12) +
177 ((my_wc_t)(s[2] & 0x3f) << 6) + (my_wc_t)(s[3] & 0x3f);
178 if (*pwc < 0x10000 || *pwc > 0x10ffff) return MY_CS_ILSEQ;
179 return 4;
180 }
181
182 return MY_CS_ILSEQ;
183}
184
185/**
186 Parses a single UTF-8 character from a byte string.
187
188 @param[out] pwc the parsed character, if any
189 @param s the string to read from
190 @param e the end of the string; will not read past this
191
192 @return the number of bytes read from s, or a value <= 0 for failure
193 (see m_ctype.h)
194*/
195static inline int my_mb_wc_utf8mb3(my_wc_t *pwc, const uint8_t *s,
196 const uint8_t *e) {
197 return my_mb_wc_utf8_prototype</*RANGE_CHECK=*/true, /*SUPPORT_MB4=*/false>(
198 pwc, s, e);
199}
200
201/**
202 Parses a single UTF-8 character from a byte string. The difference
203 between this and my_mb_wc_utf8mb3 is that this function also can handle
204 four-byte UTF-8 characters.
205
206 @param[out] pwc the parsed character, if any
207 @param s the string to read from
208 @param e the end of the string; will not read past this
209
210 @return the number of bytes read from s, or a value <= 0 for failure
211 (see m_ctype.h)
212*/
213static ALWAYS_INLINE int my_mb_wc_utf8mb4(my_wc_t *pwc, const uint8_t *s,
214 const uint8_t *e) {
215 return my_mb_wc_utf8_prototype</*RANGE_CHECK=*/true, /*SUPPORT_MB4=*/true>(
216 pwc, s, e);
217}
218
219// Non-inlined versions of the above. These are used as function pointers
220// in MY_CHARSET_HANDLER structs, and you can compare against them to see
221// if using the Mb_wc_utf8* functors would be appropriate.
222
223extern "C" int my_mb_wc_utf8mb3_thunk(const CHARSET_INFO *cs, my_wc_t *pwc,
224 const uint8_t *s, const uint8_t *e);
225
226extern "C" int my_mb_wc_utf8mb4_thunk(const CHARSET_INFO *cs, my_wc_t *pwc,
227 const uint8_t *s, const uint8_t *e);
228
229#endif // MB_WC_INCLUDED
Functor that uses a function pointer to convert a multibyte sequence to a wide character.
Definition: mb_wc.h:97
Mb_wc_through_function_pointer(const CHARSET_INFO *cs)
Definition: mb_wc.h:99
const mbwc_func_t m_funcptr
Definition: mb_wc.h:110
const CHARSET_INFO *const m_cs
Definition: mb_wc.h:111
int operator()(my_wc_t *pwc, const uint8_t *s, const uint8_t *e) const
Definition: mb_wc.h:102
int(* mbwc_func_t)(const CHARSET_INFO *, my_wc_t *, const uint8_t *, const uint8_t *)
Definition: mb_wc.h:107
A better implementation of the UNIX ctype(3) library.
static constexpr int MY_CS_TOOSMALL4
Definition: m_ctype.h:97
static constexpr int MY_CS_TOOSMALL
Definition: m_ctype.h:89
static constexpr int MY_CS_TOOSMALL3
Definition: m_ctype.h:93
unsigned long my_wc_t
Our own version of wchar_t, ie., a type that holds a single Unicode code point ("wide character").
Definition: m_ctype.h:57
static constexpr int MY_CS_ILSEQ
Definition: m_ctype.h:85
static constexpr int MY_CS_TOOSMALL2
Definition: m_ctype.h:91
int my_mb_wc_utf8mb4_thunk(const CHARSET_INFO *cs, my_wc_t *pwc, const uint8_t *s, const uint8_t *e)
A thunk to be able to use my_mb_wc_utf8mb4 in MY_CHARSET_HANDLER structs.
Definition: ctype-utf8.cc:7195
int my_mb_wc_utf8mb3_thunk(const CHARSET_INFO *cs, my_wc_t *pwc, const uint8_t *s, const uint8_t *e)
A thunk to be able to use my_mb_wc_utf8mb3 in MY_CHARSET_HANDLER structs.
Definition: ctype-utf8.cc:7179
static int my_mb_wc_utf8mb4(my_wc_t *pwc, const uint8_t *s, const uint8_t *e)
Parses a single UTF-8 character from a byte string.
Definition: mb_wc.h:213
static int my_mb_wc_utf8_prototype(my_wc_t *pwc, const uint8_t *s, const uint8_t *e)
static int my_mb_wc_utf8mb3(my_wc_t *pwc, const uint8_t *s, const uint8_t *e)
Parses a single UTF-8 character from a byte string.
Definition: mb_wc.h:195
Header for compiler-dependent features.
#define ALWAYS_INLINE
Definition: my_compiler.h:99
Definition: commit_order_queue.h:34
Definition: m_ctype.h:423
Functor that converts a UTF-8 multibyte sequence (up to three bytes) to a wide character.
Definition: mb_wc.h:71
ALWAYS_INLINE int operator()(my_wc_t *pwc, const uint8_t *s, const uint8_t *e) const
Definition: mb_wc.h:75
Mb_wc_utf8mb3()=default
Functor that converts a UTF-8 multibyte sequence (up to four bytes) to a wide character.
Definition: mb_wc.h:84
ALWAYS_INLINE int operator()(my_wc_t *pwc, const uint8_t *s, const uint8_t *e) const
Definition: mb_wc.h:88
Mb_wc_utf8mb4()=default