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