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, Inc.
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
25
#include <drizzled/charset_info.h>
26
#include <drizzled/function/str/strfunc.h>
27
#include <drizzled/plugin/function.h>
30
using namespace drizzled;
32
class SubstrFunction :public Item_str_func
36
SubstrFunction() :Item_str_func() {}
38
String *val_str(String *);
39
void fix_length_and_dec();
40
const char *func_name() const { return "substr"; }
42
bool check_argument_count(int n) { return n == 2 || n == 3; }
46
class SubstrIndexFunction :public Item_str_func
50
SubstrIndexFunction() :Item_str_func() {}
52
String *val_str(String *);
53
void fix_length_and_dec();
54
const char *func_name() const { return "substring_index"; }
56
bool check_argument_count(int n) { return n == 3; }
59
String *SubstrFunction::val_str(String *str)
62
String *res = args[0]->val_str(str);
63
/* must be int64_t to avoid truncation */
64
int64_t start= args[1]->val_int();
65
/* Assumes that the maximum length of a String is < INT32_MAX. */
66
/* Limit so that code sees out-of-bound value properly. */
67
int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
70
if ((null_value=(args[0]->null_value || args[1]->null_value ||
71
(arg_count == 3 && args[2]->null_value))))
74
/* Negative or zero length, will return empty string. */
75
if ((arg_count == 3) && (length <= 0) &&
76
(length == 0 || !args[2]->unsigned_flag))
77
return &my_empty_string;
79
/* Assumes that the maximum length of a String is < INT32_MAX. */
80
/* Set here so that rest of code sees out-of-bound value as such. */
81
if ((length <= 0) || (length > INT32_MAX))
84
/* if "unsigned_flag" is set, we have a *huge* positive number. */
85
/* Assumes that the maximum length of a String is < INT32_MAX. */
86
if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
87
(args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
88
return &my_empty_string;
91
static_cast<int64_t>(res->numchars() + start)
93
start= res->charpos((int) start);
94
if ((start < 0) || ((uint) start + 1 > res->length()))
95
return &my_empty_string;
97
length= res->charpos((int) length, (uint32_t) start);
98
tmp_length= res->length() - start;
99
length= min(length, tmp_length);
101
if (!start && (int64_t) res->length() == length)
103
tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
107
void SubstrFunction::fix_length_and_dec()
109
max_length=args[0]->max_length;
111
collation.set(args[0]->collation);
112
if (args[1]->const_item())
114
int32_t start= (int32_t) args[1]->val_int();
116
max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
118
max_length-= min((uint)(start - 1), max_length);
120
if (arg_count == 3 && args[2]->const_item())
122
int32_t length= (int32_t) args[2]->val_int();
126
set_if_smaller(max_length,(uint) length);
128
max_length*= collation.collation->mbmaxlen;
132
void SubstrIndexFunction::fix_length_and_dec()
134
max_length= args[0]->max_length;
136
if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
141
String *SubstrIndexFunction::val_str(String *str)
144
String *res= args[0]->val_str(str);
145
String *delimiter= args[1]->val_str(&tmp_value);
146
int32_t count= (int32_t) args[2]->val_int();
149
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
150
{ // string and/or delim are null
155
uint32_t delimiter_length= delimiter->length();
156
if (!res->length() || !delimiter_length || !count)
157
return &my_empty_string; // Wrong parameters
159
res->set_charset(collation.collation);
161
if (use_mb(res->charset()))
163
const char *ptr= res->ptr();
164
const char *strend= ptr+res->length();
165
const char *end= strend-delimiter_length+1;
166
const char *search= delimiter->ptr();
167
const char *search_end= search+delimiter_length;
168
int32_t n=0,c=count,pass;
170
for (pass=(count>0);pass<2;++pass)
177
i=(char*) ptr+1; j=(char*) search+1;
178
while (j != search_end)
179
if (*i++ != *j++) goto skip;
181
else if (!--c) break;
182
ptr+= delimiter_length;
186
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
188
} /* either not found or got total number when count<0 */
189
if (pass == 0) /* count<0 */
192
if (c<=0) return res; /* not found, return original string */
197
if (c) return res; /* Not found, return original string */
198
if (count>0) /* return left part */
200
tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
202
else /* return right part */
204
ptr+= delimiter_length;
205
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
213
{ // start counting from the beginning
214
for (offset=0; ; offset+= delimiter_length)
216
if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
217
return res; // Didn't find, return org string
220
tmp_value.set(*res,0,offset);
228
Negative index, start counting at the end
230
for (offset=res->length(); offset ;)
233
this call will result in finding the position pointing to one
234
address space less than where the found substring is located
237
if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
238
return res; // Didn't find, return org string
240
At this point, we've searched for the substring
241
the number of times as supplied by the index value
245
offset+= delimiter_length;
246
tmp_value.set(*res,offset,res->length()- offset);
253
We always mark tmp_value as const so that if val_str() is called again
254
on this object, we don't disrupt the contents of tmp_value when it was
255
derived from another String.
257
tmp_value.mark_as_const();
261
plugin::Create_function<SubstrFunction> *substr_function= NULL;
262
plugin::Create_function<SubstrIndexFunction> *substr_index_function= NULL;
264
static int initialize(drizzled::module::Context &context)
266
substr_function= new plugin::Create_function<SubstrFunction>("substr");
267
substr_index_function= new plugin::Create_function<SubstrIndexFunction>("substring_index");
268
context.add(substr_function);
269
context.add(substr_index_function);
273
DRIZZLE_DECLARE_PLUGIN
281
initialize, /* Plugin Init */
283
NULL /* config options */
285
DRIZZLE_DECLARE_PLUGIN_END;