~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/substr.cc

Merged from Toru - removal of my_time_t.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/function/str/substr.h>
 
23
 
 
24
String *Item_func_substr::val_str(String *str)
 
25
{
 
26
  assert(fixed == 1);
 
27
  String *res  = args[0]->val_str(str);
 
28
  /* must be int64_t to avoid truncation */
 
29
  int64_t start= args[1]->val_int();
 
30
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
31
  /* Limit so that code sees out-of-bound value properly. */
 
32
  int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
 
33
  int64_t tmp_length;
 
34
 
 
35
  if ((null_value=(args[0]->null_value || args[1]->null_value ||
 
36
                   (arg_count == 3 && args[2]->null_value))))
 
37
    return 0; /* purecov: inspected */
 
38
 
 
39
  /* Negative or zero length, will return empty string. */
 
40
  if ((arg_count == 3) && (length <= 0) &&
 
41
      (length == 0 || !args[2]->unsigned_flag))
 
42
    return &my_empty_string;
 
43
 
 
44
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
45
  /* Set here so that rest of code sees out-of-bound value as such. */
 
46
  if ((length <= 0) || (length > INT32_MAX))
 
47
    length= INT32_MAX;
 
48
 
 
49
  /* if "unsigned_flag" is set, we have a *huge* positive number. */
 
50
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
51
  if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
 
52
      (args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
 
53
    return &my_empty_string;
 
54
 
 
55
  start= ((start < 0) ? res->numchars() + start : start - 1);
 
56
  start= res->charpos((int) start);
 
57
  if ((start < 0) || ((uint) start + 1 > res->length()))
 
58
    return &my_empty_string;
 
59
 
 
60
  length= res->charpos((int) length, (uint32_t) start);
 
61
  tmp_length= res->length() - start;
 
62
  length= cmin(length, tmp_length);
 
63
 
 
64
  if (!start && (int64_t) res->length() == length)
 
65
    return res;
 
66
  tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
 
67
  return &tmp_value;
 
68
}
 
69
 
 
70
void Item_func_substr::fix_length_and_dec()
 
71
{
 
72
  max_length=args[0]->max_length;
 
73
 
 
74
  collation.set(args[0]->collation);
 
75
  if (args[1]->const_item())
 
76
  {
 
77
    int32_t start= (int32_t) args[1]->val_int();
 
78
    if (start < 0)
 
79
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
 
80
    else
 
81
      max_length-= cmin((uint)(start - 1), max_length);
 
82
  }
 
83
  if (arg_count == 3 && args[2]->const_item())
 
84
  {
 
85
    int32_t length= (int32_t) args[2]->val_int();
 
86
    if (length <= 0)
 
87
      max_length=0; /* purecov: inspected */
 
88
    else
 
89
      set_if_smaller(max_length,(uint) length);
 
90
  }
 
91
  max_length*= collation.collation->mbmaxlen;
 
92
}
 
93
 
 
94
 
 
95
void Item_func_substr_index::fix_length_and_dec()
 
96
{
 
97
  max_length= args[0]->max_length;
 
98
 
 
99
  if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
 
100
    return;
 
101
}
 
102
 
 
103
 
 
104
String *Item_func_substr_index::val_str(String *str)
 
105
{
 
106
  assert(fixed == 1);
 
107
  String *res= args[0]->val_str(str);
 
108
  String *delimiter= args[1]->val_str(&tmp_value);
 
109
  int32_t count= (int32_t) args[2]->val_int();
 
110
  uint32_t offset;
 
111
 
 
112
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
 
113
  {                                     // string and/or delim are null
 
114
    null_value=1;
 
115
    return 0;
 
116
  }
 
117
  null_value=0;
 
118
  uint32_t delimiter_length= delimiter->length();
 
119
  if (!res->length() || !delimiter_length || !count)
 
120
    return &my_empty_string;            // Wrong parameters
 
121
 
 
122
  res->set_charset(collation.collation);
 
123
 
 
124
#ifdef USE_MB
 
125
  if (use_mb(res->charset()))
 
126
  {
 
127
    const char *ptr= res->ptr();
 
128
    const char *strend= ptr+res->length();
 
129
    const char *end= strend-delimiter_length+1;
 
130
    const char *search= delimiter->ptr();
 
131
    const char *search_end= search+delimiter_length;
 
132
    int32_t n=0,c=count,pass;
 
133
    register uint32_t l;
 
134
    for (pass=(count>0);pass<2;++pass)
 
135
    {
 
136
      while (ptr < end)
 
137
      {
 
138
        if (*ptr == *search)
 
139
        {
 
140
          register char *i,*j;
 
141
          i=(char*) ptr+1; j=(char*) search+1;
 
142
          while (j != search_end)
 
143
            if (*i++ != *j++) goto skip;
 
144
          if (pass==0) ++n;
 
145
          else if (!--c) break;
 
146
          ptr+= delimiter_length;
 
147
          continue;
 
148
        }
 
149
    skip:
 
150
        if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
 
151
        else ++ptr;
 
152
      } /* either not found or got total number when count<0 */
 
153
      if (pass == 0) /* count<0 */
 
154
      {
 
155
        c+=n+1;
 
156
        if (c<=0) return res; /* not found, return original string */
 
157
        ptr=res->ptr();
 
158
      }
 
159
      else
 
160
      {
 
161
        if (c) return res; /* Not found, return original string */
 
162
        if (count>0) /* return left part */
 
163
        {
 
164
          tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
 
165
        }
 
166
        else /* return right part */
 
167
        {
 
168
          ptr+= delimiter_length;
 
169
          tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
 
170
        }
 
171
      }
 
172
    }
 
173
  }
 
174
  else
 
175
#endif /* USE_MB */
 
176
  {
 
177
    if (count > 0)
 
178
    {                                   // start counting from the beginning
 
179
      for (offset=0; ; offset+= delimiter_length)
 
180
      {
 
181
        if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
 
182
          return res;                   // Didn't find, return org string
 
183
        if (!--count)
 
184
        {
 
185
          tmp_value.set(*res,0,offset);
 
186
          break;
 
187
        }
 
188
      }
 
189
    }
 
190
    else
 
191
    {
 
192
      /*
 
193
        Negative index, start counting at the end
 
194
      */
 
195
      for (offset=res->length(); offset ;)
 
196
      {
 
197
        /*
 
198
          this call will result in finding the position pointing to one
 
199
          address space less than where the found substring is located
 
200
          in res
 
201
        */
 
202
        if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
 
203
          return res;                   // Didn't find, return org string
 
204
        /*
 
205
          At this point, we've searched for the substring
 
206
          the number of times as supplied by the index value
 
207
        */
 
208
        if (!++count)
 
209
        {
 
210
          offset+= delimiter_length;
 
211
          tmp_value.set(*res,offset,res->length()- offset);
 
212
          break;
 
213
        }
 
214
      }
 
215
    }
 
216
  }
 
217
  /*
 
218
    We always mark tmp_value as const so that if val_str() is called again
 
219
    on this object, we don't disrupt the contents of tmp_value when it was
 
220
    derived from another String.
 
221
  */
 
222
  tmp_value.mark_as_const();
 
223
  return (&tmp_value);
 
224
}