Skip to content

Commit 0a83caf

Browse files
author
Alexander Barkov
committed
Removing duplicate code: sharing implementation of "strnxfrm"
between gbk_chinese_ci and big5_chinese_ci.
1 parent 0d42d4e commit 0a83caf

File tree

3 files changed

+46
-68
lines changed

3 files changed

+46
-68
lines changed

strings/ctype-big5.c

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545

4646
#define isbig5code(c,d) (isbig5head(c) && isbig5tail(d))
4747
#define big5code(c,d) (((uchar)(c) <<8) | (uchar)(d))
48-
#define big5head(e) ((uchar)(e>>8))
49-
#define big5tail(e) ((uchar)(e&0xff))
5048

5149
#define MY_FUNCTION_NAME(x) my_ ## x ## _big5
5250
#define IS_MB1_CHAR(x) ((uchar) (x) < 0x80)
@@ -850,38 +848,6 @@ static uint16 big5strokexfrm(uint16 i)
850848
}
851849

852850

853-
static size_t
854-
my_strnxfrm_big5(CHARSET_INFO *cs,
855-
uchar *dst, size_t dstlen, uint nweights,
856-
const uchar *src, size_t srclen, uint flags)
857-
{
858-
uchar *d0= dst;
859-
uchar *de= dst + dstlen;
860-
const uchar *se= src + srclen;
861-
const uchar *sort_order= cs->sort_order;
862-
863-
for (; dst < de && src < se && nweights; nweights--)
864-
{
865-
if (my_charlen(cs, (const char *) src, (const char *) se) > 1)
866-
{
867-
/*
868-
Note, it is safe not to check (src < se)
869-
in the code below, because my_charlen() would
870-
not return 2 if src was too short
871-
*/
872-
uint16 e= big5strokexfrm((uint16) big5code(*src, *(src + 1)));
873-
*dst++= big5head(e);
874-
if (dst < de)
875-
*dst++= big5tail(e);
876-
src+= 2;
877-
}
878-
else
879-
*dst++= sort_order ? sort_order[*src++] : *src++;
880-
}
881-
return my_strxfrm_pad_desc_and_reverse(cs, d0, dst, de, nweights, flags, 0);
882-
}
883-
884-
885851
static uint mbcharlen_big5(CHARSET_INFO *cs __attribute__((unused)), uint c)
886852
{
887853
return (isbig5head(c)? 2 : 1);
@@ -6719,6 +6685,8 @@ my_mb_wc_big5(CHARSET_INFO *cs __attribute__((unused)),
67196685
#define MY_FUNCTION_NAME(x) my_ ## x ## _big5_chinese_ci
67206686
#define WEIGHT_MB1(x) (sort_order_big5[(uchar) (x)])
67216687
#define WEIGHT_MB2(x,y) (big5code(x, y))
6688+
#define WEIGHT_MB2_FRM(x,y) (big5strokexfrm((uint16) WEIGHT_MB2(x, y)))
6689+
#define DEFINE_STRNXFRM
67226690
#include "strcoll.ic"
67236691

67246692

@@ -6733,7 +6701,7 @@ static MY_COLLATION_HANDLER my_collation_handler_big5_chinese_ci=
67336701
NULL, /* init */
67346702
my_strnncoll_big5_chinese_ci,
67356703
my_strnncollsp_big5_chinese_ci,
6736-
my_strnxfrm_big5,
6704+
my_strnxfrm_big5_chinese_ci,
67376705
my_strnxfrmlen_simple,
67386706
my_like_range_mb,
67396707
my_wildcmp_mb,

strings/ctype-gbk.c

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,38 +3451,6 @@ static uint16 gbksortorder(uint16 i)
34513451
}
34523452

34533453

3454-
static size_t
3455-
my_strnxfrm_gbk(CHARSET_INFO *cs,
3456-
uchar *dst, size_t dstlen, uint nweights,
3457-
const uchar *src, size_t srclen, uint flags)
3458-
{
3459-
uchar *d0= dst;
3460-
uchar *de= dst + dstlen;
3461-
const uchar *se= src + srclen;
3462-
const uchar *sort_order= cs->sort_order;
3463-
3464-
for (; dst < de && src < se && nweights; nweights--)
3465-
{
3466-
if (my_charlen(cs, (const char *) src, (const char *) se) > 1)
3467-
{
3468-
/*
3469-
Note, it is safe not to check (src < se)
3470-
in the code below, because my_charlen() would
3471-
not return 2 if src was too short
3472-
*/
3473-
uint16 e= gbksortorder((uint16) gbkcode(*src, *(src + 1)));
3474-
*dst++= gbkhead(e);
3475-
if (dst < de)
3476-
*dst++= gbktail(e);
3477-
src+= 2;
3478-
}
3479-
else
3480-
*dst++= sort_order ? sort_order[*src++] : *src++;
3481-
}
3482-
return my_strxfrm_pad_desc_and_reverse(cs, d0, dst, de, nweights, flags, 0);
3483-
}
3484-
3485-
34863454
static uint mbcharlen_gbk(CHARSET_INFO *cs __attribute__((unused)),uint c)
34873455
{
34883456
return (isgbkhead(c)? 2 : 1);
@@ -10652,6 +10620,7 @@ my_mb_wc_gbk(CHARSET_INFO *cs __attribute__((unused)),
1065210620
#define MY_FUNCTION_NAME(x) my_ ## x ## _gbk_chinese_ci
1065310621
#define WEIGHT_MB1(x) (sort_order_gbk[(uchar) (x)])
1065410622
#define WEIGHT_MB2(x,y) (gbksortorder(gbkcode(x,y)))
10623+
#define DEFINE_STRNXFRM
1065510624
#include "strcoll.ic"
1065610625

1065710626

@@ -10666,7 +10635,7 @@ static MY_COLLATION_HANDLER my_collation_handler_gbk_chinese_ci=
1066610635
NULL, /* init */
1066710636
my_strnncoll_gbk_chinese_ci,
1066810637
my_strnncollsp_gbk_chinese_ci,
10669-
my_strnxfrm_gbk,
10638+
my_strnxfrm_gbk_chinese_ci,
1067010639
my_strnxfrmlen_simple,
1067110640
my_like_range_mb,
1067210641
my_wildcmp_mb,

strings/strcoll.ic

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,45 @@ MY_FUNCTION_NAME(strnncollsp)(CHARSET_INFO *cs __attribute__((unused)),
262262
return 0;
263263
}
264264

265+
266+
#ifdef DEFINE_STRNXFRM
267+
#ifndef WEIGHT_MB2_FRM
268+
#define WEIGHT_MB2_FRM(x,y) WEIGHT_MB2(x,y)
269+
#endif
270+
271+
static size_t
272+
MY_FUNCTION_NAME(strnxfrm)(CHARSET_INFO *cs,
273+
uchar *dst, size_t dstlen, uint nweights,
274+
const uchar *src, size_t srclen, uint flags)
275+
{
276+
uchar *d0= dst;
277+
uchar *de= dst + dstlen;
278+
const uchar *se= src + srclen;
279+
const uchar *sort_order= cs->sort_order;
280+
281+
for (; dst < de && src < se && nweights; nweights--)
282+
{
283+
if (my_charlen(cs, (const char *) src, (const char *) se) > 1)
284+
{
285+
/*
286+
Note, it is safe not to check (src < se)
287+
in the code below, because my_charlen() would
288+
not return 2 if src was too short
289+
*/
290+
uint16 e= WEIGHT_MB2_FRM(src[0], src[1]);
291+
*dst++= (uchar) (e >> 8);
292+
if (dst < de)
293+
*dst++= (uchar) (e & 0xFF);
294+
src+= 2;
295+
}
296+
else
297+
*dst++= sort_order ? sort_order[*src++] : *src++;
298+
}
299+
return my_strxfrm_pad_desc_and_reverse(cs, d0, dst, de, nweights, flags, 0);
300+
}
301+
#endif /* DEFINE_STRNXFRM */
302+
303+
265304
/*
266305
We usually include this file at least two times from the same source file,
267306
for the _ci and the _bin collations. Prepare for the second inclusion.
@@ -273,3 +312,5 @@ MY_FUNCTION_NAME(strnncollsp)(CHARSET_INFO *cs __attribute__((unused)),
273312
#undef WEIGHT_MB3
274313
#undef WEIGHT_MB4
275314
#undef WEIGHT_PAD_SPACE
315+
#undef WEIGHT_MB2_FRM
316+
#undef DEFINE_STRNXFRM

0 commit comments

Comments
 (0)