MySQL 8.0.37
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
53#include "m_ctype.h"
54#include "my_compiler.h"
55#include "my_config.h"
56
57template <bool RANGE_CHECK, bool SUPPORT_MB4>
58static int my_mb_wc_utf8_prototype(my_wc_t *pwc, const uchar *s,
59 const uchar *e);
60
61static int my_mb_wc_utf8mb3(my_wc_t *pwc, const uchar *s, const uchar *e);
62static int my_mb_wc_utf8mb4(my_wc_t *pwc, const uchar *s, const uchar *e);
63
64/**
65 Functor that converts a UTF-8 multibyte sequence (up to three bytes)
66 to a wide character.
67*/
69 Mb_wc_utf8mb3() = default;
70
72 int operator()(my_wc_t *pwc, const uchar *s, const uchar *e) const {
73 return my_mb_wc_utf8mb3(pwc, s, e);
74 }
75};
76
77/**
78 Functor that converts a UTF-8 multibyte sequence (up to four bytes)
79 to a wide character.
80*/
82 Mb_wc_utf8mb4() = default;
83
85 int operator()(my_wc_t *pwc, const uchar *s, const uchar *e) const {
86 return my_mb_wc_utf8mb4(pwc, s, e);
87 }
88};
89
90/**
91 Functor that uses a function pointer to convert a multibyte sequence
92 to a wide character.
93*/
95 public:
97 : m_funcptr(cs->cset->mb_wc), m_cs(cs) {}
98
99 int operator()(my_wc_t *pwc, const uchar *s, const uchar *e) const {
100 return m_funcptr(m_cs, pwc, s, e);
101 }
102
103 private:
104 typedef int (*mbwc_func_t)(const CHARSET_INFO *, my_wc_t *, const uchar *,
105 const uchar *);
106
108 const CHARSET_INFO *const m_cs;
109};
110
111template <bool RANGE_CHECK, bool SUPPORT_MB4>
113 const uchar *e) {
114 if (RANGE_CHECK && s >= e) return MY_CS_TOOSMALL;
115
116 uchar c = s[0];
117 if (c < 0x80) {
118 *pwc = c;
119 return 1;
120 }
121
122 if (c < 0xe0) {
123 if (c < 0xc2) // Resulting code point would be less than 0x80.
124 return MY_CS_ILSEQ;
125
126 if (RANGE_CHECK && s + 2 > e) return MY_CS_TOOSMALL2;
127
128 if ((s[1] & 0xc0) != 0x80) // Next byte must be a continuation byte.
129 return MY_CS_ILSEQ;
130
131 *pwc = ((my_wc_t)(c & 0x1f) << 6) + (my_wc_t)(s[1] & 0x3f);
132 return 2;
133 }
134
135 if (c < 0xf0) {
136 if (RANGE_CHECK && s + 3 > e) return MY_CS_TOOSMALL3;
137
138 // Next two bytes must be continuation bytes.
139 uint16 two_bytes;
140 memcpy(&two_bytes, s + 1, sizeof(two_bytes));
141 if ((two_bytes & 0xc0c0) != 0x8080) // Endianness does not matter.
142 return MY_CS_ILSEQ;
143
144 *pwc = ((my_wc_t)(c & 0x0f) << 12) + ((my_wc_t)(s[1] & 0x3f) << 6) +
145 (my_wc_t)(s[2] & 0x3f);
146 if (*pwc < 0x800) return MY_CS_ILSEQ;
147 /*
148 According to RFC 3629, UTF-8 should prohibit characters between
149 U+D800 and U+DFFF, which are reserved for surrogate pairs and do
150 not directly represent characters.
151 */
152 if (*pwc >= 0xd800 && *pwc <= 0xdfff) return MY_CS_ILSEQ;
153 return 3;
154 }
155
156 if (SUPPORT_MB4) {
157 if (RANGE_CHECK && s + 4 > e) /* We need 4 characters */
158 return MY_CS_TOOSMALL4;
159
160 /*
161 This byte must be of the form 11110xxx, and the next three bytes
162 must be continuation bytes.
163 */
164 uint32 four_bytes;
165 memcpy(&four_bytes, s, sizeof(four_bytes));
166#ifdef WORDS_BIGENDIAN
167 if ((four_bytes & 0xf8c0c0c0) != 0xf0808080)
168#else
169 if ((four_bytes & 0xc0c0c0f8) != 0x808080f0)
170#endif
171 return MY_CS_ILSEQ;
172
173 *pwc = ((my_wc_t)(c & 0x07) << 18) + ((my_wc_t)(s[1] & 0x3f) << 12) +
174 ((my_wc_t)(s[2] & 0x3f) << 6) + (my_wc_t)(s[3] & 0x3f);
175 if (*pwc < 0x10000 || *pwc > 0x10ffff) return MY_CS_ILSEQ;
176 return 4;
177 }
178
179 return MY_CS_ILSEQ;
180}
181
182/**
183 Parses a single UTF-8 character from a byte string.
184
185 @param[out] pwc the parsed character, if any
186 @param s the string to read from
187 @param e the end of the string; will not read past this
188
189 @return the number of bytes read from s, or a value <= 0 for failure
190 (see m_ctype.h)
191*/
192static inline int my_mb_wc_utf8mb3(my_wc_t *pwc, const uchar *s,
193 const uchar *e) {
194 return my_mb_wc_utf8_prototype</*RANGE_CHECK=*/true, /*SUPPORT_MB4=*/false>(
195 pwc, s, e);
196}
197
198/**
199 Parses a single UTF-8 character from a byte string. The difference
200 between this and my_mb_wc_utf8mb3 is that this function also can handle
201 four-byte UTF-8 characters.
202
203 @param[out] pwc the parsed character, if any
204 @param s the string to read from
205 @param e the end of the string; will not read past this
206
207 @return the number of bytes read from s, or a value <= 0 for failure
208 (see m_ctype.h)
209*/
210static ALWAYS_INLINE int my_mb_wc_utf8mb4(my_wc_t *pwc, const uchar *s,
211 const uchar *e) {
212 return my_mb_wc_utf8_prototype</*RANGE_CHECK=*/true, /*SUPPORT_MB4=*/true>(
213 pwc, s, e);
214}
215
216// Non-inlined versions of the above. These are used as function pointers
217// in MY_CHARSET_HANDLER structs, and you can compare against them to see
218// if using the Mb_wc_utf8* functors would be appropriate.
219
220extern "C" int my_mb_wc_utf8mb3_thunk(const CHARSET_INFO *cs, my_wc_t *pwc,
221 const uchar *s, const uchar *e);
222
223extern "C" int my_mb_wc_utf8mb4_thunk(const CHARSET_INFO *cs, my_wc_t *pwc,
224 const uchar *s, const uchar *e);
225
226#endif // MB_WC_INCLUDED
Functor that uses a function pointer to convert a multibyte sequence to a wide character.
Definition: mb_wc.h:94
Mb_wc_through_function_pointer(const CHARSET_INFO *cs)
Definition: mb_wc.h:96
const mbwc_func_t m_funcptr
Definition: mb_wc.h:107
int(* mbwc_func_t)(const CHARSET_INFO *, my_wc_t *, const uchar *, const uchar *)
Definition: mb_wc.h:104
const CHARSET_INFO *const m_cs
Definition: mb_wc.h:108
int operator()(my_wc_t *pwc, const uchar *s, const uchar *e) const
Definition: mb_wc.h:99
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:7163
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:210
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:192
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:7179
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:68
ALWAYS_INLINE int operator()(my_wc_t *pwc, const uchar *s, const uchar *e) const
Definition: mb_wc.h:72
Mb_wc_utf8mb3()=default
Functor that converts a UTF-8 multibyte sequence (up to four bytes) to a wide character.
Definition: mb_wc.h:81
ALWAYS_INLINE int operator()(my_wc_t *pwc, const uchar *s, const uchar *e) const
Definition: mb_wc.h:85
Mb_wc_utf8mb4()=default