1 /* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 #include <my_global.h>
29 #include "m_string.h"
30 #include "m_ctype.h"
31 
32 
33 /*
34   Return pointer to first occurrence of character in a multi-byte string
35   or NULL if the character doesn't appear in the multi-byte string or
36   invalid character in charset of multi-byte string is found.
37 
38   @param   cs    Pointer to charset info.
39   @param   str   Pointer to start of multi-byte string.
40   @param   end   Pointer to end of multi-byte string.
41   @param   c     Character to find first occurrence of.
42 
43   @return  Pointer to first occurence of c in str or NULL.
44 */
45 
my_strchr(const CHARSET_INFO * cs,const char * str,const char * end,pchar c)46 char *my_strchr(const CHARSET_INFO *cs, const char *str, const char *end,
47                 pchar c)
48 {
49   while (str < end)
50   {
51     uint mbl= my_mbcharlen(cs, *(uchar *)str);
52     if (mbl == 0)
53       return NULL;
54     if (mbl == 1)
55     {
56       if (*str == c)
57         return((char *)str);
58       str++;
59     }
60     else
61       str+= mbl;
62   }
63   return(0);
64 }
65 
66 
67 /**
68   Calculate the length of the initial segment of 'str' which consists
69   entirely of characters not in 'reject'.
70 
71   @param  cs              Pointer to charset info.
72   @param  str             Pointer to multi-byte string.
73   @param  str_end         Pointer to end of multi-byte string.
74   @param  reject          Pointer to start of single-byte reject string.
75   @param  reject_length   Length of single-byte reject string.
76 
77   @return Length of segment of multi-byte string that doesn't contain
78           any character of the single-byte reject string or zero if an
79           invalid encoding of a character of the multi-byte string is
80           found.
81 
82   @note The reject string points to single-byte characters so it is
83   only possible to find the first occurrence of a single-byte
84   character.  Multi-byte characters in 'str' are treated as not
85   matching any character in the reject string.
86   This method returns zero if an invalid encoding of any character
87   in the string 'str' using charset 'cs' is found.
88 
89   @todo should be moved to CHARSET_INFO if it's going to be called
90   frequently.
91 
92   @internal The implementation builds on the assumption that 'str' is long,
93   while 'reject' is short. So it compares each character in string
94   with the characters in 'reject' in a tight loop over the characters
95   in 'reject'.
96 */
97 
my_strcspn(const CHARSET_INFO * cs,const char * str,const char * str_end,const char * reject,int reject_length)98 size_t my_strcspn(const CHARSET_INFO *cs, const char *str,
99                   const char *str_end, const char *reject,
100                   int reject_length)
101 {
102   const char *ptr_str, *ptr_reject;
103   const char *reject_end= reject + reject_length;
104   uint mbl= 0;
105 
106   for (ptr_str= str; ptr_str < str_end; ptr_str+= mbl)
107   {
108     mbl= my_mbcharlen(cs, *((uchar *) ptr_str));
109 
110     if (mbl == 0)
111       return 0;
112 
113     if (mbl == 1)
114     {
115       for (ptr_reject= reject; ptr_reject < reject_end; ++ptr_reject)
116       {
117         if (*ptr_reject == *ptr_str)
118           return (size_t) (ptr_str - str);
119       }
120     }
121   }
122   return (size_t) (ptr_str - str);
123 }
124