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/trim.h>
24
String *Item_func_ltrim::val_str(String *str)
27
char buff[MAX_FIELD_WIDTH], *ptr, *end;
28
String tmp(buff,sizeof(buff),system_charset_info);
29
String *res, *remove_str;
30
uint32_t remove_length;
32
res= args[0]->val_str(str);
33
if ((null_value=args[0]->null_value))
35
remove_str= &remove; /* Default value. */
38
remove_str= args[1]->val_str(&tmp);
39
if ((null_value= args[1]->null_value))
43
if ((remove_length= remove_str->length()) == 0 ||
44
remove_length > res->length())
47
ptr= (char*) res->ptr();
48
end= ptr+res->length();
49
if (remove_length == 1)
51
char chr=(*remove_str)[0];
52
while (ptr != end && *ptr == chr)
57
const char *r_ptr=remove_str->ptr();
59
while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
63
if (ptr == res->ptr())
65
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
69
String *Item_func_rtrim::val_str(String *str)
72
char buff[MAX_FIELD_WIDTH], *ptr, *end;
73
String tmp(buff, sizeof(buff), system_charset_info);
74
String *res, *remove_str;
75
uint32_t remove_length;
77
res= args[0]->val_str(str);
78
if ((null_value=args[0]->null_value))
80
remove_str= &remove; /* Default value. */
83
remove_str= args[1]->val_str(&tmp);
84
if ((null_value= args[1]->null_value))
88
if ((remove_length= remove_str->length()) == 0 ||
89
remove_length > res->length())
92
ptr= (char*) res->ptr();
93
end= ptr+res->length();
98
if (remove_length == 1)
100
char chr=(*remove_str)[0];
102
if (use_mb(res->charset()))
106
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
112
while (ptr != end && end[-1] == chr)
117
const char *r_ptr=remove_str->ptr();
119
if (use_mb(res->charset()))
122
while (ptr + remove_length < end)
124
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
127
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
137
while (ptr + remove_length <= end &&
138
!memcmp(end-remove_length, r_ptr, remove_length))
142
if (end == res->ptr()+res->length())
144
tmp_value.set(*res,0,(uint) (end-res->ptr()));
149
String *Item_func_trim::val_str(String *str)
152
char buff[MAX_FIELD_WIDTH], *ptr, *end;
154
String tmp(buff, sizeof(buff), system_charset_info);
155
String *res, *remove_str;
156
uint32_t remove_length;
158
res= args[0]->val_str(str);
159
if ((null_value=args[0]->null_value))
161
remove_str= &remove; /* Default value. */
164
remove_str= args[1]->val_str(&tmp);
165
if ((null_value= args[1]->null_value))
169
if ((remove_length= remove_str->length()) == 0 ||
170
remove_length > res->length())
173
ptr= (char*) res->ptr();
174
end= ptr+res->length();
175
r_ptr= remove_str->ptr();
176
while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
179
if (use_mb(res->charset()))
184
while (ptr + remove_length < end)
186
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
189
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
200
while (ptr + remove_length <= end &&
201
!memcmp(end-remove_length,r_ptr,remove_length))
204
if (ptr == res->ptr() && end == ptr+res->length())
206
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
210
void Item_func_trim::fix_length_and_dec()
212
max_length= args[0]->max_length;
215
collation.set(args[0]->collation);
216
remove.set_charset(collation.collation);
217
remove.set_ascii(" ",1);
221
// Handle character set for args[1] and args[0].
222
// Note that we pass args[1] as the first item, and args[0] as the second.
223
if (agg_arg_charsets(collation, &args[1], 2, MY_COLL_CMP_CONV, -1))
228
void Item_func_trim::print(String *str, enum_query_type query_type)
232
Item_func::print(str, query_type);
235
str->append(Item_func_trim::func_name());
237
str->append(mode_name());
239
args[1]->print(str, query_type);
240
str->append(STRING_WITH_LEN(" from "));
241
args[0]->print(str, query_type);