summaryrefslogtreecommitdiff
path: root/strings/ctype-mb.inl
blob: 6cde31a34ad5a093e19052bf0da8ba159dfa0460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
   Copyright (c) 2015, MariaDB Foundation

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA
*/


#ifndef MY_FUNCTION_NAME
#error MY_FUNCTION_NAME is not defined
#endif

#if defined(IS_MB3_CHAR) && !defined(IS_MB2_CHAR)
#error IS_MB3_CHAR is defined, while IS_MB2_CHAR is not!
#endif

#if defined(IS_MB4_CHAR) && !defined(IS_MB3_CHAR)
#error IS_MB4_CHAR is defined, while IS_MB3_CHAR is not!
#endif


#ifdef DEFINE_ASIAN_ROUTINES
#define DEFINE_WELL_FORMED_CHAR_LENGTH
#define DEFINE_CHARLEN
#define DEFINE_NATIVE_TO_MB_VARLEN
#endif


#ifdef DEFINE_CHARLEN
/**
  Returns length of the left-most character of a string.
  @param cs - charset with mbminlen==1 and mbmaxlen<=4
  @param b  - the beginning of the string
  @param e  - the end of the string

  @return   MY_CS_ILSEQ         if a bad byte sequence was found
  @return   MY_CS_TOOSMALL(N)   if the string ended unexpectedly
  @return   >0                  if a valid character was found
*/
static int
MY_FUNCTION_NAME(charlen)(CHARSET_INFO *cs __attribute__((unused)),
                          const uchar *b, const uchar *e)
{
  DBUG_ASSERT(cs->mbminlen == 1);
  DBUG_ASSERT(cs->mbmaxlen <= 4);

  if (b >= e)
    return MY_CS_TOOSMALL;
  if ((uchar) b[0] < 128)
    return 1; /* Single byte ASCII character */

#ifdef IS_8BIT_CHAR
  if (IS_8BIT_CHAR(b[0]))
  {      
    /* Single byte non-ASCII character, e.g. half width kana in sjis */
    return 1;
  }
#endif

  if (b + 2 > e)
    return MY_CS_TOOSMALLN(2);
  if (IS_MB2_CHAR(b[0], b[1]))
    return 2; /* Double byte character */

#ifdef IS_MB3_CHAR
  if (b + 3 > e)
  {
#ifdef IS_MB_PREFIX2
    if (!IS_MB_PREFIX2(b[0], b[1]))
      return MY_CS_ILSEQ;
#endif
    return MY_CS_TOOSMALLN(3);
  }
  if (IS_MB3_CHAR(b[0], b[1], b[2]))
    return 3; /* Three-byte character */
#endif

#ifdef IS_MB4_CHAR
  if (b + 4 > e)
    return MY_CS_TOOSMALLN(4);
  if (IS_MB4_CHAR(b[0], b[1], b[2], b[3]))
    return 4; /* Four-byte character */
#endif

  /* Wrong byte sequence */
  return MY_CS_ILSEQ;
}
#endif /* DEFINE_CHARLEN */


#ifdef DEFINE_WELL_FORMED_CHAR_LENGTH
/**
  Returns well formed length of a string 
  measured in characters (rather than in bytes).
  Version for character sets that define IS_MB?_CHAR(), e.g. big5.
*/
static size_t
MY_FUNCTION_NAME(well_formed_char_length)(CHARSET_INFO *cs __attribute__((unused)),
                                          const char *b, const char *e,
                                          size_t nchars,
                                          MY_STRCOPY_STATUS *status)
{
  size_t nchars0= nchars;
  for ( ; b < e && nchars ; nchars--)
  {
    if ((uchar) b[0] < 128)
    {
      b++; /* Single byte ASCII character */
      continue;
    }

    if (b + 2 <= e && IS_MB2_CHAR(b[0], b[1]))
    {
      b+= 2; /* Double byte character */
      continue;
    }

#ifdef IS_MB3_CHAR
    if (b + 3 <= e && IS_MB3_CHAR(b[0], b[1], b[2]))
    {
      b+= 3; /* Three-byte character */
      continue;
    }
#endif

#ifdef IS_MB4_CHAR
    if (b + 4 <= e && IS_MB4_CHAR(b[0], b[1], b[2], b[3]))
    {
      b+= 4; /* Four-byte character */
      continue;
    }
#endif

#ifdef IS_8BIT_CHAR
    if (IS_8BIT_CHAR(b[0]))
    {      
      b++; /* Single byte non-ASCII character, e.g. half width kana in sjis */
      continue;
    }
#endif

    /* Wrong byte sequence */
    status->m_source_end_pos= status->m_well_formed_error_pos= b;
    return nchars0 - nchars;
  }
  status->m_source_end_pos= b;
  status->m_well_formed_error_pos= NULL;
  return nchars0 - nchars;
}
#endif /* DEFINE_WELL_FORMED_CHAR_LENGTH */


#ifdef DEFINE_WELL_FORMED_CHAR_LENGTH_USING_CHARLEN
#ifndef CHARLEN
#error CHARLEN is not defined
#endif
/**
  Returns well formed length of a string 
  measured in characters (rather than in bytes).
  Version for character sets that define CHARLEN(), e.g. utf8mb3.
  CHARLEN(cs,b,e) must use the same return code convension that mb_wc() does:
  - a positive number in the range [1-mbmaxlen] if a valid
    single-byte or multi-byte character was found
  - MY_CS_ILSEQ (0) on a bad byte sequence
  - MY_CS_TOOSMALLxx if the incoming sequence is incomplete
*/
static size_t
MY_FUNCTION_NAME(well_formed_char_length)(CHARSET_INFO *cs __attribute__((unused)),
                                          const char *b, const char *e,
                                          size_t nchars,
                                          MY_STRCOPY_STATUS *status)
{
  size_t nchars0= nchars;
  int chlen;
  for ( ; nchars ; nchars--, b+= chlen)
  {
    if ((chlen= CHARLEN(cs, (uchar*) b, (uchar*) e)) <= 0)
    {
      status->m_well_formed_error_pos= b < e ? b : NULL;
      status->m_source_end_pos= b;
      return nchars0 - nchars;
    }
  }
  status->m_well_formed_error_pos= NULL;
  status->m_source_end_pos= b;
  return nchars0 - nchars;
}
#endif /* DEFINE_WELL_FORMED_CHAR_LENGTH_USING_CHARLEN */


#ifdef DEFINE_NATIVE_TO_MB_VARLEN
/*
  Write a native 2-byte character.
  If the full character does not fit, only the first byte is written.
*/
static inline int
my_native_to_mb_fixed2(my_wc_t wc, uchar *s, uchar *e)
{
  /* The caller must insure there is a space for at least one byte */
  DBUG_ASSERT(s < e);
  s[0]= (uchar) (wc >> 8);
  if (s + 2 > e)
    return MY_CS_TOOSMALL2;
  s[1]= wc & 0xFF;
  return 2;
}


/*
  Write a native 3-byte character.
  If the full character does not fit, only the leading bytes are written.
*/
static inline int
my_native_to_mb_fixed3(my_wc_t wc, uchar *s, uchar *e)
{
  /* The caller must insure there is a space for at least one byte */
  DBUG_ASSERT(s < e);
  s[0]= (uchar) (wc >> 16);
  if (s + 2 > e)
    return MY_CS_TOOSMALL2;
  s[1]= (wc >> 8) & 0xFF;
  if (s + 3 > e)
    return MY_CS_TOOSMALL3;
  s[2]= wc & 0xFF;
  return 3;
}


/*
  Write a native 1-byte or 2-byte or 3-byte character.
*/

static int
MY_FUNCTION_NAME(native_to_mb)(CHARSET_INFO *cs __attribute__((unused)),
                               my_wc_t wc, uchar *s, uchar *e)
{
  if (s >= e)
    return MY_CS_TOOSMALL;
  if ((int) wc <= 0xFF)
  {
    s[0]= (uchar) wc;
    return 1;
  }
#ifdef IS_MB3_HEAD
  if (wc > 0xFFFF)
    return my_native_to_mb_fixed3(wc, s, e);
#endif
  return my_native_to_mb_fixed2(wc, s, e);
}
#endif /* DEFINE_NATIVE_TO_MB_VARLEN */


#undef MY_FUNCTION_NAME