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