1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
5
* Copyright (C) 2010 Stewart Smith
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 2 of the License.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <drizzled/plugin/function.h>
27
using namespace drizzled;
29
#include <drizzled/function/str/strfunc.h>
31
class SubstrFunction :public Item_str_func
35
SubstrFunction() :Item_str_func() {}
37
String *val_str(String *);
38
void fix_length_and_dec();
39
const char *func_name() const { return "substr"; }
41
bool check_argument_count(int n) { return n == 2 || n == 3; }
45
class SubstrIndexFunction :public Item_str_func
49
SubstrIndexFunction() :Item_str_func() {}
51
String *val_str(String *);
52
void fix_length_and_dec();
53
const char *func_name() const { return "substring_index"; }
55
bool check_argument_count(int n) { return n == 3; }
58
String *SubstrFunction::val_str(String *str)
61
String *res = args[0]->val_str(str);
62
/* must be int64_t to avoid truncation */
63
int64_t start= args[1]->val_int();
64
/* Assumes that the maximum length of a String is < INT32_MAX. */
65
/* Limit so that code sees out-of-bound value properly. */
66
int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
69
if ((null_value=(args[0]->null_value || args[1]->null_value ||
70
(arg_count == 3 && args[2]->null_value))))
73
/* Negative or zero length, will return empty string. */
74
if ((arg_count == 3) && (length <= 0) &&
75
(length == 0 || !args[2]->unsigned_flag))
76
return &my_empty_string;
78
/* Assumes that the maximum length of a String is < INT32_MAX. */
79
/* Set here so that rest of code sees out-of-bound value as such. */
80
if ((length <= 0) || (length > INT32_MAX))
83
/* if "unsigned_flag" is set, we have a *huge* positive number. */
84
/* Assumes that the maximum length of a String is < INT32_MAX. */
85
if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
86
(args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
87
return &my_empty_string;
89
start= ((start < 0) ? res->numchars() + start : start - 1);
90
start= res->charpos((int) start);
91
if ((start < 0) || ((uint) start + 1 > res->length()))
92
return &my_empty_string;
94
length= res->charpos((int) length, (uint32_t) start);
95
tmp_length= res->length() - start;
96
length= min(length, tmp_length);
98
if (!start && (int64_t) res->length() == length)
100
tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
104
void SubstrFunction::fix_length_and_dec()
106
max_length=args[0]->max_length;
108
collation.set(args[0]->collation);
109
if (args[1]->const_item())
111
int32_t start= (int32_t) args[1]->val_int();
113
max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
115
max_length-= min((uint)(start - 1), max_length);
117
if (arg_count == 3 && args[2]->const_item())
119
int32_t length= (int32_t) args[2]->val_int();
123
set_if_smaller(max_length,(uint) length);
125
max_length*= collation.collation->mbmaxlen;
129
void SubstrIndexFunction::fix_length_and_dec()
131
max_length= args[0]->max_length;
133
if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
138
String *SubstrIndexFunction::val_str(String *str)
141
String *res= args[0]->val_str(str);
142
String *delimiter= args[1]->val_str(&tmp_value);
143
int32_t count= (int32_t) args[2]->val_int();
146
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
147
{ // string and/or delim are null
152
uint32_t delimiter_length= delimiter->length();
153
if (!res->length() || !delimiter_length || !count)
154
return &my_empty_string; // Wrong parameters
156
res->set_charset(collation.collation);
158
if (use_mb(res->charset()))
160
const char *ptr= res->ptr();
161
const char *strend= ptr+res->length();
162
const char *end= strend-delimiter_length+1;
163
const char *search= delimiter->ptr();
164
const char *search_end= search+delimiter_length;
165
int32_t n=0,c=count,pass;
167
for (pass=(count>0);pass<2;++pass)
174
i=(char*) ptr+1; j=(char*) search+1;
175
while (j != search_end)
176
if (*i++ != *j++) goto skip;
178
else if (!--c) break;
179
ptr+= delimiter_length;
183
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
185
} /* either not found or got total number when count<0 */
186
if (pass == 0) /* count<0 */
189
if (c<=0) return res; /* not found, return original string */
194
if (c) return res; /* Not found, return original string */
195
if (count>0) /* return left part */
197
tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
199
else /* return right part */
201
ptr+= delimiter_length;
202
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
210
{ // start counting from the beginning
211
for (offset=0; ; offset+= delimiter_length)
213
if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
214
return res; // Didn't find, return org string
217
tmp_value.set(*res,0,offset);
225
Negative index, start counting at the end
227
for (offset=res->length(); offset ;)
230
this call will result in finding the position pointing to one
231
address space less than where the found substring is located
234
if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
235
return res; // Didn't find, return org string
237
At this point, we've searched for the substring
238
the number of times as supplied by the index value
242
offset+= delimiter_length;
243
tmp_value.set(*res,offset,res->length()- offset);
250
We always mark tmp_value as const so that if val_str() is called again
251
on this object, we don't disrupt the contents of tmp_value when it was
252
derived from another String.
254
tmp_value.mark_as_const();
258
plugin::Create_function<SubstrFunction> *substr_function= NULL;
259
plugin::Create_function<SubstrIndexFunction> *substr_index_function= NULL;
261
static int initialize(drizzled::plugin::Registry ®istry)
263
substr_function= new plugin::Create_function<SubstrFunction>("substr");
264
substr_index_function= new plugin::Create_function<SubstrIndexFunction>("substring_index");
265
registry.add(substr_function);
266
registry.add(substr_index_function);
270
static int finalize(drizzled::plugin::Registry ®istry)
272
registry.remove(substr_function);
273
registry.remove(substr_index_function);
274
delete substr_function;
275
delete substr_index_function;
279
DRIZZLE_DECLARE_PLUGIN
287
initialize, /* Plugin Init */
288
finalize, /* Plugin Deinit */
289
NULL, /* status variables */
290
NULL, /* system variables */
291
NULL /* config options */
293
DRIZZLE_DECLARE_PLUGIN_END;