mf_casecnv.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:6k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. /*
  18.   Functions to convert to lover_case and to upper_case in scandinavia.
  19.   case_sort converts a character string to a representaion that can
  20.   be compared by strcmp to find with is alfabetical bigger.
  21.   (lower- and uppercase letters is compared as the same)
  22. */
  23. #include "mysys_priv.h"
  24. #include <m_ctype.h>
  25. /* string to uppercase */
  26. void caseup_str(my_string str)
  27. {
  28. #ifdef USE_MB
  29.   register uint32 l;
  30.   register char *end=str+(uint) strlen(str);
  31.   if (use_mb(default_charset_info))
  32.     while (*str)
  33.     {
  34.       if ((l=my_ismbchar(default_charset_info, str,end))) str+=l;
  35.       else *str=toupper(*str),++str;
  36.     }
  37.   else
  38. #endif
  39.     while ((*str = toupper(*str)) != 0)
  40.       str++;
  41. } /* caseup_str */
  42. /* string to lowercase */
  43. void casedn_str(my_string str)
  44. {
  45. #ifdef USE_MB
  46.   register uint32 l;
  47.   register char *end=str+(uint) strlen(str);
  48.   if (use_mb(default_charset_info))
  49.     while (*str)
  50.     {
  51.       if ((l=my_ismbchar(default_charset_info, str,end))) str+=l;
  52.       else *str=tolower(*str),++str;
  53.     }
  54.   else
  55. #endif
  56.     while ((*str= tolower(*str)) != 0)
  57.       str++;
  58. } /* casedn_str */
  59. /* to uppercase */
  60. void caseup(my_string str, uint length)
  61. {
  62. #ifdef USE_MB
  63.   register uint32 l;
  64.   register char *end=str+length;
  65.   if (use_mb(default_charset_info))
  66.     while (str<end)
  67.     {
  68.       if ((l=my_ismbchar(default_charset_info, str,end))) str+=l;
  69.       else *str=toupper(*str),++str;
  70.     }
  71.   else
  72. #endif
  73.     for ( ; length>0 ; length--, str++)
  74.       *str= toupper(*str);
  75. } /* caseup */
  76. /* to lowercase */
  77. void casedn(my_string str, uint length)
  78. {
  79. #ifdef USE_MB
  80.   register uint32 l;
  81.   register char *end=str+length;
  82.   if (use_mb(default_charset_info))
  83.     while (str<end)
  84.     {
  85.       if ((l=my_ismbchar(default_charset_info, str,end))) str+=l;
  86.       else *str=tolower(*str),++str;
  87.     }
  88.   else
  89. #endif
  90.     for ( ; length>0 ; length--, str++)
  91.       *str= tolower(*str);
  92. } /* casedn */
  93. /* to sort-string that can be compared to get text in order */
  94. void case_sort(my_string str, uint length)
  95. {
  96.   for ( ; length>0 ; length--, str++)
  97.     *str= (char) my_sort_order[(uchar) *str];
  98. } /* case_sort */
  99. /* find string in another with no case_sensivity */
  100. /* ToDo: This function should be modified to support multibyte charset.
  101.          However it is not used untill 3.23.5.
  102.  Wei He (hewei@mail.ied.ac.cn)
  103. */
  104. my_string my_strcasestr(const char *str, const char *search)
  105. {
  106.  uchar *i,*j,*pos;
  107.  pos=(uchar*) str;
  108. skipp:
  109.  while (*pos != '')
  110.  {
  111.    if (toupper((uchar) *pos++) == toupper((uchar) *search))
  112.    {
  113.      i=(uchar*) pos; j=(uchar*) search+1;
  114.      while (*j)
  115.        if (toupper(*i++) != toupper(*j++)) goto skipp;
  116.      return ((char*) pos-1);
  117.    }
  118.  }
  119.  return ((my_string) 0);
  120. } /* strcstr */
  121. /* compare strings without regarding to case */
  122. int my_strcasecmp(const char *s, const char *t)
  123. {
  124. #ifdef USE_MB
  125.   register uint32 l;
  126.   register const char *end=s+(uint) strlen(s);
  127.   if (use_mb(default_charset_info))
  128.   {
  129.     while (s<end)
  130.     {
  131.       if ((l=my_ismbchar(default_charset_info, s,end)))
  132.       {
  133.         while (l--)
  134.           if (*s++ != *t++) return 1;
  135.       }
  136.       else if (my_ismbhead(default_charset_info, *t)) return 1;
  137.       else if (toupper((uchar) *s++) != toupper((uchar) *t++)) return 1;
  138.     }
  139.     return *t;
  140.   }
  141.   else
  142. #endif
  143.   {
  144.     while (toupper((uchar) *s) == toupper((uchar) *t++))
  145.       if (!*s++) return 0;
  146.     return ((int) toupper((uchar) s[0]) - (int) toupper((uchar) t[-1]));
  147.   }
  148. }
  149. int my_casecmp(const char *s, const char *t, uint len)
  150. {
  151. #ifdef USE_MB
  152.   register uint32 l;
  153.   register const char *end=s+len;
  154.   if (use_mb(default_charset_info))
  155.   {
  156.     while (s<end)
  157.     {
  158.       if ((l=my_ismbchar(default_charset_info, s,end)))
  159.       {
  160.         while (l--)
  161.           if (*s++ != *t++) return 1;
  162.       }
  163.       else if (my_ismbhead(default_charset_info, *t)) return 1;
  164.       else if (toupper((uchar) *s++) != toupper((uchar) *t++)) return 1;
  165.     }
  166.     return 0;
  167.   }
  168.   else
  169. #endif
  170.   {
  171.     while (len-- != 0 && toupper(*s++) == toupper(*t++)) ;
  172.     return (int) len+1;
  173.   }
  174. }
  175. int my_strsortcmp(const char *s, const char *t)
  176. {
  177. #ifdef USE_STRCOLL
  178.   if (use_strcoll(default_charset_info))
  179.     return my_strcoll(default_charset_info, (uchar *)s, (uchar *)t);
  180.   else
  181. #endif
  182.   {
  183.     while (my_sort_order[(uchar) *s] == my_sort_order[(uchar) *t++])
  184.       if (!*s++) return 0;
  185.     return ((int) my_sort_order[(uchar) s[0]] -
  186.             (int) my_sort_order[(uchar) t[-1]]);
  187.   }
  188. }
  189. int my_sortcmp(const char *s, const char *t, uint len)
  190. {
  191. #ifdef USE_STRCOLL
  192.   if (use_strcoll(default_charset_info))
  193.     return my_strnncoll(default_charset_info,
  194.                         (uchar *)s, len, (uchar *)t, len);
  195.   else
  196. #endif
  197.   {
  198.     while (len--)
  199.     {
  200.       if (my_sort_order[(uchar) *s++] != my_sort_order[(uchar) *t++])
  201.         return ((int) my_sort_order[(uchar) s[-1]] -
  202.                 (int) my_sort_order[(uchar) t[-1]]);
  203.     }
  204.     return 0;
  205.   }
  206. }
  207. int my_sortncmp(const char *s, uint s_len, const char *t, uint t_len)
  208. {
  209. #ifdef USE_STRCOLL
  210.   if (use_strcoll(default_charset_info))
  211.     return my_strnncoll(default_charset_info,
  212.                         (uchar *)s, s_len, (uchar *)t, t_len);
  213.   else
  214. #endif
  215.   {
  216.     uint len= min(s_len,t_len);
  217.     while (len--)
  218.     {
  219.       if (my_sort_order[(uchar) *s++] != my_sort_order[(uchar) *t++])
  220.         return ((int) my_sort_order[(uchar) s[-1]] -
  221.                 (int) my_sort_order[(uchar) t[-1]]);
  222.     }
  223.     return (int) (s_len - t_len);
  224.   }
  225. }