Drizzled Public API Documentation

strfunc.cc
1 /* Copyright (C) 2003 MySQL AB
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* Some useful string utility functions used by the MySQL server */
17 #include <config.h>
18 
19 #include <drizzled/typelib.h>
20 #include <drizzled/charset.h>
21 
22 namespace drizzled {
23 
24 /*
25  Function to find a string in a TYPELIB
26  (Same format as mysys/typelib.c)
27 
28  SYNOPSIS
29  find_type()
30  lib TYPELIB (struct of pointer to values + count)
31  find String to find
32  length Length of string to find
33  part_match Allow part matching of value
34 
35  RETURN
36  0 error
37  > 0 position in TYPELIB->type_names +1
38 */
39 
40 uint32_t TYPELIB::find_type(const char *find, uint32_t length, bool part_match) const
41 {
42  uint32_t found_count=0, found_pos=0;
43  const char* end= find + length;
44  const char* i;
45  const char* j;
46  for (uint32_t pos= 0 ; (j= type_names[pos++]); )
47  {
48  for (i= find ; i != end && system_charset_info->toupper(*i) == system_charset_info->toupper(*j); i++, j++)
49  {
50  }
51  if (i == end)
52  {
53  if (not *j)
54  return pos;
55  found_count++;
56  found_pos= pos;
57  }
58  }
59  return found_count == 1 && part_match ? found_pos : 0;
60 }
61 
62 
63 /*
64  Find a string in a list of strings according to collation
65 
66  SYNOPSIS
67  find_type2()
68  lib TYPELIB (struct of pointer to values + count)
69  x String to find
70  length String length
71  cs Character set + collation to use for comparison
72 
73  NOTES
74 
75  RETURN
76  0 No matching value
77  >0 Offset+1 in typelib for matched string
78 */
79 
80 uint32_t TYPELIB::find_type2(const char *x, uint32_t length, const charset_info_st *cs) const
81 {
82  if (!count)
83  return 0;
84  const char *j;
85  for (int pos=0 ; (j= type_names[pos]) ; pos++)
86  {
87  if (!my_strnncoll(cs, (const unsigned char*) x, length,
88  (const unsigned char*) j, type_lengths[pos]))
89  return pos + 1;
90  }
91  return 0;
92 } /* find_type */
93 
94 } /* namespace drizzled */