ctype-ucs2.c 76.5 KB
Newer Older
1
/* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
2 3 4
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
5 6
   License as published by the Free Software Foundation; version 2
   of the License.
7 8 9 10 11 12 13 14
   
   This library 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
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
15
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16
   MA 02110-1301, USA */
17 18 19 20

/* UCS2 support. Written by Alexander Barkov <bar@mysql.com> */

#include <my_global.h>
21
#include <my_sys.h>
22 23 24
#include "m_string.h"
#include "m_ctype.h"
#include <errno.h>
25
#include <stdarg.h>
26 27


Alexander Barkov's avatar
Alexander Barkov committed
28 29 30 31 32 33 34 35 36
#if defined(HAVE_CHARSET_utf16) || defined(HAVE_CHARSET_ucs2)
#define HAVE_CHARSET_mb2
#endif


#if defined(HAVE_CHARSET_mb2) || defined(HAVE_CHARSET_utf32)
#define HAVE_CHARSET_mb2_or_mb4
#endif

37

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
38 39 40 41
#ifndef EILSEQ
#define EILSEQ ENOENT
#endif

Alexander Barkov's avatar
Alexander Barkov committed
42 43 44 45 46 47 48
#undef  ULONGLONG_MAX
#define ULONGLONG_MAX                (~(ulonglong) 0)
#define MAX_NEGATIVE_NUMBER        ((ulonglong) LL(0x8000000000000000))
#define INIT_CNT  9
#define LFACTOR   ULL(1000000000)
#define LFACTOR1  ULL(10000000000)
#define LFACTOR2  ULL(100000000000)
49

Alexander Barkov's avatar
Alexander Barkov committed
50 51
static unsigned long lfactor[9]=
{ 1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L, 100000000L };
52 53 54



Alexander Barkov's avatar
Alexander Barkov committed
55 56 57 58
#ifdef HAVE_CHARSET_mb2_or_mb4
static inline int
my_bincmp(const uchar *s, const uchar *se,
          const uchar *t, const uchar *te)
59
{
Alexander Barkov's avatar
Alexander Barkov committed
60 61 62 63
  int slen= (int) (se - s), tlen= (int) (te - t);
  int len= min(slen, tlen);
  int cmp= memcmp(s, t, len);
  return cmp ? cmp : slen - tlen;
64 65 66
}


Alexander Barkov's avatar
Alexander Barkov committed
67 68 69
static size_t
my_caseup_str_mb2_or_mb4(CHARSET_INFO * cs  __attribute__((unused)), 
                         char * s __attribute__((unused)))
70
{
Alexander Barkov's avatar
Alexander Barkov committed
71 72
  DBUG_ASSERT(0);
  return 0;
73 74
}

75

Alexander Barkov's avatar
Alexander Barkov committed
76 77 78
static size_t
my_casedn_str_mb2_or_mb4(CHARSET_INFO *cs __attribute__((unused)), 
                         char * s __attribute__((unused)))
79
{
Alexander Barkov's avatar
Alexander Barkov committed
80 81
  DBUG_ASSERT(0);
  return 0;
82 83 84
}


Alexander Barkov's avatar
Alexander Barkov committed
85 86 87 88
static int
my_strcasecmp_mb2_or_mb4(CHARSET_INFO *cs __attribute__((unused)),
                         const char *s __attribute__((unused)),
                         const char *t __attribute__((unused)))
89
{
Alexander Barkov's avatar
Alexander Barkov committed
90
  DBUG_ASSERT(0);
91
  return 0;
92 93 94
}


Alexander Barkov's avatar
Alexander Barkov committed
95 96 97 98
static long
my_strntol_mb2_or_mb4(CHARSET_INFO *cs,
                      const char *nptr, size_t l, int base,
                      char **endptr, int *err)
99
{
Alexander Barkov's avatar
Alexander Barkov committed
100 101 102 103 104 105 106 107 108 109 110 111 112
  int      negative= 0;
  int      overflow;
  int      cnv;
  my_wc_t  wc;
  register unsigned int cutlim;
  register uint32 cutoff;
  register uint32 res;
  register const uchar *s= (const uchar*) nptr;
  register const uchar *e= (const uchar*) nptr+l;
  const uchar *save;
  
  *err= 0;
  do
113
  {
Alexander Barkov's avatar
Alexander Barkov committed
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
    if ((cnv= cs->cset->mb_wc(cs, &wc, s, e))>0)
    {
      switch (wc)
      {
        case ' ' : break;
        case '\t': break;
        case '-' : negative= !negative; break;
        case '+' : break;
        default  : goto bs;
      }
    } 
    else /* No more characters or bad multibyte sequence */
    {
      if (endptr != NULL )
        *endptr= (char*) s;
      err[0]= (cnv==MY_CS_ILSEQ) ? EILSEQ : EDOM;
      return 0;
    } 
    s+= cnv;
  } while (1);
  
bs:

  overflow= 0;
  res= 0;
  save= s;
  cutoff= ((uint32)~0L) / (uint32) base;
  cutlim= (uint) (((uint32)~0L) % (uint32) base);
  
  do {
    if ((cnv= cs->cset->mb_wc(cs, &wc, s, e)) > 0)
    {
      s+= cnv;
      if (wc >= '0' && wc <= '9')
        wc-= '0';
      else if (wc >= 'A' && wc <= 'Z')
        wc= wc - 'A' + 10;
      else if (wc >= 'a' && wc <= 'z')
        wc= wc - 'a' + 10;
      else
        break;
      if ((int)wc >= base)
        break;
      if (res > cutoff || (res == cutoff && wc > cutlim))
        overflow= 1;
      else
      {
        res*= (uint32) base;
        res+= wc;
      }
    }
    else if (cnv == MY_CS_ILSEQ)
    {
      if (endptr !=NULL )
        *endptr = (char*) s;
      err[0]= EILSEQ;
      return 0;
    } 
    else
    {
      /* No more characters */
175
      break;
Alexander Barkov's avatar
Alexander Barkov committed
176 177 178 179 180 181 182 183 184 185
    }
  } while(1);
  
  if (endptr != NULL)
    *endptr = (char *) s;
  
  if (s == save)
  {
    err[0]= EDOM;
    return 0L;
186
  }
Alexander Barkov's avatar
Alexander Barkov committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  
  if (negative)
  {
    if (res > (uint32) INT_MIN32)
      overflow= 1;
  }
  else if (res > INT_MAX32)
    overflow= 1;
  
  if (overflow)
  {
    err[0]= ERANGE;
    return negative ? INT_MIN32 : INT_MAX32;
  }
  
  return (negative ? -((long) res) : (long) res);
203 204 205
}


Alexander Barkov's avatar
Alexander Barkov committed
206 207 208 209
static ulong
my_strntoul_mb2_or_mb4(CHARSET_INFO *cs,
                       const char *nptr, size_t l, int base, 
                       char **endptr, int *err)
210
{
Alexander Barkov's avatar
Alexander Barkov committed
211 212 213 214 215 216 217 218 219 220 221 222 223
  int      negative= 0;
  int      overflow;
  int      cnv;
  my_wc_t  wc;
  register unsigned int cutlim;
  register uint32 cutoff;
  register uint32 res;
  register const uchar *s= (const uchar*) nptr;
  register const uchar *e= (const uchar*) nptr + l;
  const uchar *save;
  
  *err= 0;
  do
224
  {
Alexander Barkov's avatar
Alexander Barkov committed
225
    if ((cnv= cs->cset->mb_wc(cs, &wc, s, e)) > 0)
226
    {
Alexander Barkov's avatar
Alexander Barkov committed
227 228 229 230 231 232 233 234 235 236
      switch (wc)
      {
        case ' ' : break;
        case '\t': break;
        case '-' : negative= !negative; break;
        case '+' : break;
        default  : goto bs;
      }
    } 
    else /* No more characters or bad multibyte sequence */
237
    {
Alexander Barkov's avatar
Alexander Barkov committed
238 239 240 241 242 243 244 245 246
      if (endptr !=NULL )
        *endptr= (char*)s;
      err[0]= (cnv == MY_CS_ILSEQ) ? EILSEQ : EDOM;
      return 0;
    } 
    s+= cnv;
  } while (1);
  
bs:
247

Alexander Barkov's avatar
Alexander Barkov committed
248 249 250 251 252 253 254
  overflow= 0;
  res= 0;
  save= s;
  cutoff= ((uint32)~0L) / (uint32) base;
  cutlim= (uint) (((uint32)~0L) % (uint32) base);
  
  do
255
  {
Alexander Barkov's avatar
Alexander Barkov committed
256
    if ((cnv= cs->cset->mb_wc(cs, &wc, s, e)) > 0)
257
    {
Alexander Barkov's avatar
Alexander Barkov committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
      s+= cnv;
      if (wc >= '0' && wc <= '9')
        wc-= '0';
      else if (wc >= 'A' && wc <= 'Z')
        wc= wc - 'A' + 10;
      else if (wc >= 'a' && wc <= 'z')
        wc= wc - 'a' + 10;
      else
        break;
      if ((int) wc >= base)
        break;
      if (res > cutoff || (res == cutoff && wc > cutlim))
        overflow = 1;
      else
      {
        res*= (uint32) base;
        res+= wc;
      }
276
    }
Alexander Barkov's avatar
Alexander Barkov committed
277
    else if (cnv == MY_CS_ILSEQ)
278
    {
Alexander Barkov's avatar
Alexander Barkov committed
279 280 281 282 283 284
      if (endptr != NULL )
        *endptr= (char*)s;
      err[0]= EILSEQ;
      return 0;
    } 
    else
285
    {
Alexander Barkov's avatar
Alexander Barkov committed
286
      /* No more characters */
287 288
      break;
    }
Alexander Barkov's avatar
Alexander Barkov committed
289 290 291 292 293 294 295 296 297
  } while(1);
  
  if (endptr != NULL)
    *endptr= (char *) s;
  
  if (s == save)
  {
    err[0]= EDOM;
    return 0L;
298
  }
Alexander Barkov's avatar
Alexander Barkov committed
299 300
  
  if (overflow)
301
  {
Alexander Barkov's avatar
Alexander Barkov committed
302 303
    err[0]= (ERANGE);
    return (~(uint32) 0);
304 305
  }
  
Alexander Barkov's avatar
Alexander Barkov committed
306
  return (negative ? -((long) res) : (long) res);
307 308 309
}


Alexander Barkov's avatar
Alexander Barkov committed
310 311 312 313
static longlong 
my_strntoll_mb2_or_mb4(CHARSET_INFO *cs,
                       const char *nptr, size_t l, int base,
                       char **endptr, int *err)
314 315 316 317 318
{
  int      negative=0;
  int      overflow;
  int      cnv;
  my_wc_t  wc;
Alexander Barkov's avatar
Alexander Barkov committed
319
  register ulonglong    cutoff;
320
  register unsigned int cutlim;
Alexander Barkov's avatar
Alexander Barkov committed
321
  register ulonglong    res;
322 323 324 325 326 327 328
  register const uchar *s= (const uchar*) nptr;
  register const uchar *e= (const uchar*) nptr+l;
  const uchar *save;
  
  *err= 0;
  do
  {
329
    if ((cnv=cs->cset->mb_wc(cs,&wc,s,e))>0)
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    {
      switch (wc)
      {
        case ' ' : break;
        case '\t': break;
        case '-' : negative= !negative; break;
        case '+' : break;
        default  : goto bs;
      }
    } 
    else /* No more characters or bad multibyte sequence */
    {
      if (endptr !=NULL )
        *endptr = (char*)s;
      err[0] = (cnv==MY_CS_ILSEQ) ? EILSEQ : EDOM;
      return 0;
    } 
    s+=cnv;
  } while (1);
  
bs:

  overflow = 0;
  res = 0;
  save = s;
  cutoff = (~(ulonglong) 0) / (unsigned long int) base;
  cutlim = (uint) ((~(ulonglong) 0) % (unsigned long int) base);

  do {
359
    if ((cnv=cs->cset->mb_wc(cs,&wc,s,e))>0)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    {
      s+=cnv;
      if ( wc>='0' && wc<='9')
        wc -= '0';
      else if ( wc>='A' && wc<='Z')
        wc = wc - 'A' + 10;
      else if ( wc>='a' && wc<='z')
        wc = wc - 'a' + 10;
      else
        break;
      if ((int)wc >= base)
        break;
      if (res > cutoff || (res == cutoff && wc > cutlim))
        overflow = 1;
      else
      {
        res *= (ulonglong) base;
        res += wc;
      }
    }
    else if (cnv==MY_CS_ILSEQ)
    {
      if (endptr !=NULL )
        *endptr = (char*)s;
      err[0]=EILSEQ;
      return 0;
    } 
    else
    {
      /* No more characters */
      break;
    }
  } while(1);
  
  if (endptr != NULL)
    *endptr = (char *) s;
  
  if (s == save)
  {
    err[0]=EDOM;
    return 0L;
  }
  
  if (negative)
  {
    if (res  > (ulonglong) LONGLONG_MIN)
      overflow = 1;
  }
  else if (res > (ulonglong) LONGLONG_MAX)
    overflow = 1;
  
  if (overflow)
  {
    err[0]=ERANGE;
    return negative ? LONGLONG_MIN : LONGLONG_MAX;
  }
  
  return (negative ? -((longlong)res) : (longlong)res);
}


Alexander Barkov's avatar
Alexander Barkov committed
421 422 423 424
static ulonglong
my_strntoull_mb2_or_mb4(CHARSET_INFO *cs,
                        const char *nptr, size_t l, int base,
                        char **endptr, int *err)
425
{
Alexander Barkov's avatar
Alexander Barkov committed
426
  int      negative= 0;
427 428 429 430 431 432 433
  int      overflow;
  int      cnv;
  my_wc_t  wc;
  register ulonglong    cutoff;
  register unsigned int cutlim;
  register ulonglong    res;
  register const uchar *s= (const uchar*) nptr;
Alexander Barkov's avatar
Alexander Barkov committed
434
  register const uchar *e= (const uchar*) nptr + l;
435 436 437 438 439
  const uchar *save;
  
  *err= 0;
  do
  {
Alexander Barkov's avatar
Alexander Barkov committed
440
    if ((cnv= cs->cset->mb_wc(cs,&wc,s,e)) > 0)
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    {
      switch (wc)
      {
        case ' ' : break;
        case '\t': break;
        case '-' : negative= !negative; break;
        case '+' : break;
        default  : goto bs;
      }
    } 
    else /* No more characters or bad multibyte sequence */
    {
      if (endptr !=NULL )
        *endptr = (char*)s;
      err[0]= (cnv==MY_CS_ILSEQ) ? EILSEQ : EDOM;
      return 0;
    } 
    s+=cnv;
  } while (1);
  
bs:

  overflow = 0;
  res = 0;
  save = s;
  cutoff = (~(ulonglong) 0) / (unsigned long int) base;
  cutlim = (uint) ((~(ulonglong) 0) % (unsigned long int) base);

  do
  {
471
    if ((cnv=cs->cset->mb_wc(cs,&wc,s,e))>0)
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    {
      s+=cnv;
      if ( wc>='0' && wc<='9')
        wc -= '0';
      else if ( wc>='A' && wc<='Z')
        wc = wc - 'A' + 10;
      else if ( wc>='a' && wc<='z')
        wc = wc - 'a' + 10;
      else
        break;
      if ((int)wc >= base)
        break;
      if (res > cutoff || (res == cutoff && wc > cutlim))
        overflow = 1;
      else
      {
        res *= (ulonglong) base;
        res += wc;
      }
    }
    else if (cnv==MY_CS_ILSEQ)
    {
      if (endptr !=NULL )
        *endptr = (char*)s;
      err[0]= EILSEQ;
      return 0;
    } 
    else
    {
      /* No more characters */
      break;
    }
  } while(1);
  
  if (endptr != NULL)
    *endptr = (char *) s;
  
  if (s == save)
  {
    err[0]= EDOM;
    return 0L;
  }
  
  if (overflow)
  {
    err[0]= ERANGE;
    return (~(ulonglong) 0);
  }

  return (negative ? -((longlong) res) : (longlong) res);
}

524

Alexander Barkov's avatar
Alexander Barkov committed
525 526 527 528
static double
my_strntod_mb2_or_mb4(CHARSET_INFO *cs,
                      char *nptr, size_t length, 
                      char **endptr, int *err)
529 530 531
{
  char     buf[256];
  double   res;
Alexander Barkov's avatar
Alexander Barkov committed
532
  register char *b= buf;
533
  register const uchar *s= (const uchar*) nptr;
534
  const uchar *end;
535
  my_wc_t  wc;
Alexander Barkov's avatar
Alexander Barkov committed
536
  int     cnv;
537 538 539 540

  *err= 0;
  /* Cut too long strings */
  if (length >= sizeof(buf))
Alexander Barkov's avatar
Alexander Barkov committed
541 542
    length= sizeof(buf) - 1;
  end= s + length;
serg@serg.mylan's avatar
serg@serg.mylan committed
543

Alexander Barkov's avatar
Alexander Barkov committed
544
  while ((cnv= cs->cset->mb_wc(cs,&wc,s,end)) > 0)
545
  {
Alexander Barkov's avatar
Alexander Barkov committed
546
    s+= cnv;
547
    if (wc > (int) (uchar) 'e' || !wc)
Alexander Barkov's avatar
Alexander Barkov committed
548
      break;                                        /* Can't be part of double */
549
    *b++= (char) wc;
550
  }
serg@serg.mylan's avatar
serg@serg.mylan committed
551

552 553
  *endptr= b;
  res= my_strtod(buf, endptr, err);
Alexander Barkov's avatar
Alexander Barkov committed
554
  *endptr= nptr + cs->mbminlen * (size_t) (*endptr - buf);
555 556 557 558
  return res;
}


Alexander Barkov's avatar
Alexander Barkov committed
559 560 561 562 563
static ulonglong
my_strntoull10rnd_mb2_or_mb4(CHARSET_INFO *cs,
                             const char *nptr, size_t length,
                             int unsign_fl,
                             char **endptr, int *err)
564
{
Alexander Barkov's avatar
Alexander Barkov committed
565
  char  buf[256], *b= buf;
566 567 568
  ulonglong res;
  const uchar *end, *s= (const uchar*) nptr;
  my_wc_t  wc;
Alexander Barkov's avatar
Alexander Barkov committed
569
  int     cnv;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

  /* Cut too long strings */
  if (length >= sizeof(buf))
    length= sizeof(buf)-1;
  end= s + length;

  while ((cnv= cs->cset->mb_wc(cs,&wc,s,end)) > 0)
  {
    s+= cnv;
    if (wc > (int) (uchar) 'e' || !wc)
      break;                            /* Can't be a number part */
    *b++= (char) wc;
  }

  res= my_strntoull10rnd_8bit(cs, buf, b - buf, unsign_fl, endptr, err);
Alexander Barkov's avatar
Alexander Barkov committed
585
  *endptr= (char*) nptr + cs->mbminlen * (size_t) (*endptr - buf);
586 587 588 589
  return res;
}


590 591 592 593
/*
  This is a fast version optimized for the case of radix 10 / -10
*/

Alexander Barkov's avatar
Alexander Barkov committed
594 595 596
static size_t
my_l10tostr_mb2_or_mb4(CHARSET_INFO *cs,
                       char *dst, size_t len, int radix, long int val)
597 598 599 600
{
  char buffer[66];
  register char *p, *db, *de;
  long int new_val;
Alexander Barkov's avatar
Alexander Barkov committed
601
  int  sl= 0;
602
  unsigned long int uval = (unsigned long int) val;
603
  
Alexander Barkov's avatar
Alexander Barkov committed
604 605
  p= &buffer[sizeof(buffer) - 1];
  *p= '\0';
606 607 608 609 610
  
  if (radix < 0)
  {
    if (val < 0)
    {
Alexander Barkov's avatar
Alexander Barkov committed
611
      sl= 1;
612 613
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
      uval  = (unsigned long int)0 - uval;
614 615 616
    }
  }
  
617 618
  new_val = (long) (uval / 10);
  *--p    = '0'+ (char) (uval - (unsigned long) new_val * 10);
Alexander Barkov's avatar
Alexander Barkov committed
619
  val= new_val;
620 621 622
  
  while (val != 0)
  {
Alexander Barkov's avatar
Alexander Barkov committed
623 624
    new_val= val / 10;
    *--p= '0' + (char) (val - new_val * 10);
625 626 627 628 629
    val= new_val;
  }
  
  if (sl)
  {
Alexander Barkov's avatar
Alexander Barkov committed
630
    *--p= '-';
631 632
  }
  
Alexander Barkov's avatar
Alexander Barkov committed
633
  for ( db= dst, de= dst + len ; (dst < de) && *p ; p++)
634
  {
Alexander Barkov's avatar
Alexander Barkov committed
635 636 637
    int cnvres= cs->cset->wc_mb(cs,(my_wc_t)p[0],(uchar*) dst, (uchar*) de);
    if (cnvres > 0)
      dst+= cnvres;
638 639 640
    else
      break;
  }
Alexander Barkov's avatar
Alexander Barkov committed
641
  return (int) (dst - db);
642 643
}

644

Alexander Barkov's avatar
Alexander Barkov committed
645 646 647
static size_t
my_ll10tostr_mb2_or_mb4(CHARSET_INFO *cs,
                        char *dst, size_t len, int radix, longlong val)
648 649 650 651
{
  char buffer[65];
  register char *p, *db, *de;
  long long_val;
Alexander Barkov's avatar
Alexander Barkov committed
652
  int sl= 0;
653
  ulonglong uval= (ulonglong) val;
654 655 656 657 658
  
  if (radix < 0)
  {
    if (val < 0)
    {
Alexander Barkov's avatar
Alexander Barkov committed
659
      sl= 1;
660 661
      /* Avoid integer overflow in (-val) for LONGLONG_MIN (BUG#31799). */
      uval = (ulonglong)0 - uval;
662 663 664
    }
  }
  
Alexander Barkov's avatar
Alexander Barkov committed
665
  p= &buffer[sizeof(buffer)-1];
666 667
  *p='\0';
  
668
  if (uval == 0)
669
  {
Alexander Barkov's avatar
Alexander Barkov committed
670
    *--p= '0';
671 672 673
    goto cnv;
  }
  
674
  while (uval > (ulonglong) LONG_MAX)
675
  {
676 677
    ulonglong quo= uval/(uint) 10;
    uint rem= (uint) (uval- quo* (uint) 10);
Alexander Barkov's avatar
Alexander Barkov committed
678
    *--p= '0' + rem;
679
    uval= quo;
680 681
  }
  
682
  long_val= (long) uval;
683 684 685
  while (long_val != 0)
  {
    long quo= long_val/10;
Alexander Barkov's avatar
Alexander Barkov committed
686
    *--p= (char) ('0' + (long_val - quo*10));
687 688 689 690 691 692
    long_val= quo;
  }
  
cnv:
  if (sl)
  {
Alexander Barkov's avatar
Alexander Barkov committed
693
    *--p= '-';
694 695
  }
  
Alexander Barkov's avatar
Alexander Barkov committed
696
  for ( db= dst, de= dst + len ; (dst < de) && *p ; p++)
697
  {
Alexander Barkov's avatar
Alexander Barkov committed
698 699 700
    int cnvres= cs->cset->wc_mb(cs, (my_wc_t) p[0], (uchar*) dst, (uchar*) de);
    if (cnvres > 0)
      dst+= cnvres;
701 702 703
    else
      break;
  }
Alexander Barkov's avatar
Alexander Barkov committed
704
  return (int) (dst -db);
705 706
}

Alexander Barkov's avatar
Alexander Barkov committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
#endif /* HAVE_CHARSET_mb2_or_mb4 */


#ifdef HAVE_CHARSET_mb2
static longlong
my_strtoll10_mb2(CHARSET_INFO *cs __attribute__((unused)),
                 const char *nptr, char **endptr, int *error)
{
  const char *s, *end, *start, *n_end, *true_end;
  uchar c;
  unsigned long i, j, k;
  ulonglong li;
  int negative;
  ulong cutoff, cutoff2, cutoff3;

  s= nptr;
  /* If fixed length string */
  if (endptr)
  {
    /* Make sure string length is even */
    end= s + ((*endptr - s) / 2) * 2;
    while (s < end && !s[0] && (s[1] == ' ' || s[1] == '\t'))
      s+= 2;
    if (s == end)
      goto no_conv;
  }
  else
  {
     /* We don't support null terminated strings in UCS2 */
     goto no_conv;
  }

  /* Check for a sign. */
  negative= 0;
  if (!s[0] && s[1] == '-')
  {
    *error= -1;                                        /* Mark as negative number */
    negative= 1;
    s+= 2;
    if (s == end)
      goto no_conv;
    cutoff=  MAX_NEGATIVE_NUMBER / LFACTOR2;
    cutoff2= (MAX_NEGATIVE_NUMBER % LFACTOR2) / 100;
    cutoff3=  MAX_NEGATIVE_NUMBER % 100;
  }
  else
  {
    *error= 0;
    if (!s[0] && s[1] == '+')
    {
      s+= 2;
      if (s == end)
        goto no_conv;
    }
    cutoff=  ULONGLONG_MAX / LFACTOR2;
    cutoff2= ULONGLONG_MAX % LFACTOR2 / 100;
    cutoff3=  ULONGLONG_MAX % 100;
  }

  /* Handle case where we have a lot of pre-zero */
  if (!s[0] && s[1] == '0')
  {
    i= 0;
    do
    {
      s+= 2;
      if (s == end)
        goto end_i;                                /* Return 0 */
    }
    while (!s[0] && s[1] == '0');
    n_end= s + 2 * INIT_CNT;
  }
  else
  {
    /* Read first digit to check that it's a valid number */
    if (s[0] || (c= (s[1]-'0')) > 9)
      goto no_conv;
    i= c;
    s+= 2;
    n_end= s + 2 * (INIT_CNT-1);
  }

  /* Handle first 9 digits and store them in i */
  if (n_end > end)
    n_end= end;
  for (; s != n_end ; s+= 2)
  {
    if (s[0] || (c= (s[1]-'0')) > 9)
      goto end_i;
    i= i*10+c;
  }
  if (s == end)
    goto end_i;

  /* Handle next 9 digits and store them in j */
  j= 0;
  start= s;                                /* Used to know how much to shift i */
  n_end= true_end= s + 2 * INIT_CNT;
  if (n_end > end)
    n_end= end;
  do
  {
    if (s[0] || (c= (s[1]-'0')) > 9)
      goto end_i_and_j;
    j= j*10+c;
    s+= 2;
  } while (s != n_end);
  if (s == end)
  {
    if (s != true_end)
      goto end_i_and_j;
    goto end3;
  }
  if (s[0] || (c= (s[1]-'0')) > 9)
    goto end3;

  /* Handle the next 1 or 2 digits and store them in k */
  k=c;
  s+= 2;
  if (s == end || s[0] || (c= (s[1]-'0')) > 9)
    goto end4;
  k= k*10+c;
  s+= 2;
  *endptr= (char*) s;

  /* number string should have ended here */
  if (s != end && !s[0] && (c= (s[1]-'0')) <= 9)
    goto overflow;

  /* Check that we didn't get an overflow with the last digit */
  if (i > cutoff || (i == cutoff && ((j > cutoff2 || j == cutoff2) &&
                                     k > cutoff3)))
    goto overflow;
  li=i*LFACTOR2+ (ulonglong) j*100 + k;
  return (longlong) li;

overflow:                                        /* *endptr is set here */
  *error= MY_ERRNO_ERANGE;
  return negative ? LONGLONG_MIN : (longlong) ULONGLONG_MAX;

end_i:
  *endptr= (char*) s;
  return (negative ? ((longlong) -(long) i) : (longlong) i);

end_i_and_j:
  li= (ulonglong) i * lfactor[(size_t) (s-start) / 2] + j;
  *endptr= (char*) s;
  return (negative ? -((longlong) li) : (longlong) li);

end3:
  li=(ulonglong) i*LFACTOR+ (ulonglong) j;
  *endptr= (char*) s;
  return (negative ? -((longlong) li) : (longlong) li);

end4:
  li=(ulonglong) i*LFACTOR1+ (ulonglong) j * 10 + k;
  *endptr= (char*) s;
  if (negative)
  {
   if (li > MAX_NEGATIVE_NUMBER)
     goto overflow;
   return -((longlong) li);
  }
  return (longlong) li;

no_conv:
  /* There was no number to convert.  */
  *error= MY_ERRNO_EDOM;
  *endptr= (char *) nptr;
  return 0;
}


static size_t
my_scan_mb2(CHARSET_INFO *cs __attribute__((unused)),
            const char *str, const char *end, int sequence_type)
{
  const char *str0= str;
  end--; /* for easier loop condition, because of two bytes per character */
  
  switch (sequence_type)
  {
  case MY_SEQ_SPACES:
    for ( ; str < end; str+= 2)
    {
      if (str[0] != '\0' || str[1] != ' ')
        break;
    }
    return (size_t) (str - str0);
  default:
    return 0;
  }
}


static void
my_fill_mb2(CHARSET_INFO *cs __attribute__((unused)),
            char *s, size_t l, int fill)
{
906 907
  DBUG_ASSERT(fill <= 0xFFFF);
  for ( ; l >= 2; s[0]= (fill >> 8), s[1]= (fill & 0xFF), s+= 2, l-= 2);
Alexander Barkov's avatar
Alexander Barkov committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
}


static int
my_vsnprintf_mb2(char *dst, size_t n, const char* fmt, va_list ap)
{
  char *start=dst, *end= dst + n - 1;
  for (; *fmt ; fmt++)
  {
    if (fmt[0] != '%')
    {
      if (dst == end)                     /* End of buffer */
        break;
      
      *dst++='\0';
      *dst++= *fmt;          /* Copy ordinary char */
      continue;
    }
    
    fmt++;
    
    /* Skip if max size is used (to be compatible with printf) */
    while ( (*fmt >= '0' && *fmt <= '9') || *fmt == '.' || *fmt == '-')
      fmt++;
    
    if (*fmt == 'l')
      fmt++;
    
    if (*fmt == 's')                      /* String parameter */
    {
      char *par= va_arg(ap, char *);
      size_t plen;
      size_t left_len= (size_t)(end-dst);
      if (!par)
        par= (char*) "(null)";
      plen= strlen(par);
      if (left_len <= plen * 2)
        plen = left_len / 2 - 1;

      for ( ; plen ; plen--, dst+=2, par++)
      {
        dst[0]= '\0';
        dst[1]= par[0];
      }
      continue;
    }
    else if (*fmt == 'd' || *fmt == 'u')  /* Integer parameter */
    {
      int iarg;
      char nbuf[16];
      char *pbuf= nbuf;
      
      if ((size_t) (end - dst) < 32)
        break;
      iarg= va_arg(ap, int);
      if (*fmt == 'd')
        int10_to_str((long) iarg, nbuf, -10);
      else
        int10_to_str((long) (uint) iarg, nbuf,10);

      for (; pbuf[0]; pbuf++)
      {
        *dst++= '\0';
        *dst++= *pbuf;
      }
      continue;
    }
    
    /* We come here on '%%', unknown code or too long parameter */
    if (dst == end)
      break;
    *dst++= '\0';
    *dst++= '%';                            /* % used as % or unknown code */
  }
  
  DBUG_ASSERT(dst <= end);
  *dst='\0';                                /* End of errmessage */
  return (size_t) (dst - start);
}


static size_t
my_snprintf_mb2(CHARSET_INFO *cs __attribute__((unused)),
                char* to, size_t n, const char* fmt, ...)
{
  va_list args;
  va_start(args,fmt);
  return my_vsnprintf_mb2(to, n, fmt, args);
}


static size_t
my_lengthsp_mb2(CHARSET_INFO *cs __attribute__((unused)),
                const char *ptr, size_t length)
{
  const char *end= ptr + length;
  while (end > ptr + 1 && end[-1] == ' ' && end[-2] == '\0')
    end-= 2;
  return (size_t) (end - ptr);
}

#endif /* HAVE_CHARSET_mb2*/




#ifdef HAVE_CHARSET_utf16

/*
  D800..DB7F - Non-provate surrogate high (896 pages)
  DB80..DBFF - Private surrogate high     (128 pages)
  DC00..DFFF - Surrogate low              (1024 codes in a page)
*/

#define MY_UTF16_HIGH_HEAD(x)  ((((uchar) (x)) & 0xFC) == 0xD8)
#define MY_UTF16_LOW_HEAD(x)   ((((uchar) (x)) & 0xFC) == 0xDC)
#define MY_UTF16_SURROGATE(x)  (((x) & 0xF800) == 0xD800)

static int
my_utf16_uni(CHARSET_INFO *cs __attribute__((unused)),
             my_wc_t *pwc, const uchar *s, const uchar *e)
{
  if (s + 2 > e)
    return MY_CS_TOOSMALL2;
  
  /*
    High bytes: 0xD[89AB] = B'110110??'
    Low bytes:  0xD[CDEF] = B'110111??'
    Surrogate mask:  0xFC = B'11111100'
  */

  if (MY_UTF16_HIGH_HEAD(*s)) /* Surrogate head */
  {
    if (s + 4 > e)
      return MY_CS_TOOSMALL4;

    if (!MY_UTF16_LOW_HEAD(s[2]))  /* Broken surrigate pair */
      return MY_CS_ILSEQ;

    /*
      s[0]= 110110??  (<< 18)
      s[1]= ????????  (<< 10)
      s[2]= 110111??  (<<  8)
      s[3]= ????????  (<<  0)
    */ 

    *pwc= ((s[0] & 3) << 18) + (s[1] << 10) +
          ((s[2] & 3) << 8) + s[3] + 0x10000;

    return 4;
  }

  if (MY_UTF16_LOW_HEAD(*s)) /* Low surrogate part without high part */
    return MY_CS_ILSEQ;
  
  *pwc= (s[0] << 8) + s[1];
  return 2;
}


static int
my_uni_utf16(CHARSET_INFO *cs __attribute__((unused)),
             my_wc_t wc, uchar *s, uchar *e)
{
  if (wc <= 0xFFFF)
  {
    if (s + 2 > e)
      return MY_CS_TOOSMALL2;
    if (MY_UTF16_SURROGATE(wc))
      return MY_CS_ILUNI;
    *s++= (uchar) (wc >> 8);
    *s= (uchar) (wc & 0xFF);
    return 2;
  }

  if (wc <= 0x10FFFF)
  {
    if (s + 4 > e)
      return MY_CS_TOOSMALL4;
    *s++= (uchar) ((wc-= 0x10000) >> 18) | 0xD8;
    *s++= (uchar) (wc >> 10) & 0xFF;
    *s++= (uchar) ((wc >> 8) & 3) | 0xDC;
    *s= (uchar) wc & 0xFF;
    return 4;
  }

  return MY_CS_ILUNI;
}


static inline void
my_tolower_utf16(MY_UNICASE_INFO **uni_plane, my_wc_t *wc)
{
  int page= *wc >> 8;
1102
  if (page < 256 && *wc <= MY_CS_MAX_CHAR && uni_plane[page])
Alexander Barkov's avatar
Alexander Barkov committed
1103 1104 1105 1106 1107 1108 1109 1110
    *wc= uni_plane[page][*wc & 0xFF].tolower;
}


static inline void
my_toupper_utf16(MY_UNICASE_INFO **uni_plane, my_wc_t *wc)
{
  int page= *wc >> 8;
1111
  if (page < 256 && *wc <= MY_CS_MAX_CHAR && uni_plane[page])
Alexander Barkov's avatar
Alexander Barkov committed
1112 1113 1114 1115 1116 1117 1118 1119
    *wc= uni_plane[page][*wc & 0xFF].toupper;
}


static inline void
my_tosort_utf16(MY_UNICASE_INFO **uni_plane, my_wc_t *wc)
{
  int page= *wc >> 8;
1120
  if (page < 256 && *wc <= MY_CS_MAX_CHAR)
Alexander Barkov's avatar
Alexander Barkov committed
1121 1122 1123 1124 1125 1126
  {
    if (uni_plane[page])
      *wc= uni_plane[page][*wc & 0xFF].sort;
  }
  else
  {
Alexander Barkov's avatar
Alexander Barkov committed
1127
    *wc= MY_CS_REPLACEMENT_CHARACTER;
Alexander Barkov's avatar
Alexander Barkov committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  }
}


static size_t
my_caseup_utf16(CHARSET_INFO *cs, char *src, size_t srclen,
                char *dst __attribute__((unused)),
                size_t dstlen __attribute__((unused)))
{
  my_wc_t wc;
  int res;
  char *srcend= src + srclen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  DBUG_ASSERT(src == dst && srclen == dstlen);
  
  while ((src < srcend) &&
         (res= my_utf16_uni(cs, &wc, (uchar *)src, (uchar*) srcend)) > 0)
  {
    my_toupper_utf16(uni_plane, &wc);
    if (res != my_uni_utf16(cs, wc, (uchar*) src, (uchar*) srcend))
      break;
    src+= res;
  }
  return srclen;
}


static void
my_hash_sort_utf16(CHARSET_INFO *cs, const uchar *s, size_t slen,
                   ulong *n1, ulong *n2)
{
  my_wc_t wc;
  int res;
  const uchar *e= s+slen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  while (e > s + 1 && e[-1] == ' ' && e[-2] == '\0')
    e-= 2;

  while ((s < e) && (res= my_utf16_uni(cs, &wc, (uchar *)s, (uchar*)e)) > 0)
  {
    my_tosort_utf16(uni_plane, &wc);
    n1[0]^= (((n1[0] & 63) + n2[0]) * (wc & 0xFF)) + (n1[0] << 8);
    n2[0]+= 3;
    n1[0]^= (((n1[0] & 63) + n2[0]) * (wc >> 8)) + (n1[0] << 8);
    n2[0]+= 3;
    s+= res;
  }
}


static size_t
my_casedn_utf16(CHARSET_INFO *cs, char *src, size_t srclen,
                char *dst __attribute__((unused)),
                size_t dstlen __attribute__((unused)))
{
  my_wc_t wc;
  int res;
  char *srcend= src + srclen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  DBUG_ASSERT(src == dst && srclen == dstlen);

  while ((src < srcend) &&
         (res= my_utf16_uni(cs, &wc, (uchar*) src, (uchar*) srcend)) > 0)
  {
    my_tolower_utf16(uni_plane, &wc);
    if (res != my_uni_utf16(cs, wc, (uchar*) src, (uchar*) srcend))
      break;
    src+= res;
  }
  return srclen;
}


static int
my_strnncoll_utf16(CHARSET_INFO *cs, 
                   const uchar *s, size_t slen, 
                   const uchar *t, size_t tlen,
                   my_bool t_is_prefix)
{
  int s_res, t_res;
1209
  my_wc_t UNINIT_VAR(s_wc), UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
  const uchar *se= s + slen;
  const uchar *te= t + tlen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  while (s < se && t < te)
  {
    s_res= my_utf16_uni(cs, &s_wc, s, se);
    t_res= my_utf16_uni(cs, &t_wc, t, te);

    if (s_res <= 0 || t_res <= 0)
    {
      /* Incorrect string, compare by char value */
      return my_bincmp(s, se, t, te);
    }

    my_tosort_utf16(uni_plane, &s_wc);
    my_tosort_utf16(uni_plane, &t_wc);

    if (s_wc != t_wc)
    {
      return  s_wc > t_wc ? 1 : -1;
    }

    s+= s_res;
    t+= t_res;
  }
  return (int) (t_is_prefix ? (t - te) : ((se - s) - (te - t)));
}


/**
  Compare strings, discarding end space

  If one string is shorter as the other, then we space extend the other
  so that the strings have equal length.

  This will ensure that the following things hold:

    "a"  == "a "
    "a\0" < "a"
    "a\0" < "a "

  @param  cs        Character set pinter.
  @param  a         First string to compare.
  @param  a_length  Length of 'a'.
  @param  b         Second string to compare.
  @param  b_length  Length of 'b'.

  IMPLEMENTATION

  @return Comparison result.
    @retval Negative number, if a less than b.
    @retval 0, if a is equal to b
    @retval Positive number, if a > b
*/

static int
my_strnncollsp_utf16(CHARSET_INFO *cs,
                     const uchar *s, size_t slen,
                     const uchar *t, size_t tlen,
                     my_bool diff_if_only_endspace_difference)
{
  int res;
1273
  my_wc_t UNINIT_VAR(s_wc), UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
  const uchar *se= s + slen, *te= t + tlen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  DBUG_ASSERT((slen % 2) == 0);
  DBUG_ASSERT((tlen % 2) == 0);

#ifndef VARCHAR_WITH_DIFF_ENDSPACE_ARE_DIFFERENT_FOR_UNIQUE
  diff_if_only_endspace_difference= FALSE;
#endif

  while (s < se && t < te)
  {
    int s_res= my_utf16_uni(cs, &s_wc, s, se);
    int t_res= my_utf16_uni(cs, &t_wc, t, te);

    if (s_res <= 0 || t_res <= 0)
    {
      /* Incorrect string, compare bytewise */
      return my_bincmp(s, se, t, te);
    }

    my_tosort_utf16(uni_plane, &s_wc);
    my_tosort_utf16(uni_plane, &t_wc);
    
    if (s_wc != t_wc)
    {
      return s_wc > t_wc ? 1 : -1;
    }

    s+= s_res;
    t+= t_res;
  }

  slen= (size_t) (se - s);
  tlen= (size_t) (te - t);
  res= 0;

  if (slen != tlen)
  {
    int s_res, swap= 1;
    if (diff_if_only_endspace_difference)
      res= 1;                                   /* Assume 's' is bigger */
    if (slen < tlen)
    {
      slen= tlen;
      s= t;
      se= te;
      swap= -1;
      res= -res;
    }

    for ( ; s < se; s+= s_res)
    {
      if ((s_res= my_utf16_uni(cs, &s_wc, s, se)) < 0)
      {
        DBUG_ASSERT(0);
        return 0;
      }
      if (s_wc != ' ')
        return (s_wc < ' ') ? -swap : swap;
    }
  }
  return res;
}


static uint
my_ismbchar_utf16(CHARSET_INFO *cs __attribute__((unused)),
                  const char *b __attribute__((unused)),
                  const char *e __attribute__((unused)))
{
  if (b + 2 > e)
    return 0;
  
  if (MY_UTF16_HIGH_HEAD(*b))
  {
    return (b + 4 <= e) && MY_UTF16_LOW_HEAD(b[2]) ? 4 : 0;
  }
  
  if (MY_UTF16_LOW_HEAD(*b))
    return 0;
  
  return 2;
}


static uint
my_mbcharlen_utf16(CHARSET_INFO *cs  __attribute__((unused)),
                   uint c __attribute__((unused)))
{
  return MY_UTF16_HIGH_HEAD(c) ? 4 : 2;
}


static size_t
my_numchars_utf16(CHARSET_INFO *cs,
                  const char *b, const char *e)
{
  size_t nchars= 0;
  for ( ; ; nchars++)
  {
    size_t charlen= my_ismbchar_utf16(cs, b, e);
    if (!charlen)
      break;
    b+= charlen;
  }
  return nchars;
}


static size_t
my_charpos_utf16(CHARSET_INFO *cs,
                 const char *b, const char *e, size_t pos)
{
  const char *b0= b;
  uint charlen;
  
  for ( ; pos; b+= charlen, pos--)
  {
    if (!(charlen= my_ismbchar(cs, b, e)))
      return (e + 2 - b0); /* Error, return pos outside the string */
  }
  return (size_t) (pos ? (e + 2 - b0) : (b - b0));
}


static size_t
my_well_formed_len_utf16(CHARSET_INFO *cs,
                         const char *b, const char *e,
                         size_t nchars, int *error)
{
  const char *b0= b;
  uint charlen;
  *error= 0;
  
  for ( ; nchars; b+= charlen, nchars--)
  {
    if (!(charlen= my_ismbchar(cs, b, e)))
    {
      *error= b < e ? 1 : 0;
      break;
    }
  }
  return (size_t) (b - b0);
}


static int
my_wildcmp_utf16_ci(CHARSET_INFO *cs,
                    const char *str,const char *str_end,
                    const char *wildstr,const char *wildend,
                    int escape, int w_one, int w_many)
{
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  return my_wildcmp_unicode(cs, str, str_end, wildstr, wildend,
                            escape, w_one, w_many, uni_plane); 
}


static int
my_wildcmp_utf16_bin(CHARSET_INFO *cs,
                     const char *str,const char *str_end,
                     const char *wildstr,const char *wildend,
                     int escape, int w_one, int w_many)
{
  return my_wildcmp_unicode(cs, str, str_end, wildstr, wildend,
                            escape, w_one, w_many, NULL); 
}


static int
my_strnncoll_utf16_bin(CHARSET_INFO *cs, 
                       const uchar *s, size_t slen,
                       const uchar *t, size_t tlen,
                       my_bool t_is_prefix)
{
  int s_res,t_res;
1451
  my_wc_t UNINIT_VAR(s_wc), UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
1452 1453 1454 1455 1456 1457 1458
  const uchar *se=s+slen;
  const uchar *te=t+tlen;

  while ( s < se && t < te )
  {
    s_res= my_utf16_uni(cs,&s_wc, s, se);
    t_res= my_utf16_uni(cs,&t_wc, t, te);
1459

Alexander Barkov's avatar
Alexander Barkov committed
1460 1461 1462 1463 1464 1465 1466
    if (s_res <= 0 || t_res <= 0)
    {
      /* Incorrect string, compare by char value */
      return my_bincmp(s, se, t, te);
    }
    if (s_wc != t_wc)
    {
1467
      return s_wc > t_wc ? 1 : -1;
Alexander Barkov's avatar
Alexander Barkov committed
1468
    }
1469

Alexander Barkov's avatar
Alexander Barkov committed
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
    s+= s_res;
    t+= t_res;
  }
  return (int) (t_is_prefix ? (t - te) : ((se - s) - (te - t)));
}


static int
my_strnncollsp_utf16_bin(CHARSET_INFO *cs,
                         const uchar *s, size_t slen,
                         const uchar *t, size_t tlen,
                         my_bool diff_if_only_endspace_difference)
{
  int res;
1484
  my_wc_t UNINIT_VAR(s_wc), UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
  const uchar *se= s + slen, *te= t + tlen;

  DBUG_ASSERT((slen % 2) == 0);
  DBUG_ASSERT((tlen % 2) == 0);

#ifndef VARCHAR_WITH_DIFF_ENDSPACE_ARE_DIFFERENT_FOR_UNIQUE
  diff_if_only_endspace_difference= FALSE;
#endif

  while (s < se && t < te)
  {
    int s_res= my_utf16_uni(cs, &s_wc, s, se);
    int t_res= my_utf16_uni(cs, &t_wc, t, te);

    if (s_res <= 0 || t_res <= 0)
    {
      /* Incorrect string, compare bytewise */
      return my_bincmp(s, se, t, te);
    }

    if (s_wc != t_wc)
    {
1507
      return s_wc > t_wc ? 1 : -1;
Alexander Barkov's avatar
Alexander Barkov committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    }

    s+= s_res;
    t+= t_res;
  }

  slen= (size_t) (se - s);
  tlen= (size_t) (te - t);
  res= 0;

  if (slen != tlen)
  {
    int s_res, swap= 1;
    if (diff_if_only_endspace_difference)
      res= 1;                                   /* Assume 's' is bigger */
    if (slen < tlen)
    {
      slen= tlen;
      s= t;
      se= te;
      swap= -1;
      res= -res;
    }

    for ( ; s < se; s+= s_res)
    {
      if ((s_res= my_utf16_uni(cs, &s_wc, s, se)) < 0)
      {
        DBUG_ASSERT(0);
        return 0;
      }
      if (s_wc != ' ')
        return (s_wc < ' ') ? -swap : swap;
    }
  }
  return res;
}


static void
my_hash_sort_utf16_bin(CHARSET_INFO *cs __attribute__((unused)),
                       const uchar *key, size_t len,ulong *nr1, ulong *nr2)
{
  const uchar *pos = key;
  
  key+= len;

  while (key > pos + 1 && key[-1] == ' ' && key[-2] == '\0')
    key-= 2;

  for (; pos < (uchar*) key ; pos++)
  {
    nr1[0]^= (ulong) ((((uint) nr1[0] & 63) + nr2[0]) * 
              ((uint)*pos)) + (nr1[0] << 8);
    nr2[0]+= 3;
  }
}


static MY_COLLATION_HANDLER my_collation_utf16_general_ci_handler =
{
  NULL,                /* init */
  my_strnncoll_utf16,
  my_strnncollsp_utf16,
  my_strnxfrm_unicode,
  my_strnxfrmlen_simple,
1574
  my_like_range_generic,
Alexander Barkov's avatar
Alexander Barkov committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
  my_wildcmp_utf16_ci,
  my_strcasecmp_mb2_or_mb4,
  my_instr_mb,
  my_hash_sort_utf16,
  my_propagate_simple
};


static MY_COLLATION_HANDLER my_collation_utf16_bin_handler =
{
  NULL,                /* init */
  my_strnncoll_utf16_bin,
  my_strnncollsp_utf16_bin,
1588 1589
  my_strnxfrm_unicode_full_bin,
  my_strnxfrmlen_unicode_full_bin,
1590
  my_like_range_generic,
Alexander Barkov's avatar
Alexander Barkov committed
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
  my_wildcmp_utf16_bin,
  my_strcasecmp_mb2_or_mb4,
  my_instr_mb,
  my_hash_sort_utf16_bin,
  my_propagate_simple
};


MY_CHARSET_HANDLER my_charset_utf16_handler=
{
  NULL,                /* init         */
  my_ismbchar_utf16,   /* ismbchar     */
  my_mbcharlen_utf16,  /* mbcharlen    */
  my_numchars_utf16,
  my_charpos_utf16,
  my_well_formed_len_utf16,
  my_lengthsp_mb2,
  my_numcells_mb,
  my_utf16_uni,        /* mb_wc        */
  my_uni_utf16,        /* wc_mb        */
  my_mb_ctype_mb,
  my_caseup_str_mb2_or_mb4,
  my_casedn_str_mb2_or_mb4,
  my_caseup_utf16,
  my_casedn_utf16,
  my_snprintf_mb2,
  my_l10tostr_mb2_or_mb4,
  my_ll10tostr_mb2_or_mb4,
  my_fill_mb2,
  my_strntol_mb2_or_mb4,
  my_strntoul_mb2_or_mb4,
  my_strntoll_mb2_or_mb4,
  my_strntoull_mb2_or_mb4,
  my_strntod_mb2_or_mb4,
  my_strtoll10_mb2,
  my_strntoull10rnd_mb2_or_mb4,
  my_scan_mb2
};


CHARSET_INFO my_charset_utf16_general_ci=
{
  54,0,0,              /* number       */
  MY_CS_COMPILED|MY_CS_PRIMARY|MY_CS_STRNXFRM|MY_CS_UNICODE|MY_CS_NONASCII,
  "utf16",             /* cs name    */
  "utf16_general_ci",  /* name         */
  "UTF-16 Unicode",    /* comment      */
  NULL,                /* tailoring    */
  NULL,                /* ctype        */
  NULL,                /* to_lower     */
  NULL,                /* to_upper     */
  NULL,                /* sort_order   */
  NULL,                /* contractions */
  NULL,                /* sort_order_big*/
  NULL,                /* tab_to_uni   */
  NULL,                /* tab_from_uni */
  my_unicase_default,  /* caseinfo     */
  NULL,                /* state_map    */
  NULL,                /* ident_map    */
  1,                   /* strxfrm_multiply */
  1,                   /* caseup_multiply  */
  1,                   /* casedn_multiply  */
  2,                   /* mbminlen     */
  4,                   /* mbmaxlen     */
  0,                   /* min_sort_char */
  0xFFFF,              /* max_sort_char */
  ' ',                 /* pad char      */
  0,                   /* escape_with_backslash_is_dangerous */
  &my_charset_utf16_handler,
  &my_collation_utf16_general_ci_handler
};


CHARSET_INFO my_charset_utf16_bin=
{
  55,0,0,              /* number       */
1667
  MY_CS_COMPILED|MY_CS_BINSORT|MY_CS_STRNXFRM|MY_CS_UNICODE|MY_CS_NONASCII,
Alexander Barkov's avatar
Alexander Barkov committed
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
  "utf16",             /* cs name      */
  "utf16_bin",         /* name         */
  "UTF-16 Unicode",    /* comment      */
  NULL,                /* tailoring    */
  NULL,                /* ctype        */
  NULL,                /* to_lower     */
  NULL,                /* to_upper     */
  NULL,                /* sort_order   */
  NULL,                /* contractions */
  NULL,                /* sort_order_big*/
  NULL,                /* tab_to_uni   */
  NULL,                /* tab_from_uni */
  my_unicase_default,  /* caseinfo     */
  NULL,                /* state_map    */
  NULL,                /* ident_map    */
  1,                   /* strxfrm_multiply */
  1,                   /* caseup_multiply  */
  1,                   /* casedn_multiply  */
  2,                   /* mbminlen     */
  4,                   /* mbmaxlen     */
  0,                   /* min_sort_char */
  0xFFFF,              /* max_sort_char */
  ' ',                 /* pad char      */
  0,                   /* escape_with_backslash_is_dangerous */
  &my_charset_utf16_handler,
  &my_collation_utf16_bin_handler
};

#endif /* HAVE_CHARSET_utf16 */


#ifdef HAVE_CHARSET_utf32

static int
my_utf32_uni(CHARSET_INFO *cs __attribute__((unused)),
             my_wc_t *pwc, const uchar *s, const uchar *e)
{
  if (s + 4 > e)
    return MY_CS_TOOSMALL4;
  *pwc= (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + (s[3]);
  return 4;
}


static int
my_uni_utf32(CHARSET_INFO *cs __attribute__((unused)),
             my_wc_t wc, uchar *s, uchar *e)
{
  if (s + 4 > e) 
    return MY_CS_TOOSMALL4;
  
  s[0]= (uchar) (wc >> 24);
  s[1]= (uchar) (wc >> 16) & 0xFF;
  s[2]= (uchar) (wc >> 8)  & 0xFF;
  s[3]= (uchar) wc & 0xFF;
  return 4;
}


static inline void
my_tolower_utf32(MY_UNICASE_INFO **uni_plane, my_wc_t *wc)
{
  int page= *wc >> 8;
1731
  if (page < 256 && *wc <= MY_CS_MAX_CHAR && uni_plane[page])
Alexander Barkov's avatar
Alexander Barkov committed
1732 1733 1734 1735 1736 1737 1738 1739
    *wc= uni_plane[page][*wc & 0xFF].tolower;
}


static inline void
my_toupper_utf32(MY_UNICASE_INFO **uni_plane, my_wc_t *wc)
{
  int page= *wc >> 8;
1740
  if (page < 256 && *wc <= MY_CS_MAX_CHAR && uni_plane[page])
Alexander Barkov's avatar
Alexander Barkov committed
1741 1742 1743 1744 1745 1746 1747 1748
    *wc= uni_plane[page][*wc & 0xFF].toupper;
}


static inline void
my_tosort_utf32(MY_UNICASE_INFO **uni_plane, my_wc_t *wc)
{
  int page= *wc >> 8;
1749
  if (page < 256 && *wc <= MY_CS_MAX_CHAR)
Alexander Barkov's avatar
Alexander Barkov committed
1750 1751 1752 1753 1754 1755
  {
    if (uni_plane[page])
      *wc= uni_plane[page][*wc & 0xFF].sort;
  }
  else
  {
Alexander Barkov's avatar
Alexander Barkov committed
1756
    *wc= MY_CS_REPLACEMENT_CHARACTER;
Alexander Barkov's avatar
Alexander Barkov committed
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
  }
}


static size_t
my_caseup_utf32(CHARSET_INFO *cs, char *src, size_t srclen,
                char *dst __attribute__((unused)),
                size_t dstlen __attribute__((unused)))
{
  my_wc_t wc;
  int res;
  char *srcend= src + srclen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  DBUG_ASSERT(src == dst && srclen == dstlen);
  
  while ((src < srcend) &&
         (res= my_utf32_uni(cs, &wc, (uchar *)src, (uchar*) srcend)) > 0)
  {
    my_toupper_utf32(uni_plane, &wc);
    if (res != my_uni_utf32(cs, wc, (uchar*) src, (uchar*) srcend))
      break;
    src+= res;
  }
  return srclen;
}


static inline void
my_hash_add(ulong *n1, ulong *n2, uint ch)
{
  n1[0]^= (((n1[0] & 63) + n2[0]) * (ch)) + (n1[0] << 8);
  n2[0]+= 3;
}


static void
my_hash_sort_utf32(CHARSET_INFO *cs, const uchar *s, size_t slen,
                   ulong *n1, ulong *n2)
{
  my_wc_t wc;
  int res;
  const uchar *e= s + slen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  /* Skip trailing spaces */
  while (e > s + 3 && e[-1] == ' ' && !e[-2] && !e[-3] && !e[-4])
    e-= 4;

  while ((res= my_utf32_uni(cs, &wc, (uchar*) s, (uchar*) e)) > 0)
  {
    my_tosort_utf32(uni_plane, &wc);
    my_hash_add(n1, n2, (uint) (wc >> 24));
    my_hash_add(n1, n2, (uint) (wc >> 16) & 0xFF);
    my_hash_add(n1, n2, (uint) (wc >> 8)  & 0xFF);
    my_hash_add(n1, n2, (uint) (wc & 0xFF));
    s+= res;
  }
}


static size_t
my_casedn_utf32(CHARSET_INFO *cs, char *src, size_t srclen,
                char *dst __attribute__((unused)),
                size_t dstlen __attribute__((unused)))
{
  my_wc_t wc;
  int res;
  char *srcend= src + srclen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  DBUG_ASSERT(src == dst && srclen == dstlen);

  while ((res= my_utf32_uni(cs, &wc, (uchar*) src, (uchar*) srcend)) > 0)
  {
    my_tolower_utf32(uni_plane,&wc);
    if (res != my_uni_utf32(cs, wc, (uchar*) src, (uchar*) srcend))
      break;
    src+= res;
  }
  return srclen;
}


static int
my_strnncoll_utf32(CHARSET_INFO *cs, 
                   const uchar *s, size_t slen, 
                   const uchar *t, size_t tlen,
                   my_bool t_is_prefix)
{
1845
  my_wc_t UNINIT_VAR(s_wc),UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
  const uchar *se= s + slen;
  const uchar *te= t + tlen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  while (s < se && t < te)
  {
    int s_res= my_utf32_uni(cs, &s_wc, s, se);
    int t_res= my_utf32_uni(cs, &t_wc, t, te);
    
    if ( s_res <= 0 || t_res <= 0)
    {
      /* Incorrect string, compare by char value */
      return my_bincmp(s, se, t, te);
    }
    
    my_tosort_utf32(uni_plane, &s_wc);
    my_tosort_utf32(uni_plane, &t_wc);
    
    if (s_wc != t_wc)
    {
      return s_wc > t_wc ? 1 : -1;
    }
    
    s+= s_res;
    t+= t_res;
  }
  return (int) (t_is_prefix ? (t - te) : ((se - s) - (te - t)));
}


/**
  Compare strings, discarding end space

  If one string is shorter as the other, then we space extend the other
  so that the strings have equal length.

  This will ensure that the following things hold:

    "a"  == "a "
    "a\0" < "a"
    "a\0" < "a "

  @param  cs        Character set pinter.
  @param  a         First string to compare.
  @param  a_length  Length of 'a'.
  @param  b         Second string to compare.
  @param  b_length  Length of 'b'.

  IMPLEMENTATION

  @return Comparison result.
    @retval Negative number, if a less than b.
    @retval 0, if a is equal to b
    @retval Positive number, if a > b
*/


static int
my_strnncollsp_utf32(CHARSET_INFO *cs,
                     const uchar *s, size_t slen,
                     const uchar *t, size_t tlen,
                     my_bool diff_if_only_endspace_difference)
{
  int res;
1910
  my_wc_t UNINIT_VAR(s_wc), UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
  const uchar *se= s + slen, *te= t + tlen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  DBUG_ASSERT((slen % 4) == 0);
  DBUG_ASSERT((tlen % 4) == 0);

#ifndef VARCHAR_WITH_DIFF_ENDSPACE_ARE_DIFFERENT_FOR_UNIQUE
  diff_if_only_endspace_difference= FALSE;
#endif

  while ( s < se && t < te )
  {
    int s_res= my_utf32_uni(cs, &s_wc, s, se);
    int t_res= my_utf32_uni(cs, &t_wc, t, te);

    if ( s_res <= 0 || t_res <= 0 )
    {
      /* Incorrect string, compare bytewise */
      return my_bincmp(s, se, t, te);
    }

    my_tosort_utf32(uni_plane, &s_wc);
    my_tosort_utf32(uni_plane, &t_wc);
    
    if ( s_wc != t_wc )
    {
      return s_wc > t_wc ? 1 : -1;
    }

    s+= s_res;
    t+= t_res;
  }

  slen= (size_t) (se - s);
  tlen= (size_t) (te - t);
  res= 0;

  if (slen != tlen)
  {
    int s_res, swap= 1;
    if (diff_if_only_endspace_difference)
      res= 1;                                   /* Assume 's' is bigger */
    if (slen < tlen)
    {
      slen= tlen;
      s= t;
      se= te;
      swap= -1;
      res= -res;
    }

    for ( ; s < se; s+= s_res)
    {
      if ((s_res= my_utf32_uni(cs, &s_wc, s, se)) < 0)
      {
        DBUG_ASSERT(0);
        return 0;
      }
      if (s_wc != ' ')
        return (s_wc < ' ') ? -swap : swap;
    }
  }
  return res;
}


static size_t
my_strnxfrmlen_utf32(CHARSET_INFO *cs __attribute__((unused)), size_t len)
{
  return len / 2;
}


static uint
my_ismbchar_utf32(CHARSET_INFO *cs __attribute__((unused)),
                  const char *b __attribute__((unused)),
                  const char *e __attribute__((unused)))
{
  return 4;
}


static uint
my_mbcharlen_utf32(CHARSET_INFO *cs  __attribute__((unused)) , 
                   uint c __attribute__((unused)))
{
  return 4;
}


static int
my_vsnprintf_utf32(char *dst, size_t n, const char* fmt, va_list ap)
{
  char *start= dst, *end= dst + n;
  DBUG_ASSERT((n % 4) == 0);
  for (; *fmt ; fmt++)
  {
    if (fmt[0] != '%')
    {
      if (dst >= end)                        /* End of buffer */
        break;
      
      *dst++= '\0';
      *dst++= '\0';
      *dst++= '\0';
      *dst++= *fmt;        /* Copy ordinary char */
      continue;
    }
    
    fmt++;
    
    /* Skip if max size is used (to be compatible with printf) */
    while ( (*fmt>='0' && *fmt<='9') || *fmt == '.' || *fmt == '-')
      fmt++;
    
    if (*fmt == 'l')
      fmt++;
    
    if (*fmt == 's')                                /* String parameter */
    {
      reg2 char *par= va_arg(ap, char *);
      size_t plen;
      size_t left_len= (size_t)(end - dst);
      if (!par) par= (char*)"(null)";
      plen= strlen(par);
      if (left_len <= plen*4)
        plen= left_len / 4 - 1;

      for ( ; plen ; plen--, dst+= 4, par++)
      {
        dst[0]= '\0';
        dst[1]= '\0';
        dst[2]= '\0';
        dst[3]= par[0];
      }
      continue;
    }
    else if (*fmt == 'd' || *fmt == 'u')        /* Integer parameter */
    {
      register int iarg;
      char nbuf[16];
      char *pbuf= nbuf;
      
      if ((size_t) (end - dst) < 64)
        break;
      iarg= va_arg(ap, int);
      if (*fmt == 'd')
        int10_to_str((long) iarg, nbuf, -10);
      else
        int10_to_str((long) (uint) iarg,nbuf,10);

      for (; pbuf[0]; pbuf++)
      {
        *dst++= '\0';
        *dst++= '\0';
        *dst++= '\0';
        *dst++= *pbuf;
      }
      continue;
    }
    
    /* We come here on '%%', unknown code or too long parameter */
    if (dst == end)
      break;
    *dst++= '\0';
    *dst++= '\0';
    *dst++= '\0';
    *dst++= '%';    /* % used as % or unknown code */
  }
  
  DBUG_ASSERT(dst < end);
  *dst++= '\0';
  *dst++= '\0';
  *dst++= '\0';
  *dst++= '\0';     /* End of errmessage */
  return (size_t) (dst - start - 4);
}


static size_t
my_snprintf_utf32(CHARSET_INFO *cs __attribute__((unused)),
                  char* to, size_t n, const char* fmt, ...)
{
  va_list args;
  va_start(args,fmt);
  return my_vsnprintf_utf32(to, n, fmt, args);
}


static longlong
my_strtoll10_utf32(CHARSET_INFO *cs __attribute__((unused)),
                   const char *nptr, char **endptr, int *error)
{
  const char *s, *end, *start, *n_end, *true_end;
  uchar c;
  unsigned long i, j, k;
  ulonglong li;
  int negative;
  ulong cutoff, cutoff2, cutoff3;

  s= nptr;
  /* If fixed length string */
  if (endptr)
  {
    /* Make sure string length is even */
    end= s + ((*endptr - s) / 4) * 4;
    while (s < end && !s[0] && !s[1] && !s[2] &&
           (s[3] == ' ' || s[3] == '\t'))
      s+= 4;
    if (s == end)
      goto no_conv;
  }
  else
  {
     /* We don't support null terminated strings in UCS2 */
     goto no_conv;
  }

  /* Check for a sign. */
  negative= 0;
  if (!s[0] && !s[1] && !s[2] && s[3] == '-')
  {
    *error= -1;                                        /* Mark as negative number */
    negative= 1;
    s+= 4;
    if (s == end)
      goto no_conv;
    cutoff=  MAX_NEGATIVE_NUMBER / LFACTOR2;
    cutoff2= (MAX_NEGATIVE_NUMBER % LFACTOR2) / 100;
    cutoff3=  MAX_NEGATIVE_NUMBER % 100;
  }
  else
  {
    *error= 0;
    if (!s[0] && !s[1] && !s[2] && s[3] == '+')
    {
      s+= 4;
      if (s == end)
        goto no_conv;
    }
    cutoff=  ULONGLONG_MAX / LFACTOR2;
    cutoff2= ULONGLONG_MAX % LFACTOR2 / 100;
    cutoff3=  ULONGLONG_MAX % 100;
  }

  /* Handle case where we have a lot of pre-zero */
  if (!s[0] && !s[1] && !s[2] && s[3] == '0')
  {
    i= 0;
    do
    {
      s+= 4;
      if (s == end)
        goto end_i;                                /* Return 0 */
    }
    while (!s[0] && !s[1] && !s[2] && s[3] == '0');
    n_end= s + 4 * INIT_CNT;
  }
  else
  {
    /* Read first digit to check that it's a valid number */
    if (s[0] || s[1] || s[2] || (c= (s[3]-'0')) > 9)
      goto no_conv;
    i= c;
    s+= 4;
    n_end= s + 4 * (INIT_CNT-1);
  }

  /* Handle first 9 digits and store them in i */
  if (n_end > end)
    n_end= end;
  for (; s != n_end ; s+= 4)
  {
    if (s[0] || s[1] || s[2] || (c= (s[3] - '0')) > 9)
      goto end_i;
    i= i * 10 + c;
  }
  if (s == end)
    goto end_i;

  /* Handle next 9 digits and store them in j */
  j= 0;
  start= s;                                /* Used to know how much to shift i */
  n_end= true_end= s + 4 * INIT_CNT;
  if (n_end > end)
    n_end= end;
  do
  {
    if (s[0] || s[1] || s[2] || (c= (s[3] - '0')) > 9)
      goto end_i_and_j;
    j= j * 10 + c;
    s+= 4;
  } while (s != n_end);
  if (s == end)
  {
    if (s != true_end)
      goto end_i_and_j;
    goto end3;
  }
  if (s[0] || s[1] || s[2] || (c= (s[3] - '0')) > 9)
    goto end3;

  /* Handle the next 1 or 2 digits and store them in k */
  k=c;
  s+= 4;
  if (s == end || s[0] || s[1] || s[2] || (c= (s[3]-'0')) > 9)
    goto end4;
  k= k * 10 + c;
  s+= 2;
  *endptr= (char*) s;

  /* number string should have ended here */
  if (s != end && !s[0] && !s[1] && !s[2] && (c= (s[3] - '0')) <= 9)
    goto overflow;

  /* Check that we didn't get an overflow with the last digit */
  if (i > cutoff || (i == cutoff && ((j > cutoff2 || j == cutoff2) &&
                                     k > cutoff3)))
    goto overflow;
  li= i * LFACTOR2+ (ulonglong) j * 100 + k;
  return (longlong) li;

overflow:                                        /* *endptr is set here */
  *error= MY_ERRNO_ERANGE;
  return negative ? LONGLONG_MIN : (longlong) ULONGLONG_MAX;

end_i:
  *endptr= (char*) s;
  return (negative ? ((longlong) -(long) i) : (longlong) i);

end_i_and_j:
  li= (ulonglong) i * lfactor[(size_t) (s-start) / 4] + j;
  *endptr= (char*) s;
  return (negative ? -((longlong) li) : (longlong) li);

end3:
  li= (ulonglong) i*LFACTOR+ (ulonglong) j;
  *endptr= (char*) s;
  return (negative ? -((longlong) li) : (longlong) li);

end4:
  li= (ulonglong) i*LFACTOR1+ (ulonglong) j * 10 + k;
  *endptr= (char*) s;
  if (negative)
  {
   if (li > MAX_NEGATIVE_NUMBER)
     goto overflow;
   return -((longlong) li);
  }
  return (longlong) li;

no_conv:
  /* There was no number to convert.  */
  *error= MY_ERRNO_EDOM;
  *endptr= (char *) nptr;
  return 0;
}


static size_t
my_numchars_utf32(CHARSET_INFO *cs __attribute__((unused)),
                  const char *b, const char *e)
{
  return (size_t) (e - b) / 4;
}


static size_t
my_charpos_utf32(CHARSET_INFO *cs __attribute__((unused)),
                 const char *b, const char *e, size_t pos)
{
  size_t string_length= (size_t) (e - b);
  return pos * 4 > string_length ? string_length + 4 : pos * 4;
}


static size_t
my_well_formed_len_utf32(CHARSET_INFO *cs __attribute__((unused)),
                         const char *b, const char *e,
                         size_t nchars, int *error)
{
  /* Ensure string length is divisible by 4 */
  const char *b0= b;
  size_t length= e - b;
  DBUG_ASSERT((length % 4) == 0);
  *error= 0;
  nchars*= 4;
  if (length > nchars)
  {
    length= nchars;
    e= b + nchars;
  }
  for (; b < e; b+= 4)
  {
    /* Don't accept characters greater than U+10FFFF */
    if (b[0] || (uchar) b[1] > 0x10)
    {
      *error= 1;
      return b - b0;
    }
  }
  return length;
}


static
void my_fill_utf32(CHARSET_INFO *cs,
                   char *s, size_t slen, int fill)
{
  char buf[10];
  uint buflen;
  char *e= s + slen;
  
  DBUG_ASSERT((slen % 4) == 0);

  buflen= cs->cset->wc_mb(cs, (my_wc_t) fill, (uchar*) buf,
                          (uchar*) buf + sizeof(buf));
  DBUG_ASSERT(buflen == 4);
  while (s < e)
  {
    memcpy(s, buf, 4);
    s+= 4;
  }
}


static size_t
my_lengthsp_utf32(CHARSET_INFO *cs __attribute__((unused)),
                  const char *ptr, size_t length)
{
  const char *end= ptr + length;
  DBUG_ASSERT((length % 4) == 0);
  while (end > ptr + 3 && end[-1] == ' ' && !end[-2] && !end[-3] && !end[-4])
    end-= 4;
  return (size_t) (end - ptr);
}


static int
my_wildcmp_utf32_ci(CHARSET_INFO *cs,
                    const char *str, const char *str_end,
                    const char *wildstr, const char *wildend,
                    int escape, int w_one, int w_many)
{
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  return my_wildcmp_unicode(cs, str, str_end, wildstr, wildend,
                            escape, w_one, w_many, uni_plane); 
}


static int
my_wildcmp_utf32_bin(CHARSET_INFO *cs,
                     const char *str,const char *str_end,
                     const char *wildstr,const char *wildend,
                     int escape, int w_one, int w_many)
{
  return my_wildcmp_unicode(cs, str, str_end, wildstr, wildend,
                            escape, w_one, w_many, NULL); 
}


static int
my_strnncoll_utf32_bin(CHARSET_INFO *cs, 
                       const uchar *s, size_t slen,
                       const uchar *t, size_t tlen,
                       my_bool t_is_prefix)
{
2378
  my_wc_t UNINIT_VAR(s_wc), UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
  const uchar *se= s + slen;
  const uchar *te= t + tlen;

  while (s < se && t < te)
  {
    int s_res= my_utf32_uni(cs, &s_wc, s, se);
    int t_res= my_utf32_uni(cs, &t_wc, t, te);
    
    if (s_res <= 0 || t_res <= 0)
    {
      /* Incorrect string, compare by char value */
      return my_bincmp(s, se, t, te);
    }
    if (s_wc != t_wc)
    {
      return  s_wc > t_wc ? 1 : -1;
    }
    
    s+= s_res;
    t+= t_res;
  }
  return (int) (t_is_prefix ? (t-te) : ((se - s) - (te - t)));
}


static inline my_wc_t
my_utf32_get(const uchar *s)
{
  return
    ((my_wc_t) s[0] << 24) +
    ((my_wc_t) s[1] << 16) +
    ((my_wc_t) s[2] << 8) +
    s[3];
}


static int
my_strnncollsp_utf32_bin(CHARSET_INFO *cs __attribute__((unused)), 
                         const uchar *s, size_t slen, 
                         const uchar *t, size_t tlen,
                         my_bool diff_if_only_endspace_difference
                         __attribute__((unused)))
{
  const uchar *se, *te;
  size_t minlen;

  DBUG_ASSERT((slen % 4) == 0);
  DBUG_ASSERT((tlen % 4) == 0);

  se= s + slen;
  te= t + tlen;

  for (minlen= min(slen, tlen); minlen; minlen-= 4)
  {
    my_wc_t s_wc= my_utf32_get(s);
    my_wc_t t_wc= my_utf32_get(t);
    if (s_wc != t_wc)
      return  s_wc > t_wc ? 1 : -1;

    s+= 4;
    t+= 4;
  }

  if (slen != tlen)
  {
    int swap= 1;
    if (slen < tlen)
    {
      s= t;
      se= te;
      swap= -1;
    }

    for ( ; s < se ; s+= 4)
    {
      my_wc_t s_wc= my_utf32_get(s);
      if (s_wc != ' ')
        return (s_wc < ' ') ? -swap : swap;
    }
  }
  return 0;
}


static size_t
my_scan_utf32(CHARSET_INFO *cs,
              const char *str, const char *end, int sequence_type)
{
  const char *str0= str;
  
  switch (sequence_type)
  {
  case MY_SEQ_SPACES:
    for ( ; str < end; )
    {
      my_wc_t wc;
2475
      int res= my_utf32_uni(cs, &wc, (uchar*) str, (uchar*) end);
Alexander Barkov's avatar
Alexander Barkov committed
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
      if (res < 0 || wc != ' ')
        break;
      str+= res;
    }
    return (size_t) (str - str0);
  default:
    return 0;
  }
}


static MY_COLLATION_HANDLER my_collation_utf32_general_ci_handler =
{
  NULL, /* init */
  my_strnncoll_utf32,
  my_strnncollsp_utf32,
  my_strnxfrm_unicode,
  my_strnxfrmlen_utf32,
2494
  my_like_range_generic,
Alexander Barkov's avatar
Alexander Barkov committed
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
  my_wildcmp_utf32_ci,
  my_strcasecmp_mb2_or_mb4,
  my_instr_mb,
  my_hash_sort_utf32,
  my_propagate_simple
};


static MY_COLLATION_HANDLER my_collation_utf32_bin_handler =
{
  NULL, /* init */
  my_strnncoll_utf32_bin,
  my_strnncollsp_utf32_bin,
2508 2509
  my_strnxfrm_unicode_full_bin,
  my_strnxfrmlen_unicode_full_bin,
2510
  my_like_range_generic,
Alexander Barkov's avatar
Alexander Barkov committed
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
  my_wildcmp_utf32_bin,
  my_strcasecmp_mb2_or_mb4,
  my_instr_mb,
  my_hash_sort_utf32,
  my_propagate_simple
};


MY_CHARSET_HANDLER my_charset_utf32_handler=
{
  NULL, /* init */
  my_ismbchar_utf32,
  my_mbcharlen_utf32,
  my_numchars_utf32,
  my_charpos_utf32,
  my_well_formed_len_utf32,
  my_lengthsp_utf32,
  my_numcells_mb,
  my_utf32_uni,
  my_uni_utf32,
  my_mb_ctype_mb,
  my_caseup_str_mb2_or_mb4,
  my_casedn_str_mb2_or_mb4,
  my_caseup_utf32,
  my_casedn_utf32,
  my_snprintf_utf32,
  my_l10tostr_mb2_or_mb4,
  my_ll10tostr_mb2_or_mb4,
  my_fill_utf32,
  my_strntol_mb2_or_mb4,
  my_strntoul_mb2_or_mb4,
  my_strntoll_mb2_or_mb4,
  my_strntoull_mb2_or_mb4,
  my_strntod_mb2_or_mb4,
  my_strtoll10_utf32,
  my_strntoull10rnd_mb2_or_mb4,
  my_scan_utf32
};


CHARSET_INFO my_charset_utf32_general_ci=
{
  60,0,0,              /* number       */
  MY_CS_COMPILED|MY_CS_PRIMARY|MY_CS_STRNXFRM|MY_CS_UNICODE|MY_CS_NONASCII,
  "utf32",             /* cs name    */
  "utf32_general_ci",  /* name         */
  "UTF-32 Unicode",    /* comment      */
  NULL,                /* tailoring    */
  NULL,                /* ctype        */
  NULL,                /* to_lower     */
  NULL,                /* to_upper     */
  NULL,                /* sort_order   */
  NULL,                /* contractions */
  NULL,                /* sort_order_big*/
  NULL,                /* tab_to_uni   */
  NULL,                /* tab_from_uni */
  my_unicase_default,  /* caseinfo     */
  NULL,                /* state_map    */
  NULL,                /* ident_map    */
  1,                   /* strxfrm_multiply */
  1,                   /* caseup_multiply  */
  1,                   /* casedn_multiply  */
  4,                   /* mbminlen     */
  4,                   /* mbmaxlen     */
  0,                   /* min_sort_char */
  0xFFFF,              /* max_sort_char */
  ' ',                 /* pad char      */
  0,                   /* escape_with_backslash_is_dangerous */
  &my_charset_utf32_handler,
  &my_collation_utf32_general_ci_handler
};


CHARSET_INFO my_charset_utf32_bin=
{
  61,0,0,              /* number       */
  MY_CS_COMPILED|MY_CS_BINSORT|MY_CS_UNICODE|MY_CS_NONASCII,
  "utf32",             /* cs name    */
  "utf32_bin",         /* name         */
  "UTF-32 Unicode",    /* comment      */
  NULL,                /* tailoring    */
  NULL,                /* ctype        */
  NULL,                /* to_lower     */
  NULL,                /* to_upper     */
  NULL,                /* sort_order   */
  NULL,                /* contractions */
  NULL,                /* sort_order_big*/
  NULL,                /* tab_to_uni   */
  NULL,                /* tab_from_uni */
  my_unicase_default,  /* caseinfo     */
  NULL,                /* state_map    */
  NULL,                /* ident_map    */
  1,                   /* strxfrm_multiply */
  1,                   /* caseup_multiply  */
  1,                   /* casedn_multiply  */
  4,                   /* mbminlen     */
  4,                   /* mbmaxlen     */
  0,                   /* min_sort_char */
  0xFFFF,              /* max_sort_char */
  ' ',                 /* pad char      */
  0,                   /* escape_with_backslash_is_dangerous */
  &my_charset_utf32_handler,
  &my_collation_utf32_bin_handler
};


#endif /* HAVE_CHARSET_utf32 */


#ifdef HAVE_CHARSET_ucs2

static uchar ctype_ucs2[] = {
    0,
   32, 32, 32, 32, 32, 32, 32, 32, 32, 40, 40, 40, 40, 40, 32, 32,
   32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
   72, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
  132,132,132,132,132,132,132,132,132,132, 16, 16, 16, 16, 16, 16,
   16,129,129,129,129,129,129,  1,  1,  1,  1,  1,  1,  1,  1,  1,
    1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 16, 16, 16, 16, 16,
   16,130,130,130,130,130,130,  2,  2,  2,  2,  2,  2,  2,  2,  2,
    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 16, 16, 16, 16, 32,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
};

static uchar to_lower_ucs2[] = {
    0,  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, 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, 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
};

static uchar to_upper_ucs2[] = {
    0,  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, 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,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
};


static int my_ucs2_uni(CHARSET_INFO *cs __attribute__((unused)),
		       my_wc_t * pwc, const uchar *s, const uchar *e)
{
  if (s+2 > e) /* Need 2 characters */
    return MY_CS_TOOSMALL2;
  
  *pwc= ((uchar)s[0]) * 256  + ((uchar)s[1]);
  return 2;
}

static int my_uni_ucs2(CHARSET_INFO *cs __attribute__((unused)) ,
		       my_wc_t wc, uchar *r, uchar *e)
{
  if ( r+2 > e ) 
    return MY_CS_TOOSMALL2;
2696 2697 2698 2699

  if (wc > 0xFFFF) /* UCS2 does not support characters outside BMP */
    return MY_CS_ILUNI;

Alexander Barkov's avatar
Alexander Barkov committed
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
  r[0]= (uchar) (wc >> 8);
  r[1]= (uchar) (wc & 0xFF);
  return 2;
}


static size_t my_caseup_ucs2(CHARSET_INFO *cs, char *src, size_t srclen,
                           char *dst __attribute__((unused)),
                           size_t dstlen __attribute__((unused)))
{
  my_wc_t wc;
  int res;
  char *srcend= src + srclen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  DBUG_ASSERT(src == dst && srclen == dstlen);
  
  while ((src < srcend) &&
         (res= my_ucs2_uni(cs, &wc, (uchar *)src, (uchar*) srcend)) > 0)
  {
    int plane= (wc>>8) & 0xFF;
    wc= uni_plane[plane] ? uni_plane[plane][wc & 0xFF].toupper : wc;
    if (res != my_uni_ucs2(cs, wc, (uchar*) src, (uchar*) srcend))
      break;
    src+= res;
  }
  return srclen;
}


static void my_hash_sort_ucs2(CHARSET_INFO *cs, const uchar *s, size_t slen,
			      ulong *n1, ulong *n2)
{
  my_wc_t wc;
  int res;
  const uchar *e=s+slen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  while (e > s+1 && e[-1] == ' ' && e[-2] == '\0')
    e-= 2;

  while ((s < e) && (res=my_ucs2_uni(cs,&wc, (uchar *)s, (uchar*)e)) >0)
  {
    int plane = (wc>>8) & 0xFF;
    wc = uni_plane[plane] ? uni_plane[plane][wc & 0xFF].sort : wc;
    n1[0]^= (((n1[0] & 63)+n2[0])*(wc & 0xFF))+ (n1[0] << 8);
    n2[0]+=3;
    n1[0]^= (((n1[0] & 63)+n2[0])*(wc >> 8))+ (n1[0] << 8);
    n2[0]+=3;
    s+=res;
  }
}


static size_t my_casedn_ucs2(CHARSET_INFO *cs, char *src, size_t srclen,
                           char *dst __attribute__((unused)),
                           size_t dstlen __attribute__((unused)))
{
  my_wc_t wc;
  int res;
  char *srcend= src + srclen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
  DBUG_ASSERT(src == dst && srclen == dstlen);

  while ((src < srcend) &&
         (res= my_ucs2_uni(cs, &wc, (uchar*) src, (uchar*) srcend)) > 0)
  {
    int plane= (wc>>8) & 0xFF;
    wc= uni_plane[plane] ? uni_plane[plane][wc & 0xFF].tolower : wc;
    if (res != my_uni_ucs2(cs, wc, (uchar*) src, (uchar*) srcend))
      break;
    src+= res;
  }
  return srclen;
}


static int my_strnncoll_ucs2(CHARSET_INFO *cs, 
			     const uchar *s, size_t slen, 
                             const uchar *t, size_t tlen,
                             my_bool t_is_prefix)
{
  int s_res,t_res;
2782
  my_wc_t UNINIT_VAR(s_wc),UNINIT_VAR(t_wc);
Alexander Barkov's avatar
Alexander Barkov committed
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
  const uchar *se=s+slen;
  const uchar *te=t+tlen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;

  while ( s < se && t < te )
  {
    int plane;
    s_res=my_ucs2_uni(cs,&s_wc, s, se);
    t_res=my_ucs2_uni(cs,&t_wc, t, te);
    
    if ( s_res <= 0 || t_res <= 0 )
    {
      /* Incorrect string, compare by char value */
      return ((int)s[0]-(int)t[0]); 
    }
    
    plane=(s_wc>>8) & 0xFF;
    s_wc = uni_plane[plane] ? uni_plane[plane][s_wc & 0xFF].sort : s_wc;
    plane=(t_wc>>8) & 0xFF;
    t_wc = uni_plane[plane] ? uni_plane[plane][t_wc & 0xFF].sort : t_wc;
    if ( s_wc != t_wc )
    {
      return  s_wc > t_wc ? 1 : -1;
    }
    
    s+=s_res;
    t+=t_res;
  }
  return (int) (t_is_prefix ? t-te : ((se-s) - (te-t)));
}

/*
  Compare strings, discarding end space

  SYNOPSIS
    my_strnncollsp_ucs2()
    cs                  character set handler
    a                   First string to compare
    a_length            Length of 'a'
    b                   Second string to compare
    b_length            Length of 'b'
2824

Alexander Barkov's avatar
Alexander Barkov committed
2825 2826 2827
  IMPLEMENTATION
    If one string is shorter as the other, then we space extend the other
    so that the strings have equal length.
2828

Alexander Barkov's avatar
Alexander Barkov committed
2829 2830 2831 2832 2833
    This will ensure that the following things hold:

    "a"  == "a "
    "a\0" < "a"
    "a\0" < "a "
2834

Alexander Barkov's avatar
Alexander Barkov committed
2835 2836 2837 2838 2839
  RETURN
    < 0  a <  b
    = 0  a == b
    > 0  a > b
*/
2840

Alexander Barkov's avatar
Alexander Barkov committed
2841 2842 2843 2844 2845
static int my_strnncollsp_ucs2(CHARSET_INFO *cs __attribute__((unused)),
                               const uchar *s, size_t slen,
                               const uchar *t, size_t tlen,
                               my_bool diff_if_only_endspace_difference
			       __attribute__((unused)))
2846
{
Alexander Barkov's avatar
Alexander Barkov committed
2847 2848 2849
  const uchar *se, *te;
  size_t minlen;
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
2850

Alexander Barkov's avatar
Alexander Barkov committed
2851 2852 2853
  /* extra safety to make sure the lengths are even numbers */
  slen&= ~1;
  tlen&= ~1;
2854

Alexander Barkov's avatar
Alexander Barkov committed
2855 2856 2857 2858
  se= s + slen;
  te= t + tlen;

  for (minlen= min(slen, tlen); minlen; minlen-= 2)
2859
  {
Alexander Barkov's avatar
Alexander Barkov committed
2860 2861 2862 2863 2864 2865 2866 2867
    int s_wc = uni_plane[s[0]] ? (int) uni_plane[s[0]][s[1]].sort :
                                 (((int) s[0]) << 8) + (int) s[1];

    int t_wc = uni_plane[t[0]] ? (int) uni_plane[t[0]][t[1]].sort : 
                                 (((int) t[0]) << 8) + (int) t[1];
    if ( s_wc != t_wc )
      return  s_wc > t_wc ? 1 : -1;

2868
    s+= 2;
Alexander Barkov's avatar
Alexander Barkov committed
2869
    t+= 2;
2870
  }
Alexander Barkov's avatar
Alexander Barkov committed
2871 2872

  if (slen != tlen)
2873
  {
Alexander Barkov's avatar
Alexander Barkov committed
2874 2875
    int swap= 1;
    if (slen < tlen)
2876
    {
Alexander Barkov's avatar
Alexander Barkov committed
2877 2878 2879
      s= t;
      se= te;
      swap= -1;
2880 2881
    }

Alexander Barkov's avatar
Alexander Barkov committed
2882
    for ( ; s < se ; s+= 2)
2883
    {
Alexander Barkov's avatar
Alexander Barkov committed
2884 2885
      if (s[0] || s[1] != ' ')
        return (s[0] == 0 && s[1] < ' ') ? -swap : swap;
2886 2887
    }
  }
Alexander Barkov's avatar
Alexander Barkov committed
2888 2889
  return 0;
}
2890 2891


Alexander Barkov's avatar
Alexander Barkov committed
2892 2893 2894 2895 2896 2897
static uint my_ismbchar_ucs2(CHARSET_INFO *cs __attribute__((unused)),
                             const char *b __attribute__((unused)),
                             const char *e __attribute__((unused)))
{
  return 2;
}
2898 2899


Alexander Barkov's avatar
Alexander Barkov committed
2900 2901 2902 2903
static uint my_mbcharlen_ucs2(CHARSET_INFO *cs  __attribute__((unused)) , 
                              uint c __attribute__((unused)))
{
  return 2;
2904 2905 2906
}


2907
static
2908 2909
size_t my_numchars_ucs2(CHARSET_INFO *cs __attribute__((unused)),
                        const char *b, const char *e)
2910
{
2911
  return (size_t) (e-b)/2;
2912 2913
}

2914

2915
static
2916 2917 2918 2919
size_t my_charpos_ucs2(CHARSET_INFO *cs __attribute__((unused)),
                       const char *b  __attribute__((unused)),
                       const char *e  __attribute__((unused)),
                       size_t pos)
2920
{
2921
  size_t string_length= (size_t) (e - b);
monty@mysql.com's avatar
monty@mysql.com committed
2922
  return pos > string_length ? string_length + 2 : pos * 2;
2923 2924
}

2925

bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
2926
static
2927 2928 2929
size_t my_well_formed_len_ucs2(CHARSET_INFO *cs __attribute__((unused)),
                               const char *b, const char *e,
                               size_t nchars, int *error)
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
2930
{
monty@mysql.com's avatar
monty@mysql.com committed
2931
  /* Ensure string length is dividable with 2 */
2932
  size_t nbytes= ((size_t) (e-b)) & ~(size_t) 1;
2933
  *error= 0;
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
2934
  nchars*= 2;
2935
  return min(nbytes, nchars);
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
2936 2937
}

2938

2939 2940 2941 2942 2943 2944
static
int my_wildcmp_ucs2_ci(CHARSET_INFO *cs,
		    const char *str,const char *str_end,
		    const char *wildstr,const char *wildend,
		    int escape, int w_one, int w_many)
{
2945
  MY_UNICASE_INFO **uni_plane= cs->caseinfo;
2946 2947
  return my_wildcmp_unicode(cs,str,str_end,wildstr,wildend,
                            escape,w_one,w_many,uni_plane); 
2948 2949
}

2950

2951 2952 2953 2954 2955 2956
static
int my_wildcmp_ucs2_bin(CHARSET_INFO *cs,
		    const char *str,const char *str_end,
		    const char *wildstr,const char *wildend,
		    int escape, int w_one, int w_many)
{
2957 2958
  return my_wildcmp_unicode(cs,str,str_end,wildstr,wildend,
                            escape,w_one,w_many,NULL); 
2959 2960 2961 2962 2963
}


static
int my_strnncoll_ucs2_bin(CHARSET_INFO *cs, 
2964 2965
                          const uchar *s, size_t slen,
                          const uchar *t, size_t tlen,
2966
                          my_bool t_is_prefix)
2967 2968
{
  int s_res,t_res;
2969
  my_wc_t UNINIT_VAR(s_wc),UNINIT_VAR(t_wc);
2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
  const uchar *se=s+slen;
  const uchar *te=t+tlen;

  while ( s < se && t < te )
  {
    s_res=my_ucs2_uni(cs,&s_wc, s, se);
    t_res=my_ucs2_uni(cs,&t_wc, t, te);
    
    if ( s_res <= 0 || t_res <= 0 )
    {
      /* Incorrect string, compare by char value */
      return ((int)s[0]-(int)t[0]); 
    }
    if ( s_wc != t_wc )
    {
2985
      return  s_wc > t_wc ? 1 : -1;
2986 2987 2988 2989 2990
    }
    
    s+=s_res;
    t+=t_res;
  }
2991
  return (int) (t_is_prefix ? t-te : ((se-s) - (te-t)));
2992 2993
}

evgen@moonbone.local's avatar
evgen@moonbone.local committed
2994
static int my_strnncollsp_ucs2_bin(CHARSET_INFO *cs __attribute__((unused)), 
2995 2996
                                   const uchar *s, size_t slen, 
                                   const uchar *t, size_t tlen,
monty@mysql.com's avatar
monty@mysql.com committed
2997 2998
                                   my_bool diff_if_only_endspace_difference
                                   __attribute__((unused)))
2999
{
evgen@moonbone.local's avatar
evgen@moonbone.local committed
3000
  const uchar *se, *te;
3001
  size_t minlen;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037

  /* extra safety to make sure the lengths are even numbers */
  slen= (slen >> 1) << 1;
  tlen= (tlen >> 1) << 1;

  se= s + slen;
  te= t + tlen;

  for (minlen= min(slen, tlen); minlen; minlen-= 2)
  {
    int s_wc= s[0] * 256 + s[1];
    int t_wc= t[0] * 256 + t[1];
    if ( s_wc != t_wc )
      return  s_wc > t_wc ? 1 : -1;

    s+= 2;
    t+= 2;
  }

  if (slen != tlen)
  {
    int swap= 1;
    if (slen < tlen)
    {
      s= t;
      se= te;
      swap= -1;
    }

    for ( ; s < se ; s+= 2)
    {
      if (s[0] || s[1] != ' ')
        return (s[0] == 0 && s[1] < ' ') ? -swap : swap;
    }
  }
  return 0;
3038 3039
}

3040

3041 3042
static
void my_hash_sort_ucs2_bin(CHARSET_INFO *cs __attribute__((unused)),
3043
			   const uchar *key, size_t len,ulong *nr1, ulong *nr2)
3044 3045 3046 3047
{
  const uchar *pos = key;
  
  key+= len;
3048 3049 3050 3051

  while (key > pos+1 && key[-1] == ' ' && key[-2] == '\0')
    key-= 2;

3052 3053 3054 3055 3056 3057 3058 3059
  for (; pos < (uchar*) key ; pos++)
  {
    nr1[0]^=(ulong) ((((uint) nr1[0] & 63)+nr2[0]) * 
	     ((uint)*pos)) + (nr1[0] << 8);
    nr2[0]+=3;
  }
}

monty@mysql.com's avatar
monty@mysql.com committed
3060

3061
static MY_COLLATION_HANDLER my_collation_ucs2_general_ci_handler =
3062
{
3063
    NULL,		/* init */
3064
    my_strnncoll_ucs2,
3065
    my_strnncollsp_ucs2,
Alexander Barkov's avatar
Alexander Barkov committed
3066
    my_strnxfrm_unicode,
3067
    my_strnxfrmlen_simple,
3068
    my_like_range_generic,
3069
    my_wildcmp_ucs2_ci,
Alexander Barkov's avatar
Alexander Barkov committed
3070
    my_strcasecmp_mb2_or_mb4,
3071
    my_instr_mb,
3072 3073
    my_hash_sort_ucs2,
    my_propagate_simple
3074 3075
};

3076

3077 3078
static MY_COLLATION_HANDLER my_collation_ucs2_bin_handler =
{
3079
    NULL,		/* init */
3080
    my_strnncoll_ucs2_bin,
3081
    my_strnncollsp_ucs2_bin,
Alexander Barkov's avatar
Alexander Barkov committed
3082
    my_strnxfrm_unicode,
3083
    my_strnxfrmlen_simple,
3084
    my_like_range_generic,
3085
    my_wildcmp_ucs2_bin,
Alexander Barkov's avatar
Alexander Barkov committed
3086
    my_strcasecmp_mb2_or_mb4,
3087
    my_instr_mb,
3088 3089
    my_hash_sort_ucs2_bin,
    my_propagate_simple
3090 3091
};

3092

3093
MY_CHARSET_HANDLER my_charset_ucs2_handler=
3094
{
3095
    NULL,		/* init */
3096 3097 3098 3099
    my_ismbchar_ucs2,	/* ismbchar     */
    my_mbcharlen_ucs2,	/* mbcharlen    */
    my_numchars_ucs2,
    my_charpos_ucs2,
3100
    my_well_formed_len_ucs2,
Alexander Barkov's avatar
Alexander Barkov committed
3101
    my_lengthsp_mb2,
3102
    my_numcells_mb,
3103 3104
    my_ucs2_uni,	/* mb_wc        */
    my_uni_ucs2,	/* wc_mb        */
3105
    my_mb_ctype_mb,
Alexander Barkov's avatar
Alexander Barkov committed
3106 3107
    my_caseup_str_mb2_or_mb4,
    my_casedn_str_mb2_or_mb4,
3108 3109
    my_caseup_ucs2,
    my_casedn_ucs2,
Alexander Barkov's avatar
Alexander Barkov committed
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
    my_snprintf_mb2,
    my_l10tostr_mb2_or_mb4,
    my_ll10tostr_mb2_or_mb4,
    my_fill_mb2,
    my_strntol_mb2_or_mb4,
    my_strntoul_mb2_or_mb4,
    my_strntoll_mb2_or_mb4,
    my_strntoull_mb2_or_mb4,
    my_strntod_mb2_or_mb4,
    my_strtoll10_mb2,
    my_strntoull10rnd_mb2_or_mb4,
    my_scan_mb2
3122 3123 3124
};


3125
CHARSET_INFO my_charset_ucs2_general_ci=
3126 3127
{
    35,0,0,		/* number       */
3128
    MY_CS_COMPILED|MY_CS_PRIMARY|MY_CS_STRNXFRM|MY_CS_UNICODE|MY_CS_NONASCII,
3129 3130 3131
    "ucs2",		/* cs name    */
    "ucs2_general_ci",	/* name         */
    "",			/* comment      */
3132
    NULL,		/* tailoring    */
3133 3134 3135 3136
    ctype_ucs2,		/* ctype        */
    to_lower_ucs2,	/* to_lower     */
    to_upper_ucs2,	/* to_upper     */
    to_upper_ucs2,	/* sort_order   */
3137 3138
    NULL,		/* contractions */
    NULL,		/* sort_order_big*/
3139 3140
    NULL,		/* tab_to_uni   */
    NULL,		/* tab_from_uni */
3141
    my_unicase_default, /* caseinfo     */
3142 3143
    NULL,		/* state_map    */
    NULL,		/* ident_map    */
3144
    1,			/* strxfrm_multiply */
3145 3146
    1,                  /* caseup_multiply  */
    1,                  /* casedn_multiply  */
3147
    2,			/* mbminlen     */
3148
    2,			/* mbmaxlen     */
3149 3150
    0,			/* min_sort_char */
    0xFFFF,		/* max_sort_char */
3151
    ' ',                /* pad char      */
3152
    0,                  /* escape_with_backslash_is_dangerous */
3153 3154
    &my_charset_ucs2_handler,
    &my_collation_ucs2_general_ci_handler
3155 3156
};

3157 3158 3159 3160

CHARSET_INFO my_charset_ucs2_general_mysql500_ci=
{
  159, 0, 0,                                       /* number           */
3161
  MY_CS_COMPILED|MY_CS_STRNXFRM|MY_CS_UNICODE|MY_CS_NONASCII, /* state */
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190
  "ucs2",                                          /* cs name          */
  "ucs2_general_mysql500_ci",                      /* name             */
  "",                                              /* comment          */
  NULL,                                            /* tailoring        */
  ctype_ucs2,                                      /* ctype            */
  to_lower_ucs2,                                   /* to_lower         */
  to_upper_ucs2,                                   /* to_upper         */
  to_upper_ucs2,                                   /* sort_order       */
  NULL,                                            /* contractions     */
  NULL,                                            /* sort_order_big   */
  NULL,                                            /* tab_to_uni       */
  NULL,                                            /* tab_from_uni     */
  my_unicase_mysql500,                             /* caseinfo         */
  NULL,                                            /* state_map        */
  NULL,                                            /* ident_map        */
  1,                                               /* strxfrm_multiply */
  1,                                               /* caseup_multiply  */
  1,                                               /* casedn_multiply  */
  2,                                               /* mbminlen         */
  2,                                               /* mbmaxlen         */
  0,                                               /* min_sort_char    */
  0xFFFF,                                          /* max_sort_char    */
  ' ',                                             /* pad char         */
  0,                          /* escape_with_backslash_is_dangerous    */
  &my_charset_ucs2_handler,
  &my_collation_ucs2_general_ci_handler
};


3191 3192 3193
CHARSET_INFO my_charset_ucs2_bin=
{
    90,0,0,		/* number       */
3194
    MY_CS_COMPILED|MY_CS_BINSORT|MY_CS_UNICODE|MY_CS_NONASCII,
3195 3196 3197
    "ucs2",		/* cs name    */
    "ucs2_bin",		/* name         */
    "",			/* comment      */
3198
    NULL,		/* tailoring    */
3199 3200 3201
    ctype_ucs2,		/* ctype        */
    to_lower_ucs2,	/* to_lower     */
    to_upper_ucs2,	/* to_upper     */
bar@mysql.com's avatar
bar@mysql.com committed
3202
    NULL,		/* sort_order   */
3203
    NULL,		/* contractions */
3204
    NULL,		/* sort_order_big*/
3205 3206
    NULL,		/* tab_to_uni   */
    NULL,		/* tab_from_uni */
3207
    my_unicase_default, /* caseinfo     */
3208 3209
    NULL,		/* state_map    */
    NULL,		/* ident_map    */
3210
    1,			/* strxfrm_multiply */
3211 3212
    1,                  /* caseup_multiply  */
    1,                  /* casedn_multiply  */
3213
    2,			/* mbminlen     */
3214
    2,			/* mbmaxlen     */
3215 3216
    0,			/* min_sort_char */
    0xFFFF,		/* max_sort_char */
3217
    ' ',                /* pad char      */
3218
    0,                  /* escape_with_backslash_is_dangerous */
3219 3220
    &my_charset_ucs2_handler,
    &my_collation_ucs2_bin_handler
3221 3222 3223
};


Alexander Barkov's avatar
Alexander Barkov committed
3224
#endif /* HAVE_CHARSET_ucs2 */