~drizzle-trunk/drizzle/development

574.3.5 by Lee
moving functions from item_strfunc to functions/str directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
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.
9
 *
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.
14
 *
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
18
 */
19
20
#include <drizzled/server_includes.h>
21
#include CSTDINT_H
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/str/trim.h>
574.3.5 by Lee
moving functions from item_strfunc to functions/str directory
23
24
String *Item_func_ltrim::val_str(String *str)
25
{
26
  assert(fixed == 1);
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;
31
32
  res= args[0]->val_str(str);
33
  if ((null_value=args[0]->null_value))
34
    return 0;
35
  remove_str= &remove;                          /* Default value. */
36
  if (arg_count == 2)
37
  {
38
    remove_str= args[1]->val_str(&tmp);
39
    if ((null_value= args[1]->null_value))
40
      return 0;
41
  }
42
43
  if ((remove_length= remove_str->length()) == 0 ||
44
      remove_length > res->length())
45
    return res;
46
47
  ptr= (char*) res->ptr();
48
  end= ptr+res->length();
49
  if (remove_length == 1)
50
  {
51
    char chr=(*remove_str)[0];
52
    while (ptr != end && *ptr == chr)
53
      ptr++;
54
  }
55
  else
56
  {
57
    const char *r_ptr=remove_str->ptr();
58
    end-=remove_length;
59
    while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
60
      ptr+=remove_length;
61
    end+=remove_length;
62
  }
63
  if (ptr == res->ptr())
64
    return res;
65
  tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
66
  return &tmp_value;
67
}
68
69
String *Item_func_rtrim::val_str(String *str)
70
{
71
  assert(fixed == 1);
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;
76
77
  res= args[0]->val_str(str);
78
  if ((null_value=args[0]->null_value))
79
    return 0;
80
  remove_str= &remove;                          /* Default value. */
81
  if (arg_count == 2)
82
  {
83
    remove_str= args[1]->val_str(&tmp);
84
    if ((null_value= args[1]->null_value))
85
      return 0;
86
  }
87
88
  if ((remove_length= remove_str->length()) == 0 ||
89
      remove_length > res->length())
90
    return res;
91
92
  ptr= (char*) res->ptr();
93
  end= ptr+res->length();
94
#ifdef USE_MB
95
  char *p=ptr;
96
  register uint32_t l;
97
#endif
98
  if (remove_length == 1)
99
  {
100
    char chr=(*remove_str)[0];
101
#ifdef USE_MB
102
    if (use_mb(res->charset()))
103
    {
104
      while (ptr < end)
105
      {
106
	if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l,p=ptr;
107
	else ++ptr;
108
      }
109
      ptr=p;
110
    }
111
#endif
112
    while (ptr != end  && end[-1] == chr)
113
      end--;
114
  }
115
  else
116
  {
117
    const char *r_ptr=remove_str->ptr();
118
#ifdef USE_MB
119
    if (use_mb(res->charset()))
120
    {
121
  loop:
122
      while (ptr + remove_length < end)
123
      {
124
	if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
125
	else ++ptr;
126
      }
127
      if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
128
      {
129
	end-=remove_length;
130
	ptr=p;
131
	goto loop;
132
      }
133
    }
134
    else
135
#endif /* USE_MB */
136
    {
137
      while (ptr + remove_length <= end &&
138
	     !memcmp(end-remove_length, r_ptr, remove_length))
139
	end-=remove_length;
140
    }
141
  }
142
  if (end == res->ptr()+res->length())
143
    return res;
144
  tmp_value.set(*res,0,(uint) (end-res->ptr()));
145
  return &tmp_value;
146
}
147
148
149
String *Item_func_trim::val_str(String *str)
150
{
151
  assert(fixed == 1);
152
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
153
  const char *r_ptr;
154
  String tmp(buff, sizeof(buff), system_charset_info);
155
  String *res, *remove_str;
156
  uint32_t remove_length;
157
158
  res= args[0]->val_str(str);
159
  if ((null_value=args[0]->null_value))
160
    return 0;
161
  remove_str= &remove;                          /* Default value. */
162
  if (arg_count == 2)
163
  {
164
    remove_str= args[1]->val_str(&tmp);
165
    if ((null_value= args[1]->null_value))
166
      return 0;
167
  }
168
169
  if ((remove_length= remove_str->length()) == 0 ||
170
      remove_length > res->length())
171
    return res;
172
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))
177
    ptr+=remove_length;
178
#ifdef USE_MB
179
  if (use_mb(res->charset()))
180
  {
181
    char *p=ptr;
182
    register uint32_t l;
183
 loop:
184
    while (ptr + remove_length < end)
185
    {
186
      if ((l=my_ismbchar(res->charset(), ptr,end))) ptr+=l;
187
      else ++ptr;
188
    }
189
    if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
190
    {
191
      end-=remove_length;
192
      ptr=p;
193
      goto loop;
194
    }
195
    ptr=p;
196
  }
197
  else
198
#endif /* USE_MB */
199
  {
200
    while (ptr + remove_length <= end &&
201
	   !memcmp(end-remove_length,r_ptr,remove_length))
202
      end-=remove_length;
203
  }
204
  if (ptr == res->ptr() && end == ptr+res->length())
205
    return res;
206
  tmp_value.set(*res,(uint) (ptr - res->ptr()),(uint) (end-ptr));
207
  return &tmp_value;
208
}
209
210
void Item_func_trim::fix_length_and_dec()
211
{
212
  max_length= args[0]->max_length;
213
  if (arg_count == 1)
214
  {
215
    collation.set(args[0]->collation);
216
    remove.set_charset(collation.collation);
217
    remove.set_ascii(" ",1);
218
  }
219
  else
220
  {
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))
224
      return;
225
  }
226
}
227
228
void Item_func_trim::print(String *str, enum_query_type query_type)
229
{
230
  if (arg_count == 1)
231
  {
232
    Item_func::print(str, query_type);
233
    return;
234
  }
235
  str->append(Item_func_trim::func_name());
236
  str->append('(');
237
  str->append(mode_name());
238
  str->append(' ');
239
  args[1]->print(str, query_type);
240
  str->append(STRING_WITH_LEN(" from "));
241
  args[0]->print(str, query_type);
242
  str->append(')');
243
}