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/function/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();
96
if (remove_length == 1)
98
char chr=(*remove_str)[0];
99
if (use_mb(res->charset()))
103
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
108
while (ptr != end && end[-1] == chr)
113
const char *r_ptr=remove_str->ptr();
114
if (use_mb(res->charset()))
117
while (ptr + remove_length < end)
119
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
122
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
131
while (ptr + remove_length <= end &&
132
!memcmp(end-remove_length, r_ptr, remove_length))
136
if (end == res->ptr()+res->length())
138
tmp_value.set(*res,0,(uint) (end-res->ptr()));
143
String *Item_func_trim::val_str(String *str)
146
char buff[MAX_FIELD_WIDTH], *ptr, *end;
148
String tmp(buff, sizeof(buff), system_charset_info);
149
String *res, *remove_str;
150
uint32_t remove_length;
152
res= args[0]->val_str(str);
153
if ((null_value=args[0]->null_value))
155
remove_str= &remove; /* Default value. */
158
remove_str= args[1]->val_str(&tmp);
159
if ((null_value= args[1]->null_value))
163
if ((remove_length= remove_str->length()) == 0 ||
164
remove_length > res->length())
167
ptr= (char*) res->ptr();
168
end= ptr+res->length();
169
r_ptr= remove_str->ptr();
170
while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
172
if (use_mb(res->charset()))
177
while (ptr + remove_length < end)
179
if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
182
if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
192
while (ptr + remove_length <= end &&
193
!memcmp(end-remove_length,r_ptr,remove_length))
196
if (ptr == res->ptr() && end == ptr+res->length())
198
tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
202
void Item_func_trim::fix_length_and_dec()
204
max_length= args[0]->max_length;
207
collation.set(args[0]->collation);
208
remove.set_charset(collation.collation);
209
remove.set_ascii(" ",1);
213
// Handle character set for args[1] and args[0].
214
// Note that we pass args[1] as the first item, and args[0] as the second.
215
if (agg_arg_charsets(collation, &args[1], 2, MY_COLL_CMP_CONV, -1))
220
void Item_func_trim::print(String *str, enum_query_type query_type)
224
Item_func::print(str, query_type);
227
str->append(Item_func_trim::func_name());
229
str->append(mode_name());
231
args[1]->print(str, query_type);
232
str->append(STRING_WITH_LEN(" from "));
233
args[0]->print(str, query_type);