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;
90
static_cast<int64_t>(res->numchars() + start)
92
start= res->charpos((int) start);
93
if ((start < 0) || ((uint) start + 1 > res->length()))
94
return &my_empty_string;
96
length= res->charpos((int) length, (uint32_t) start);
97
tmp_length= res->length() - start;
98
length= min(length, tmp_length);
100
if (!start && (int64_t) res->length() == length)
102
tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
106
void SubstrFunction::fix_length_and_dec()
108
max_length=args[0]->max_length;
110
collation.set(args[0]->collation);
111
if (args[1]->const_item())
113
int32_t start= (int32_t) args[1]->val_int();
115
max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
117
max_length-= min((uint)(start - 1), max_length);
119
if (arg_count == 3 && args[2]->const_item())
121
int32_t length= (int32_t) args[2]->val_int();
125
set_if_smaller(max_length,(uint) length);
127
max_length*= collation.collation->mbmaxlen;
131
void SubstrIndexFunction::fix_length_and_dec()
133
max_length= args[0]->max_length;
135
if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
140
String *SubstrIndexFunction::val_str(String *str)
143
String *res= args[0]->val_str(str);
144
String *delimiter= args[1]->val_str(&tmp_value);
145
int32_t count= (int32_t) args[2]->val_int();
148
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
149
{ // string and/or delim are null
154
uint32_t delimiter_length= delimiter->length();
155
if (!res->length() || !delimiter_length || !count)
156
return &my_empty_string; // Wrong parameters
158
res->set_charset(collation.collation);
160
if (use_mb(res->charset()))
162
const char *ptr= res->ptr();
163
const char *strend= ptr+res->length();
164
const char *end= strend-delimiter_length+1;
165
const char *search= delimiter->ptr();
166
const char *search_end= search+delimiter_length;
167
int32_t n=0,c=count,pass;
169
for (pass=(count>0);pass<2;++pass)
176
i=(char*) ptr+1; j=(char*) search+1;
177
while (j != search_end)
178
if (*i++ != *j++) goto skip;
180
else if (!--c) break;
181
ptr+= delimiter_length;
185
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
187
} /* either not found or got total number when count<0 */
188
if (pass == 0) /* count<0 */
191
if (c<=0) return res; /* not found, return original string */
196
if (c) return res; /* Not found, return original string */
197
if (count>0) /* return left part */
199
tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
201
else /* return right part */
203
ptr+= delimiter_length;
204
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
212
{ // start counting from the beginning
213
for (offset=0; ; offset+= delimiter_length)
215
if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
216
return res; // Didn't find, return org string
219
tmp_value.set(*res,0,offset);
227
Negative index, start counting at the end
229
for (offset=res->length(); offset ;)
232
this call will result in finding the position pointing to one
233
address space less than where the found substring is located
236
if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
237
return res; // Didn't find, return org string
239
At this point, we've searched for the substring
240
the number of times as supplied by the index value
244
offset+= delimiter_length;
245
tmp_value.set(*res,offset,res->length()- offset);
252
We always mark tmp_value as const so that if val_str() is called again
253
on this object, we don't disrupt the contents of tmp_value when it was
254
derived from another String.
256
tmp_value.mark_as_const();
260
plugin::Create_function<SubstrFunction> *substr_function= NULL;
261
plugin::Create_function<SubstrIndexFunction> *substr_index_function= NULL;
263
static int initialize(drizzled::module::Context &context)
265
substr_function= new plugin::Create_function<SubstrFunction>("substr");
266
substr_index_function= new plugin::Create_function<SubstrIndexFunction>("substring_index");
267
context.add(substr_function);
268
context.add(substr_index_function);
272
DRIZZLE_DECLARE_PLUGIN
280
initialize, /* Plugin Init */
281
NULL, /* system variables */
282
NULL /* config options */
284
DRIZZLE_DECLARE_PLUGIN_END;