~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/strfunc.cc

  • Committer: Andrew Hutchings
  • Date: 2011-02-01 10:23:22 UTC
  • mto: (2136.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2137.
  • Revision ID: andrew@linuxjedi.co.uk-20110201102322-oxztcyrjzg3c7yta
Fix counters cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
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 {
 
17
#include "config.h"
 
18
 
 
19
#include "drizzled/strfunc.h"
 
20
#include "drizzled/typelib.h"
 
21
#include "drizzled/charset_info.h"
 
22
#include "drizzled/global_charset_info.h"
 
23
 
 
24
namespace drizzled
 
25
{
 
26
 
 
27
/*
 
28
  Return bitmap for strings used in a set
 
29
 
 
30
  SYNOPSIS
 
31
  find_set()
 
32
  lib                   Strings in set
 
33
  str                   Strings of set-strings separated by ','
 
34
  err_pos               If error, set to point to start of wrong set string
 
35
  err_len               If error, set to the length of wrong set string
 
36
  set_warning           Set to 1 if some string in set couldn't be used
 
37
 
 
38
  NOTE
 
39
    We delete all end space from str before comparison
 
40
 
 
41
  RETURN
 
42
    bitmap of all sets found in x.
 
43
    set_warning is set to 1 if there was any sets that couldn't be set
 
44
*/
 
45
 
 
46
static const char field_separator=',';
 
47
 
 
48
uint64_t find_set(TYPELIB *lib, const char *str, uint32_t length,
 
49
                  const CHARSET_INFO * const cs,
 
50
                  char **err_pos, uint32_t *err_len, bool *set_warning)
 
51
{
 
52
  const CHARSET_INFO * const strip= cs ? cs : &my_charset_utf8_general_ci;
 
53
  const char *end= str + strip->cset->lengthsp(strip, str, length);
 
54
  uint64_t found= 0;
 
55
  *err_pos= 0;                  // No error yet
 
56
  if (str != end)
 
57
  {
 
58
    const char *start= str;
 
59
    for (;;)
 
60
    {
 
61
      const char *pos= start;
 
62
      uint32_t var_len;
 
63
      int mblen= 1;
 
64
 
 
65
      for (; pos != end && *pos != field_separator; pos++) 
 
66
      {}
 
67
      var_len= (uint32_t) (pos - start);
 
68
      uint32_t find= cs ? find_type2(lib, start, var_len, cs) :
 
69
                      find_type(lib, start, var_len, (bool) 0);
 
70
      if (!find)
 
71
      {
 
72
        *err_pos= (char*) start;
 
73
        *err_len= var_len;
 
74
        *set_warning= 1;
 
75
      }
 
76
      else
 
77
        found|= ((int64_t) 1 << (find - 1));
 
78
      if (pos >= end)
 
79
        break;
 
80
      start= pos + mblen;
 
81
    }
 
82
  }
 
83
  return found;
 
84
}
 
85
 
23
86
 
24
87
/*
25
88
  Function to find a string in a TYPELIB
37
100
  > 0 position in TYPELIB->type_names +1
38
101
*/
39
102
 
40
 
uint32_t TYPELIB::find_type(const char *find, uint32_t length, bool part_match) const
 
103
uint32_t find_type(const TYPELIB *lib, const char *find, uint32_t length,
 
104
               bool part_match)
41
105
{
42
106
  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++]) ; )
 
107
  const char *end= find+length;
 
108
  const char *i;
 
109
  const char *j;
 
110
  for (uint32_t pos=0 ; (j=lib->type_names[pos++]) ; )
47
111
  {
48
 
    for (i= find ; i != end && my_toupper(system_charset_info, *i) == my_toupper(system_charset_info, *j); i++, j++) 
49
 
    {
50
 
    }
 
112
    for (i=find ; i != end &&
 
113
           my_toupper(system_charset_info,*i) ==
 
114
           my_toupper(system_charset_info,*j) ; i++, j++) ;
51
115
    if (i == end)
52
116
    {
53
 
      if (not *j)
54
 
        return pos;
 
117
      if (! *j)
 
118
        return(pos);
55
119
      found_count++;
56
120
      found_pos= pos;
57
121
    }
58
122
  }
59
 
  return found_count == 1 && part_match ? found_pos : 0;
 
123
  return(found_count == 1 && part_match ? found_pos : 0);
60
124
}
61
125
 
62
126
 
77
141
    >0  Offset+1 in typelib for matched string
78
142
*/
79
143
 
80
 
uint32_t TYPELIB::find_type2(const char *x, uint32_t length, const charset_info_st *cs) const
 
144
uint32_t find_type2(const TYPELIB *typelib, const char *x, uint32_t length,
 
145
                const CHARSET_INFO * const cs)
81
146
{
82
 
  if (!count)
83
 
    return 0;
 
147
  int pos;
84
148
  const char *j;
85
 
  for (int pos=0 ; (j= type_names[pos]) ; pos++)
 
149
 
 
150
  if (!typelib->count)
 
151
  {
 
152
    return(0);
 
153
  }
 
154
 
 
155
  for (pos=0 ; (j=typelib->type_names[pos]) ; pos++)
86
156
  {
87
157
    if (!my_strnncoll(cs, (const unsigned char*) x, length,
88
 
                          (const unsigned char*) j, type_lengths[pos]))
89
 
      return pos + 1;
 
158
                          (const unsigned char*) j, typelib->type_lengths[pos]))
 
159
      return(pos+1);
90
160
  }
91
 
  return 0;
 
161
  return(0);
92
162
} /* find_type */
93
163
 
94
164
} /* namespace drizzled */