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
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.
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.
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
20
#include <drizzled/server_includes.h>
22
#include <drizzled/functions/str/substr.h>
24
String *Item_func_substr::val_str(String *str)
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;
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 */
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;
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))
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;
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;
60
length= res->charpos((int) length, (uint32_t) start);
61
tmp_length= res->length() - start;
62
length= cmin(length, tmp_length);
64
if (!start && (int64_t) res->length() == length)
66
tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
70
void Item_func_substr::fix_length_and_dec()
72
max_length=args[0]->max_length;
74
collation.set(args[0]->collation);
75
if (args[1]->const_item())
77
int32_t start= (int32_t) args[1]->val_int();
79
max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
81
max_length-= cmin((uint)(start - 1), max_length);
83
if (arg_count == 3 && args[2]->const_item())
85
int32_t length= (int32_t) args[2]->val_int();
87
max_length=0; /* purecov: inspected */
89
set_if_smaller(max_length,(uint) length);
91
max_length*= collation.collation->mbmaxlen;
95
void Item_func_substr_index::fix_length_and_dec()
97
max_length= args[0]->max_length;
99
if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
104
String *Item_func_substr_index::val_str(String *str)
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();
112
if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
113
{ // string and/or delim are null
118
uint32_t delimiter_length= delimiter->length();
119
if (!res->length() || !delimiter_length || !count)
120
return &my_empty_string; // Wrong parameters
122
res->set_charset(collation.collation);
125
if (use_mb(res->charset()))
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;
134
for (pass=(count>0);pass<2;++pass)
141
i=(char*) ptr+1; j=(char*) search+1;
142
while (j != search_end)
143
if (*i++ != *j++) goto skip;
145
else if (!--c) break;
146
ptr+= delimiter_length;
150
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
152
} /* either not found or got total number when count<0 */
153
if (pass == 0) /* count<0 */
156
if (c<=0) return res; /* not found, return original string */
161
if (c) return res; /* Not found, return original string */
162
if (count>0) /* return left part */
164
tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
166
else /* return right part */
168
ptr+= delimiter_length;
169
tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
178
{ // start counting from the beginning
179
for (offset=0; ; offset+= delimiter_length)
181
if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
182
return res; // Didn't find, return org string
185
tmp_value.set(*res,0,offset);
193
Negative index, start counting at the end
195
for (offset=res->length(); offset ;)
198
this call will result in finding the position pointing to one
199
address space less than where the found substring is located
202
if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
203
return res; // Didn't find, return org string
205
At this point, we've searched for the substring
206
the number of times as supplied by the index value
210
offset+= delimiter_length;
211
tmp_value.set(*res,offset,res->length()- offset);
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.
222
tmp_value.mark_as_const();